Singer类

描述

 

实现一个Singer类,通过以下测试:

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;
}

 

输入

 

输入包含两行,第一行为歌手s1的信息,第二行为歌手s2的信息,每位歌手的信息包括姓名(不包含空格)、性别、年龄 和 分数;姓名、性别、年龄和分数之间用空格分隔

 

输出

 

输出为三行,前两行分别是歌手s1和s2的信息,第三行根据s1和s2比较结果输出(s1和s2的比较结果和他们的分数的比较结果一致),具体参见主函数

 

输入样例 1 

Mary F 28 99.5
Peter M 26 98

输出样例 1

Mary F 28 99.5
Peter M 26 98
Mary's score is higher than Peter's.
#include <iostream>
#include <string>
using namespace std;

struct Singer
{
	private:
		string name;
		char sex;
		int year;
		float score;
	
	public:
		
		friend istream & operator>>(istream &is,Singer &s)
		{
			is>>s.name>>s.sex>>s.year>>s.score;
			return is;
		};
		
		friend ostream & operator<<(ostream &os,Singer &s)
		{
			os<<s.name<<" "<<s.sex<<" "<<s.year<<" "<<s.score;
			return os;
		};
		
		friend bool operator>(const Singer &s1,const Singer &s2)
		{
			if(s1.score>s2.score)
				return 1;
			return 0;
		};
		
		friend bool operator==(const Singer &s1,const Singer &s2)
		{
			if(s1.score==s2.score)
				return 1;
			return 0;
		};
		
		string getName()
		{
			return name;
		};
		
};

 

posted on 2020-02-23 00:28  海月CSDN  阅读(158)  评论(0)    收藏  举报