一.
const Magic& c
std::ostream &
二.
1.
#include <iostream>
using namespace std;
class Complex
{
private:
double x;
double y;
public:
friend const Complex operator+(const Complex &L,const Complex &R);
friend const Complex operator+(double num1,const Complex &R);
friend const Complex operator+(const Complex &L,const double num1);
friend ostream &operator<<(ostream &cout, const Complex &p);
friend istream & operator>>(istream &cin, Complex &p);
Complex()
{}
Complex(double a,double b):x(a),y(b)
{}
};
const Complex operator+(const Complex &L,const Complex &R)
{
Complex sum;
sum.x=L.x+R.x;
sum.y=L.y+R.y;
return sum;
}
const Complex operator+(const double num1,const Complex &R)
{
Complex sum;
sum.x=num1+R.x;
sum.y=num1+R.y;
return sum;
}
const Complex operator+(const Complex &L,const double num1)
{
Complex sum;
sum.x=L.x+num1;
sum.y=L.y+num1;
return sum;
}
ostream &operator<<(ostream &cout, const Complex &p)
{
cout << p.x << endl;
cout << p.y << endl;
return cout;
}
istream & operator>>(istream &cin,Complex &p)
{
cin >> p.x >> p.y ;
return cin;
}
int main()
{
}
2.
#include <iostream>
using namespace std;
class Time
{
friend const Time operator+(const Time &L,const Time &R);
friend const Time operator-(const Time &L,const Time &R);
friend ostream &operator<<(ostream &cout, const Time &p);
friend istream & operator>>(istream &cin, Time &p);
private:
int hours;
int minutes;
int seconds;
public:
Time()
{}
Time(int h,int m,int s):hours(h),minutes(m),seconds(s)
{}
};
const Time operator+(const Time &L,const Time &R)
{
Time sum;
sum.hours=L.hours+R.hours;
sum.minutes=L.minutes+R.minutes;
sum.seconds=L.seconds+R.seconds;
if(sum.seconds>=60)
{
sum.minutes+=sum.seconds/60;
sum.seconds=sum.seconds%60;
}
if(sum.minutes>=60)
{
sum.hours+=sum.minutes/60;
sum.minutes=sum.minutes%60;
}
return sum;
}
const Time operator-(const Time &L,const Time &R)
{
Time sum;
sum.hours=L.hours-R.hours;
sum.minutes=L.minutes-R.minutes;
sum.seconds=L.seconds-R.seconds;
if(sum.seconds>=60)
{
sum.minutes+=sum.seconds/60;
sum.seconds=sum.seconds%60;
}
if(sum.minutes>=60)
{
sum.hours+=sum.minutes/60;
sum.minutes=sum.minutes%60;
}
return sum;
}
ostream &operator<<(ostream &cout, const Time &p)
{
cout << p.hours << endl;
cout << p.minutes << endl;
cout << p.seconds << endl;
return cout;
}
istream & operator>>(istream &cin,Time &p)
{
cin >> p.hours >> p.minutes >> p.seconds;
return cin;
}
int main()
{
}
3.
#include <iostream>
using namespace std;
class Money
{
private:
double number;
public:
friend const Money operator*(const Money &L,const double num1);
friend const Money operator*(double num1,const Money &R);
friend ostream &operator<<(ostream &cout, const Money &p);
Money()
{}
Money(double a):number(a)
{}
};
const Money operator*(const double num1,const Money &R)
{
Money sum;
sum.number=num1*R.number;
return sum;
}
const Money operator*(const Money &L,const double num1)
{
Money sum;
sum.number=num1*L.number;
return sum;
}
ostream &operator<<(ostream &cout, const Money &p)
{
cout << p.number << endl;
return cout;
}
int main()
{
}