定义一个员工类Employee,其成员数据包括编号id、姓名name和年龄age 5
3、定义一个员工类Employee,其成员数据包括编号id、姓名name和年龄age,在构造函数中增加相关代码实现记录总员工人数和计算所有员工的平均年龄。补充并运行代码:...
3、定义一个员工类Employee,其成员数据包括编号id、姓名name和年龄age,在构造函数中增加相关代码实现记录总员工人数和计算所有员工的平均年龄。
补充并运行代码:
#include <iostream> // 包含头文件iostream
using namespace std; // 使用命名空间std
class Employee
{
public:
Employee(int pId,char* pName,int age);
~Employee();
void printEmployee();
static float average(); //静态成员函数声明
private:
int id;
char* name;
int age;
static float sum;//定义静态成员数据
static int count;
};
Employee::Employee(int pId,char* pName,int pAge)
{
id = pId;
name = new char[strlen(pName)+1];
if(name != 0)
strcpy(name,pName);
age = pAge;
count++;
allAges+=age;
cout<<"Constructed with all parameters."<<endl;
}
float Student::sum=0; //静态成员数据初始化
float Student::count=0;
Employee::~Employee()
{
delete[] name;
count--;
allAges-=age;
cout<<"Deconstructed."<<endl;
}
void Employee::printEmployee()
{
cout<<"id: "<<id<<", "
<<"name: "<<name<<", "
<<"age: "<<age<<endl;
}
void Employee::printStat()//定义静态成员函数
{
cout<<"The number of employees is: "<< <<endl;
cout<<"The average age is: "<< <<endl;
}
int main()
{
Employee e1(1001,"zhangsan",22);
Employee e2(1002,"lisi",23);
Employee e3(1003,"wangwu",25);
Employee e4(1004,"zhouliu",20);
//使用对象名引用静态成员函数
/使用类名引用静态成员函数
return 0;
}
void Employee::printStat()//定义静态成员函数
{
cout<<"The number of employees is: "<<____________<<endl;
cout<<"The average age is: "<<_____________<<endl;
}
int main()
{
Employee e1(1001,"zhangsan",22);
Employee e2(1002,"lisi",23);
Employee e3(1003,"wangwu",25);
Employee e4(1004,"zhouliu",20);
__________________ //使用对象名引用静态成员函数
________________/使用类名引用静态成员函数
return 0;
} 展开
补充并运行代码:
#include <iostream> // 包含头文件iostream
using namespace std; // 使用命名空间std
class Employee
{
public:
Employee(int pId,char* pName,int age);
~Employee();
void printEmployee();
static float average(); //静态成员函数声明
private:
int id;
char* name;
int age;
static float sum;//定义静态成员数据
static int count;
};
Employee::Employee(int pId,char* pName,int pAge)
{
id = pId;
name = new char[strlen(pName)+1];
if(name != 0)
strcpy(name,pName);
age = pAge;
count++;
allAges+=age;
cout<<"Constructed with all parameters."<<endl;
}
float Student::sum=0; //静态成员数据初始化
float Student::count=0;
Employee::~Employee()
{
delete[] name;
count--;
allAges-=age;
cout<<"Deconstructed."<<endl;
}
void Employee::printEmployee()
{
cout<<"id: "<<id<<", "
<<"name: "<<name<<", "
<<"age: "<<age<<endl;
}
void Employee::printStat()//定义静态成员函数
{
cout<<"The number of employees is: "<< <<endl;
cout<<"The average age is: "<< <<endl;
}
int main()
{
Employee e1(1001,"zhangsan",22);
Employee e2(1002,"lisi",23);
Employee e3(1003,"wangwu",25);
Employee e4(1004,"zhouliu",20);
//使用对象名引用静态成员函数
/使用类名引用静态成员函数
return 0;
}
void Employee::printStat()//定义静态成员函数
{
cout<<"The number of employees is: "<<____________<<endl;
cout<<"The average age is: "<<_____________<<endl;
}
int main()
{
Employee e1(1001,"zhangsan",22);
Employee e2(1002,"lisi",23);
Employee e3(1003,"wangwu",25);
Employee e4(1004,"zhouliu",20);
__________________ //使用对象名引用静态成员函数
________________/使用类名引用静态成员函数
return 0;
} 展开
2个回答
展开全部
你这个程序是自己编的吧?里面有点错误呢。
allAges没在private:里面声明过,怎么就用了呢。
在public:里面声明了average(),但是后面没有定义。
又看了看你的程序,把allAges改成sum就对了。
还有,静态数据成员初始化把Student改成Employee。主程序上面printStat请改成printEmployee,声明的时候明明是printEmployee!!
哎,开头忘了using,应该是using namespace std;。静态数据成员初始化把第二句的float改成int,明明声明的时候是int嘛。
刚才说错,不是改printStat,而是应该在public里面声明printStat。老实说横线上我也不知道填什么。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询