【题目】1. 设计如下类CPerson实现对人的封装,包括私有数据成员:身份证号ID、姓名Name和年龄Age。定义含三个参数的构造函数、拷贝构造函数;定义函数set(),提示键盘输入ID、Name和Age,以修改身份证号、姓名和年龄;定义输出函数display()。
# include <iostream>
using namespace std;
class CPerson
{
private :
int ID;
string Name;
int age;
public :
CPerson ( int i= 0 , string n = " " , int a= 0 )
{
ID = a;
Name = n;
age = a;
}
CPerson ( const CPerson& cp)
{
ID = cp. ID;
Name = cp. Name;
age = cp. age;
}
void set ( int i, string n, int a)
{
ID = i;
Name = n;
age = a;
}
void display ( )
{
cout << "ID:" << ID << "\t" << "Name:" << Name << "\t" << "age:" << age << endl;
}
} ;
int main ( )
{
CPerson obj1 ( 1002 , "LiMing" , 22 ) ;
obj1. display ( ) ;
CPerson obj2 ( obj1) ;
obj2. set ( 1222 , "liu" , 34 ) ;
obj2. display ( ) ;
return 0 ;
}
2. 在上一题基础上,设计类CEmployee公有继承CPerson,新定义私有数据成员工号EmpNo和薪资Salary。定义带参数的构造函数,并通过成员初始化列表向其基类传递参数;重定义函数set(),提示通过键盘输入Salary,以修改薪资;重定义函数display(),并在其内部访问基类中的同名函数。主函数如下:
# include <iostream>
using namespace std;
class CPerson
{
private :
int ID;
string Name;
int age;
public :
CPerson ( int i = 0 , string n = " " , int a = 0 )
{
ID = a;
Name = n;
age = a;
}
CPerson ( const CPerson& cp)
{
ID = cp. ID;
Name = cp. Name;
age = cp. age;
}
void set ( )
{
cin>> ID;
cin>> Name;
cin>> age;
}
void display ( )
{
cout << "ID:" << ID << "\t" << "Name:" << Name << "\t" << "age:" << age ;
}
} ;
class CEmployee : public CPerson
{
private :
string EmpNo;
double Salary;
public :
CEmployee ( string EN, double Sa, int i, string n, int a) : CPerson ( i, n, a)
{
EmpNo = EN;
Salary = Sa;
}
void set ( )
{
CPerson :: set ( ) ;
cin >> EmpNo;
cin >> Salary;
}
void display ( )
{
CPerson :: display ( ) ;
cout << "EmpNo:" << EmpNo << "\t" << "Salary:" << Salary << endl;
}
} ;
int main ( )
{
CEmployee e1 ( "LiMing" , 82.22 , 22 , "A101" , 5500 ) ;
e1. display ( ) ;
e1. set ( ) ;
e1. display ( ) ;
return 0 ;
}
3·在上一题类CEmployee中,新增静态成员count和sum,以统计员工总人数和总薪资。定义静态成员函数display_sum(),以显示count和平均薪资;并在主函数中实现对上述相关成员的访问。
# include <iostream>
using namespace std;
class CPerson
{
private :
int ID;
string Name;
int age;
public :
CPerson ( int i = 0 , string n = " " , int a = 0 )
{
ID = a;
Name = n;
age = a;
}
CPerson ( const CPerson& cp)
{
ID = cp. ID;
Name = cp. Name;
age = cp. age;
}
void set ( )
{
cin >> ID;
cin >> Name;
cin >> age;
}
void display ( )
{
cout << "ID:" << ID << "\t" << "Name:" << Name << "\t" << "age:" << age;
}
} ;
class CEmployee : public CPerson
{
private :
string EmpNo;
double Salary;
int static sum;
double static count;
public :
CEmployee ( int i, string n, int a, string EN, double Sa) : CPerson ( i, n, a)
{
EmpNo = EN;
Salary = Sa;
sum++ ;
count = count + Salary;
}
void set ( )
{
CPerson :: set ( ) ;
cin >> EmpNo;
cin >> Salary;
sum++ ;
count = count + Salary;
}
void display ( )
{
CPerson :: display ( ) ;
cout << "EmpNo:" << EmpNo << "\t" << "Salary:" << Salary << endl;
}
static void display_sum ( )
{
cout << "总薪资为:" << count << "\t" << "平均薪资为:" << count / sum << endl;
}
} ;
double CEmployee:: count = 0 ;
int CEmployee:: sum = 0 ;
int main ( )
{
CEmployee e1 ( 22 , "A101" , 5500 , "LiMing" , 82.22 ) ;
e1. display ( ) ;
e1. set ( ) ;
e1. display ( ) ;
e1. display_sum ( ) ;
return 0 ;
}