例10.1 通过函数来实现复数相加
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){ real = 0; imag = 0; }//在类内定义构造函数
Complex(double r, double i){ real = r; imag = i; }
//构造函数重载并定义
Complex complex_add(Complex&c2);
void display();
private:
double real;
double imag;
};
Complex Complex::complex_add(Complex &c2)
{
Complex c;
c.real = real + c2.real;
c.imag = imag + c2.imag;
return c;
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;//定义三个复数对象
c3 = c1.complex_add(c2);
cout << "c1="; c1.display();
cout << "c2="; c2.display();
cout << "c1+c2"; c3.display();
return 0;
}
程序执行效果如图:
例10.2 对运算符"+"实行重载:对上例改写如下:
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){ real = 0; imag = 0; }//在类内定义构造函数
Complex(double r, double i){ real = r; imag = i; }
//构造函数重载并定义
Complex operator +(Complex&c2);//声明重载运算符+的函数
void display();
private:
double real;
double imag;
};
Complex Complex::operator +(Complex &c2)//定义重载运算符+的函数
{
Complex c;
c.real = real + c2.real;
c.imag = imag + c2.imag;
return c;
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;//定义三个复数对象
c3 = c1 + c2;
cout << "c1="; c1.display();
cout << "c2="; c2.display();
cout << "c1+c2"; c3.display();
return 0;
}
程序执行结果和例10.1一样。
例10.3将运算符+重载放在类外,作为complex类的友元函数。
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){ real = 0; imag = 0; }//在类内定义构造函数
Complex(double r, double i){ real = r; imag = i; }
//构造函数重载并定义
friend Complex operator +(Complex&c1,Complex &c2);
//声明重载运算符+的函数作为友元函数
void display();
private:
double real;
double imag;
};
Complex operator +(Complex &c1, Complex &c2)//定义重载运算符+的函数
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;//定义三个复数对象
c3 = c1 + c2;
cout << "c1="; c1.display();
cout << "c2="; c2.display();
cout << "c1+c2"; c3.display();
return 0;
}
程序执行结果和例10.1一样。
#include <iostream>
using namespace std;
#include<string>
class String
{
public:
String(){ p = NULL; }//定义默认构造函数
String(char *str);//声明构造函数
friend bool operator>(String &string1, String&string2);
//声明运算符函数为重载函数
friend bool operator<(String &string1, String&string2);
friend bool operator==(String &string1, String&string2);
void display();
private:
char *p;//字符型指针用于指向字符串
};
String::String(char *str)//定义构造函数
{
p = str;
}
void String::display()
{
cout <<p;
}
bool operator>(String&string1, String&string2)
{
if (strcmp(string1.p, string2.p) > 0)
return true;
else return false;
}
bool operator<(String&string1, String&string2)
{
if (strcmp(string1.p, string2.p) < 0)
return true;
else return false;
}
bool operator==(String&string1, String&string2)
{
if (strcmp(string1.p, string2.p) == 0)
return true;
else return false;
}
void compare(String&string1, String&string2)
{
if (operator>(string1,string2)==1)
{string1.display(); cout << ">"; string2.display();}
else
if (operator<(string1, string2) == 1)
{
string1.display(); cout << "<"; string2.display();
}
else
if (operator==(string1, string2) == 1)
{
string1.display(); cout << "=="; string2.display();
}
cout << endl;
}
int main()
{
String string1("Hello"), string2("Book"),string3("Compare")
, string4("Hello");
compare(string1 ,string2) ;
compare(string2, string3) ;
compare(string1, string4);
return 0;
}
程序执行结果如图:
例10.5 单目运算符++的重载
#include <iostream>
using namespace std;
#include<string>
class Time
{
public:
Time(){ minute = 0; sec = 0; }//默认构造函数
Time(int m, int s) :minute(m), sec(s){}//构造函数重载
Time operator++();//声明运算符成员函数
void display(){
cout << minute << ":" << sec << endl;}
private:
int minute;
int sec;
};
Time Time::operator++()//定义运算符重载成员函数
{
if (++sec >= 60)
{
sec -= 60;
++minute;
}
return *this;
}
int main(){
Time time1(34, 0);
for (int i = 0; i < 61;i++)
{
++time1;
time1.display();
}
return 0;
}
程序执行结果如图;