C++重载运算符问题
aqqpz 2009-04-12 09:26:41 #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( );
}
原题是
4. Add more functions to the person class so for each person, options should be available to the user to display or change the name, ID number and office number (through the action( ) function of the class person). The operator == should be overloaded for the class person so it can be used to check if two persons are identical (returning a logical true or false), i.e. all the corresponding data members of the two persons have the same values.
5. Add more functions to the person class and modify the record class so that the programme will be able to deal with 3 persons. There should also be options in the run( ) function of the record class to allow comparison of any two persons to see if they are identical.
对于
bool operator==(person & aperson)
{
return this->name( ) ==aperson.name( );
}
我的这个程序有错误:项不会计算为接受 0 个参数的函数
请各位高手帮我看些还有其他问题不,谢谢
急!!!!!!!!!!!