|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<iostream>
- using namespace std;
- class Singer
- {
- protected:
- string name;
- char sex;
- int age;
- double score;
- public:
- string getName() const{ return name; };
- Singer(string name="", char s='?' ,int a=0,double n=0 );//构造函数要写的
- friend istream& operator >> (istream &, Singer &);
- bool operator >(Singer & a) ;
- bool operator ==(Singer &b) ;
- friend ostream& operator << (ostream &, const Singer &);
- };
- Singer::Singer(string name, char s, int a,double n )
- {
- this->name= name;
- score = n;
- sex = s;
- age = a;
- }
- istream& operator >> (istream & myin, Singer & sin)
- {
- myin >> sin.name >> sin.sex >> sin.age >> sin.score;
- return myin;
- }
- ostream& operator << (ostream & myout, const Singer & sing)
- {
- myout << sing.name << sing.sex << sing.age << sing.score;
- return myout;
- }
- bool operator >(Singer & a)
- {
- if(this->score > a.score)
- return true;
- else
- return false;
- }
- bool operator ==(Singer &b)
- {
- if(this->score == b.score)
- return true;
- else
- return false;
- }
- int main()
- {
- Singer s1,s2;
- cin>>s1>>s2;
- cout<<s1<<"\n"<<s2<<endl;
- if(s1>s2)
- cout<<s1.getName()<<"'s score is higher than "<<s2.getName()<<"'s.\n";
- else if(s1==s2)
- cout<<s1.getName()<<"'s score is equal to "<<s2.getName()<<"'s.\n";
- else
- cout<<s1.getName()<<"'s score is lower than "<<s2.getName()<<"'s.\n";
- return 0;
- }
复制代码
//bool的地方出了问题,但我不知道哪里有问题,感谢 |
|