已知测试main函数,设计符合要求的Money类。
类Money:
数据成员:yuan,jiao,fen
成员函数:operator>>;operator<<;
operator+; ==
构造函数按情况来编写。
int main()
{ CMoney mymoney,yourmoney,addmoney;
cout<<“请输入钱数按元角分,中间以空格分开:”;
cin>>mymoney;
cout<<“请输入钱数按元角分,中间以空格分开:”;
cin>>yourmoney;
cout<<mymoney<<endl;
cout<<yourmoney;
if(mymoney==yourmoney)
cout<<“钱一样多。“<<endl;
addmoney=mymoney+yourmoney;
cout<<“一共有:”;
cout<<addmoney;
return 0;
}
#include<iostream>
using namespace std;
class Money
{
public:
Money()
{
yuan=0;
jiao=0;
fen=0;
}
Money(int y,int j,int f)
{
yuan=y;
jiao=j;
fen=f;
}
friend Money operator+(const Money m1,const Money m2)
{Money m0;
m0.yuan=m1.yuan+m2.yuan;
m0.jiao=m1.jiao+m2.jiao;
m0.fen=m1.fen+m2.fen;
if(m0.fen>9)
{
m0.fen-=10;
m0.jiao+=1;
}
if(m0.jiao>9)
{
m0.jiao-=10;
m0.yuan+=1;
}
return m0;
}
friend bool operator==(const Money m1,const Money m2)
{
if(m1.yuan==m2.yuan&&m1.jiao==m2.jiao&&m1.fen==m2.fen)
return true;
else return false;
}
friend istream &operator>>(istream &ins,Money&m)
{
ins>>m.yuan>>m.jiao>>m.fen;
}
friend ostream&operator<<(ostream&out,Money&m)
{
out<<m.yuan<<"yuan "<<m.jiao<<"jiao "<<m.fen<<"fen ";
}
private:
int yuan;
int jiao;
int fen;
};
int main()
{
Money mymoney,yourmoney,addmoney;
cout<<"请输入钱数按元角分,中间以空格分开:";
cin>>mymoney;
cout<<"请输入钱数按元角分,中间以空格分开:";
cin>>yourmoney;
cout<<mymoney<<endl;
cout<<yourmoney;
if(mymoney==yourmoney)
cout<<"钱一样多。"<<endl;
addmoney=mymoney+yourmoney;
cout<<"一共有:";
cout<<addmoney;
return 0;
}
“你说你喜欢长安,而我叫十三,长安十三朝的十三”