C++的问题,求助
#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的地方出了问题,但我不知道哪里有问题,感谢 你好!首先,你的代码中缺少了头文件的引用,应该加上`#include <iostream>`。
接下来是构造函数的实现部分。构造函数用于初始化对象的成员变量。根据你的代码,构造函数应该是这样的:
Singer::Singer(string n, char s, int a, double sc)
{
name = n;
sex = s;
age = a;
score = sc;
}
在这里,我们使用了参数列表来初始化成员变量。
接下来是比较运算符的重载部分。根据你的代码,重载的比较运算符应该是这样的:
bool Singer::operator>(Singer& a)
{
return score > a.score;
}
bool Singer::operator==(Singer& b)
{
return score == b.score;
}
这里我们使用了成员函数的方式来重载比较运算符。
最后,是输出流运算符的重载部分。按照你的代码,重载的输出流运算符应该是这样的:
ostream& operator<<(ostream& os, Singer& sin)
{
os << sin.name << " " << sin.sex << " " << sin.age << " " << sin.score;
return os;
}
这里我们使用了friend函数的方式来重载输出流运算符。
完整的代码如下所示:
#include <iostream>
using namespace std;
class Singer
{
protected:
string name;
char sex;
int age;
double score;
public:
Singer(string name = "", char s = '?', int a = 0, double n = 0);
string getName() const { return name; };
friend istream& operator>>(istream&, Singer&);
bool operator>(Singer& a);
bool operator==(Singer& b);
friend ostream& operator<<(ostream&, Singer&);
};
Singer::Singer(string n, char s, int a, double sc)
{
name = n;
sex = s;
age = a;
score = sc;
}
bool Singer::operator>(Singer& a)
{
return score > a.score;
}
bool Singer::operator==(Singer& b)
{
return score == b.score;
}
istream& operator>>(istream& myin, Singer& sin)
{
myin >> sin.name >> sin.sex >> sin.age >> sin.score;
return myin;
}
ostream& operator<<(ostream& os, Singer& sin)
{
os << sin.name << " " << sin.sex << " " << sin.age << " " << sin.score;
return os;
}
int main()
{
Singer s1, s2;
cin >> s1 >> s2;
cout << s1 << endl;
cout << s2 << endl;
if (s1 > s2)
{
cout << "s1的得分大于s2" << endl;
}
else if (s1 == s2)
{
cout << "s1和s2的得分相等" << endl;
}
else
{
cout << "s1的得分小于s2" << endl;
}
return 0;
}
希望能帮到你!如果还有其他问题,请随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]