65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string.h>
using namespace std;
class person
{
private:
int id;
char name[50];
int office;
int nowhour;
public:
void show_nowhour(void)
{
cout <<endl;
cout <<name <<" has worked " <<nowhour <<" hours" <<endl;
}
//default constructor
person()
{
id=0; strcpy(name,"Not known"); office=123; nowhour=0;
}
//normal constructor
person(int id_in, char name_in[50], int office_in,
int nowhour_in)
{
id=id_in;
strcpy(name,name_in);
office=office_in;
nowhour=nowhour_in;
}
void changename(void)
{
char name0[100];
cout <<"Please input new name" <<endl;
cin.getline(name0,100);
strcpy(name,name0);
cout <<"Action finished with " <<name <<endl;
}
void changeid(void)
{
int value1;
cout <<"Please input new value for id" <<endl;
cin>>value1;
id=value1;
cout <<"Action finished with " <<name <<endl;
}
void changeoffice(void)
{
int value2;
cout <<"Please input new value for office" <<endl;
cin>>value2;
cin.ignore();
office=value2;
cout <<"Action finished with " <<name <<endl;
}
void changenowhour(void)
{
int value;
cout <<"Please input new value for nowhour" <<endl;
cin>>value;
cin.ignore();
nowhour=value;
cout <<"Action finished with " <<name <<endl;
}
//a function that asks a user to perform action on its data
void action(void)
{
int choice;
cout <<"what do you want to do with " <<name <<endl;
cout <<" 1 for display, 2 for changing hours of working,3 for changing name, 4 for change id, 5 for changing office" <<endl;
cin>>choice;
cin.ignore();
if(choice==1)
show_nowhour( );
if(choice==2)
changenowhour( );
if(choice==3)
changename();
if (choice==4)
changeid();
if(choice==5)
changeoffice();
}
bool operator==(person & aperson)
{
return this->name == aperson.name;
}
};
// a class to handle two objects of CLASS person
class record
{
private:
person p1,p2,p3;
public:
void run(void)
{
int userchoice=1;
while(userchoice>0 && userchoice <4)
{
cout <<"input your choice: 1 for p1, 2 for p2, 3 for p3;4 to exit" <<endl;
cin>>userchoice;
cin.ignore();
if (userchoice==1) p1.action();
if (userchoice==2) p2.action();
if (userchoice==3) p3.action();
if(p1==p2)
cout <<"p1 and p2 are the same." <<endl;
if(p1==p3)
cout <<"p1 and p3 are the same." <<endl;
if(p3==p2)
cout <<"p3 and p2 are the same." <<endl;
}
}
};
//main programme follows all class definition
void main( void )
{
record r1;
r1.run( );
}