运算符重载:
bool Rectangle::operator==(const Rectangle& s)
{
if(this==&s) return true;
if((xlow==s.xlow)&&(ylow==s.ylow)&&(height==s.height)&&(width==s.width))
return true;
else
return flase;
}
ostream& operator<<(ostream& os, Rectangle& r)
{
os<<"position is:"<<r.xlow<<" ";
os<<r.ylow<<endl;
os<<"height is:"<<r.height<<endl;
os<<"width is"<<r.width<<endl;
return os;
}
bool Rectangle::opreator<(const Rectangle& s)
{
int s1,s2;
s1=height*width;
s2=s.height*s.width;
if(s1<s2)
return true;
else
return false;
}
习题2.1.6
(3)currency类
#pragma once
#include<iostream>
using namespace std;
class currency
{
private:
int $;
int cents;
public:
currency(int dollars, int cent)
{
$ = dollars;
cents = cent;
}
~currency();
void getcurrency()
{
cout << $ << endl;
cout << cents << endl;
}
currency& operator+(currency& );
friend currency& operator*(currency&,float , float );
friend
ostream& operator<<(ostream& os, currency& r);
friend istream& operator>>(istream&in,currency& r);
};
ostream& operator<<(ostream& os, currency& r)
{
os << r.$ << endl;
os << r.cents << endl;
return os;
}
istream& operator>>(istream& in,currency& r)
{
in >> r.$;
in >> r.cents;
return in;
}
currency currency::operator+(currency& s)
{
$ += s.$;
cents += s.cents;
}
currency currency::operator*(currency& r,float a, float b)
{
}