第8章多态性与运算符重载
约 1247 字大约 4 分钟
2026-11-10
8-1 P309 复数类加减法运算重载——成员函数形式
源文件:8_01.cpp
/*
8-1 P309
复数类加减法运算重载——成员函数形式
*/
#include <iostream>
using namespace std;
class Complex //复数类
{
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex &c2) const; //运算符+重载的成员函数
Complex operator-(const Complex &c2) const; //运算符-重载的成员函数
void display() const; //显示复数的值
private:
double real;
double imag;
};
Complex Complex::operator+(const Complex &c2) const
{
return Complex(real + c2.real, imag + c2.imag); //创建一个临时对象作为返回值,这里调用了Complex类的构造函数
}
Complex Complex::operator-(const Complex &c2) const
{
return Complex(real - c2.real, imag - c2.imag); //创建一个临时对象作为返回值,这里调用了Complex类的构造函数
}
void Complex::display() const
{ //显示复数中的内容
cout << "(" << real << "," << imag << ")" << endl;
}
int main()
{
Complex c1(5, 4), c2(2, 10), c3; //定义复数类的对象
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();
c3 = c1 - c2; //使用重载运算符完成复数减法
cout << "c3=c1-c2=";
c3.display();
c3 = c1 + c2; //使用重载运算符完成复数加法
cout << "c3=c1+c2=";
c3.display();
return 0;
}8-2 P311 将单目运算符“++”重载为成员函数的形式
源文件:8_02.cpp
/*
8-2 P311
将单目运算符“++”重载为成员函数的形式
*/
#include <iostream>
using namespace std;
class Clock
{ //时钟类定义
public:
Clock(int hour = 0, int minute = 0, int second = 0);
void showTime() const;
Clock &operator++(); //前置单目运算符重载
Clock operator++(int); //后置单目运算符重载
private:
int hour, minute, second;
};
Clock::Clock(int hour, int minute, int second)
{
if (0 <= hour && hour < 24 && 0 <= minute && minute < 60 && 0 <= second && second < 60)
{
this->hour = hour;
this->minute = minute;
this->second = second;
}
else
cout << "Time error!" << endl;
}
void Clock::showTime() const
{ //显示时间
cout << hour << ":" << minute << ":" << second << endl;
}
Clock &Clock::operator++()
{
second++;
if (second >= 60)
{
second -= 60;
minute++;
if (minute >= 60)
{
minute -= 60;
hour = (hour + 1) % 24;
}
}
return *this;
}
Clock Clock::operator++(int)
{ //注意形参表中的整型参数
Clock old = *this;
++(*this); //调用前置“++”运算符
return old;
}
int main()
{
Clock myClock(23, 59, 59);
cout << "First time output: ";
myClock.showTime();
cout << "Show myClock++: ";
(myClock++).showTime();
cout << "Show ++myClock: ";
(++myClock).showTime();
return 0;
}8-3 P313 以非成员函数形式重载Complex的加减法运算和“<<”运算符
源文件:8_03.cpp
/*
8-3 P313
以非成员函数形式重载Complex的加减法运算和“<<”运算符
*/
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
friend Complex operator+(const Complex &c1, const Complex &c2);
friend Complex operator-(const Complex &c1, const Complex &c2);
friend ostream &operator<<(ostream &out, const Complex &c);
private:
double real; //复数实部
double imag; //复数虚部
};
Complex operator+(const Complex &c1, const Complex &c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
Complex operator-(const Complex &c1, const Complex &c2)
{
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
ostream &operator<<(ostream &out, const Complex &c)
{
out << "(" << c.real << ", " << c.imag << ")";
return out;
}
int main()
{
Complex c1(5, 4), c2(2, 10), c3;
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
c3 = c1 - c2; //使用重载运算符完成复数减法
cout << "c3 = c1 - c2 = " << c3 << endl;
c3 = c1 + c2; //使用重载运算符完成复数加法
cout << "c3 = c1 + c2 = " << c3 << endl;
return 0;
}8-4 P316 虚函数成员
源文件:8_04.cpp
/*
8-4 P316
虚函数成员
*/
#include <iostream>
using namespace std;
class Base1
{
public:
// void display() const;//这个不是虚函数,会类型转换
virtual void display() const; //这个函数是虚函数,可多态
};
class Base2 : public Base1
{
public:
void display() const;
};
class Derived : public Base2
{
public:
void display() const;
};
void Base1::display() const
{
cout << "Base1::display()" << endl;
}
void Base2::display() const
{
cout << "Base2::display()" << endl;
}
void Derived::display() const
{
cout << "Derived::display()" << endl;
}
void fun(Base1 *p)
{
p->display();
}
void fun1(Base1 b)
{ //使用对象名,绑定发生在编译过程中!
b.display();
}
int main()
{
Base1 b1;
Base2 b2;
Derived d1;
fun(&b1);
fun(&b2);
fun(&d1);
//使用对象名,绑定发生在编译过程中!
fun1(b1);
fun1(b2);
fun1(d1);
return 0;
}8-5 P320 虚析构函数举例
源文件:8_05.cpp
/*
8-5 P320
虚析构函数举例
*/
#include <iostream>
using namespace std;
class Base
{
public:
// ~Base(); //不是虚函数
virtual ~Base(); //是虚函数
};
Base::~Base()
{
cout << "Base destructor" << endl;
}
class Derived : public Base
{
public:
Derived();
// ~Derived(); //不是虚函数
virtual ~Derived(); //是虚函数,基类虚函数要加virtual,派生类的函数不加virtual也行
private:
int *p;
};
Derived::Derived()
{
p = new int(0);
}
Derived::~Derived()
{
cout << "Derived destructor" << endl;
delete p;
}
void fun(Base *b)
{
delete b; //静态绑定,只会调用~Base()
}
int main()
{
Base *b = new Derived();
fun(b);
return 0;
}8-6 P323 抽象类举例
源文件:8_06.cpp
/*
8-6 P323
抽象类举例
*/
#include <iostream>
using namespace std;
class Base1 //基类Base1定义
{
public:
virtual void display() const = 0; //纯虚函数
};
class Base2 : public Base1 //公有派生类Base2定义
{
public:
void display() const; //覆盖基类纯虚函数
};
void Base2::display() const
{
cout << "Base2::display" << endl;
}
class Derived : public Base2 //公有派生类Derived定义
{
public:
void display() const;
};
void Derived::display() const
{
cout << "Derived::display" << endl;
}
void fun(Base1 *ptr)
{
ptr->display();
}
int main()
{
// Base1 b1;//错误,声明基类对象
Base1 *p; //正确,声明基类指针
Base2 b2; //声明派生类对象
Derived d1; //声明派生类对象
p = &b2;
fun(p); //调用派生类Base2函数成员
p = &d1;
fun(p); //调用派生类Derived函数成员
return 0;
}