1
2
#include <iostream>
using namespace std;
class Complex
{
private:
double a;
double b;
public:
Complex(){}
Complex(double a,double b):a(a),b(b)
{}
const Complex operator+(const Complex &R)const
{
Complex temp;
temp.a=a+R.a;
temp.b=b+R.b;
return temp;
}
const Complex operator+(double s)const
{
Complex temp;
temp.a=a+s;
temp.b=b;
return temp;
}
friend const Complex operator+(double s,Complex &R);
void show()
{
cout << "(" << a << "," << b << ")" << endl;
}
};
const Complex operator+(double s,Complex &R)
{
Complex temp;
temp.a=s+R.a;
temp.b=R.b;
return temp;
}
int main()
{
Complex a(2,5);
Complex b(7,8);
Complex c(0,0);
c=a+b;
c.show();
c=4.1+a;
c.show();
c=b+5.6;
c.show();
return 0;
}
3
#include <iostream>
using namespace std;
class Time
{
private:
int hour;
int minute;
int second;
public:
Time(){}
Time(int h,int m,int s):hour(h),minute(m),second(s)
{}
const Time operator+(const Time &R)
{
int s;
Time temp;
s=second+R.second+minute*60+R.minute*60+hour*3600+R.hour*3600;
temp.second=s%60;
temp.minute=(s/60)%60;
temp.hour=s/3600;
return temp;
}
const Time operator-(const Time &R)
{
int s;
Time temp;
s=(second+minute*60+hour*3600)-(R.second+R.minute*60+R.hour*3600);
if(s<0)
{
s=s+24*3600;
}
temp.second=s%60;
temp.minute=(s/60)%60;
temp.hour=s/3600;
return temp;
}
void show()
{
cout << hour << ":" << minute << ":" << second << endl;
}
};
int main()
{
Time a(10,40,55);
Time b(5,23,23);
Time c;
c=a+b;
c.show();
c=a-b;
c.show();
return 0;
}
4
#include <iostream>
using namespace std;
class Money
{
private:
int m;
public:
Money(){}
Money(int m):m(m){}
friend Money operator*(const Money &R,double s);
friend Money operator*(double s,const Money &R);
void show()
{
cout << m << endl;
}
};
Money operator*(const Money &L,double s)
{
Money temp;
temp.m=L.m*s;
return temp;
}
Money operator*(double s,const Money &R)
{
Money temp;
temp.m=s*R.m;
return temp;
}
int main()
{
Money a(12);
Money b;
b=a*2.4;
b.show();
b=1.2*a;
b.show();
return 0;
}