65,210
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
using namespace std;
class record
{
private:
int staffn;
char firstn[20],familyn[20];
int sal;
public:
record()
{
staffn=999; strcpy(firstn,"New"); strcpy(familyn,"Staff"); sal=0;
}
record(int staffn_in, char firstn_in[20], char familyn_in[20], int sal_in)
{
staffn=staffn_in;
strcpy(firstn, firstn_in);
strcpy(familyn, familyn_in);
sal=sal_in;
}
void show(void)
{
cout <<endl;
cout <<"The staff number is:" <<staffn <<endl;
cout <<"The staff's first name is: " <<firstn <<endl;
cout <<"The staff's family name is: " <<familyn <<endl;
cout <<"The staff's salary is: " <<sal <<endl;
cout <<"End." <<endl;
}
string return_name() //参数设计的很混乱
{
char str1[20], str2[20];
cout <<"Please enter the staff's first name>" <<endl;
cin>>str1;
strcpy(firstn,str1);
cout <<"Please enter the staff's family name>" <<endl;
cin.get();////////////////////////////////////////////////////加这一行
cin>>str2;
strcpy(familyn,str2);
string str3(str1);
str3 +=str2;
return str3;
}
void changestaffn(void)
{
int sn;
cout <<"Please enter the the staff number>" <<endl;
cin>>sn;
staffn=sn;
cout <<"Action finished with " <<firstn <<" " <<familyn <<endl;
}
void changesal(void)
{
int s;
cout <<"please enter the staff's salary>" <<endl;
cin>>s;
sal=s;
cout <<"Action finished with " <<firstn <<" " <<familyn <<endl;
}
void action(void)
{
int choice;
cout <<"what do you want to do with " <<firstn <<" " <<familyn <<endl;
cout <<"***********************************************" <<endl;
cout <<"1. Display. 2. change the staff's name." <<endl;
cout <<"3. change staff number. " <<endl;
cout <<"4. change staff's salary. 5. exit. " <<endl;
cout <<"***********************************************" <<endl;
cin>>choice;
if(choice==1)
show();
else if(choice==2)
return_name();
else if(choice==3)
changestaffn();
else if(choice==4)
changesal();
}
friend int operator + (const record &left, const record &right)
{
return (left.sal + right.sal);
}
};
class manager //Another class to manipulate the objects of class person ?ACTING AS A MANAGER.
{
private:
record p1; //declare a object
record p2;
public:
void run(void) // choose between different person
{
int userchoice=1;
while(userchoice>0 && userchoice <3)
{
cout <<"input your choice: 1 for p1, 2 for p2, 3 to exit" <<endl;
cin>>userchoice;
cin.get();
if (userchoice==1) p1.action();
if (userchoice==2) p2.action();
}
}
};
int main()
{
manager m1;
m1.run();
system("pause");
};