问一个运算符重载的问题
#include<iostream>#include<string>
using namespace std;
class Account
{
private:
float checking,savings,cash_reserve;
public:
Account(float ch=0,float sa=0,float ca=0)
{
checking=ch;
savings=sa;
cash_reserve=ca;
}
float total()
{
float z;
z=checking+savings+cash_reserve;
return z;
}
int operator<(Account x) //account与另一个account进行比较
{
if (total()<x.total())
return 1;
else return 0;
}
int operator<(int x) //account与一个整数进行比较
{
if (total()<x)
return 1;
else return 0;
}
int operator<(int x,Account a) //一个整数与account进行比较应该如何编,这样可以对吗?
{
if (a.total()<x)
return 0;
else return 1;
}
~Account()
{
cout<<checking<<" "<<savings<<" "<<cash_reserve<<endl;
}
};
void main()
{
Account a(0,100,30),b(20,110,10),c(40,30,10);
if(a<b)
cout<<"b has more than a\n";
else
cout<<"a has more than b\n";
if(c<100)
cout<<"c has more than 100\n";
else
cout<<"c has less than 100\n";
if(100<a)
cout<<"a has more than 100\n";
else
cout<<"a has less than 100\n";
}
这个程序有问题,一个整数与account进行比较,格式应该是怎么样的?