当前位置:   article > 正文

BJFU2023-C++程序设计-实验4-运算符重载_cout << (c1 != c2) << endl;报错

cout << (c1 != c2) << endl;报错

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.

提示

后台已有main函数,提交时去掉main函数

  1. // 注意:无需提交main函数
  2. #include<iostream>
  3. using namespace std;
  4. class Singer {
  5. public:
  6. string m_name;
  7. char m_sex;
  8. int m_age;
  9. double m_score;
  10. public:
  11. string getName();
  12. friend istream& operator>>(istream& in, Singer &s);
  13. friend ostream& operator<<(ostream& out, const Singer &s);
  14. friend bool operator>(const Singer s1, const Singer s2);
  15. friend bool operator==(const Singer s1, const Singer s2);
  16. };
  17. bool operator>(const Singer s1, const Singer s2) {
  18. if (s1.m_score > s2.m_score)
  19. return true;
  20. else
  21. return false;
  22. }
  23. bool operator==(const Singer s1, const Singer s2) {
  24. if (s1.m_score == s2.m_score)
  25. return true;
  26. else
  27. return false;
  28. }
  29. istream& operator>>(istream& in, Singer &s) {
  30. in >> s.m_name >> s.m_sex >> s.m_age >> s.m_score;
  31. return in;
  32. }
  33. ostream& operator<<(ostream& out, const Singer &s) {
  34. out << s.m_name <<" " << s.m_sex <<" "<<s.m_age << " " << s.m_score ;
  35. return out;
  36. }
  37. string Singer::getName() {
  38. return m_name;
  39. }

Sales_data类

描述

实现以下Sales_data类(包括它的友元函数):

class Sales_data {

//依次输入书号、销量和收入

friend istream & operator>>(istream&, Sales_data &);

//依次输出书号、销量、收入和均价

friend ostream & operator<<(ostream &, const Sales_data &);

// 判两个对象==的条件是它们的3个属性(书号、销量和收入)都一样

friend bool operator==(const Sales_data &, const Sales_data &);

friend bool operator!=(const Sales_data &, const Sales_data &);

// for "+", assume that both objects refer to the same book

friend Sales_data operator+(const Sales_data &, const Sales_data &);

public:

Sales_data(): units_sold(0), revenue(0.0) {}

Sales_data(const string & s, unsigned n, double r): bookNo(s), units_sold(n), revenue(r) {}

string get_bookNo() const;

// for "+=", assume that both objects refer to the same book

Sales_data & operator+=(const Sales_data &);

private:

double avg_price() const; //均价,等于收入除以销量

string bookNo; //书号

unsigned units_sold; //销量

double revenue; //收入

};

通过以下main函数的测试

int main(){

Sales_data item1,item2;

while(cin>>item1>>item2){

cout<<item1<<"\n"<<item2<<"\n";

if(item1==item2)

cout<<item1.get_bookNo()<<" equals "<<item2.get_bookNo()<<"\n";

if(item1!=item2)

cout<<item1.get_bookNo()<<" doesn't equal "<<item2.get_bookNo()<<"\n";

cout<<(item1+item2)<<"\n";

item1 += item2;

cout<<item1<<"\n";

}

return 0;

}

输入

输入多组数据,每组数据两行,每行表示1个Sales_data对象,依次是书号、销量和收入

输出

对于每组数据,输出5行,具体参见main函数和输出样例

输入样例 1 

001 10 100.0
001 10 100.0

输出样例 1

001 10 100 10
001 10 100 10
001 equals 001
001 20 200 10
001 20 200 10

输入样例 2 

002 5 250
003 8 400

输出样例 2

002 5 250 50
003 8 400 50
002 doesn't equal 003
002 13 650 50
002 13 650 50

提示

后台已有main函数,提交时去掉main函数

  1. // 注意:无需提交main函数
  2. #include<iostream>
  3. using namespace std;
  4. class Sales_data {
  5. friend istream& operator>>(istream&, Sales_data&);
  6. friend ostream& operator<<(ostream&, const Sales_data&);
  7. friend bool operato
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/806842
推荐阅读
相关标签
  

闽ICP备14008679号