一. 多态的概念和意义
1.函数重写回顾
父类中被重写的函数依然会继承给子类
子类中重写的函数将覆盖父类中的函数
通过作用域分辨符(::)可以访问到父类中的函数
Child c;
Parent* p = &c;
c.Parent::print(); //从父类中继承
c.print(); //在子类中重写
p->print(); //父类中定义
2.多态的概念和意义
面向对象中期望的行为:
(1)根据实际的对象类型判断如何调用重写函数
(2)父类指针(引用)指向父类对象则调用父类中定义的函数
(3)父类指针(引用)指向子类对象则调用子类中定义的重写函数
面向对象中的多态的概念:
(1)根据实际的对象类型决定函数调用的具体目标
(2)同样的调用语句在实际运行时有多种不同的表现形态
C++语言直接支持多态的概念:
(1)通过使用virtual关键字对多态进行支持
(2)被virtual声明的函数被重写后具有多态特性
(3)被virtual声明的函数叫做虚函数
多态示例:
#include <iostream>
#include <string>
using namespace std;
class Parent
{
public:
virtual void print()
{
cout << "I'm Parent." << endl;
}
};
class Child : public Parent
{
public:
virtual void print()
{
cout << "I'm Child." << endl;
}
};
void how_to_print(Parent* p)
{
p->print(); // 展现多态的行为
}
int main()
{
Parent p;
Child c;
how_to_print(&p); // Expected to print: I'm Parent.
how_to_print(&c); // Expected to print: I'm Child.
return 0;
}
运行结果
I'm Parent.
I'm Child.
多态的意义:
- 在程序运行过程中展现出动态的特性
- 函数重写必须用多态实现,否则没有意义
- 多态是面向对象组件化程序设计的基础特性
理论中的概念:
- 静态联编:在程序的编译期间就能确定具体的函数调用(函数重载)
- 动态联编:在程序实际运行后才能确定具体的函数调用(函数重写)
示例:
#include <iostream>
#include <string>
using namespace std;
class Parent
{
public:
virtual void func()
{
cout << "void func()" << endl;
}
virtual void func(int i)
{
cout << "void func(int i) : " << i << endl;
}
virtual void func(int i, int j)
{
cout << "void func(int i, int j) : " << "(" << i << ", " << j << ")" << endl;
}
};
class Child : public Parent
{
public:
void func(int i, int j)
{
cout << "void func(int i, int j) : " << i + j << endl;
}
void func(int i, int j, int k)
{
cout << "void func(int i, int j, int k) : " << i + j + k << endl;
}
};
void run(Parent* p)
{
p->func(1, 2); // Showing polymorphic characteristics
// Dynamic binding
}
int main()
{
Parent p;
p.func(); // Static binding
p.func(1); // Static binding
p.func(1, 2); // Static binding
cout << endl;
Child c;
c.func(1, 2); // Static binding
cout << endl;
run(&p);
run(&c);
return 0;
}
运行结果
void func()
void func(int i) : 1
void func(int i, int j) : (1, 2)
void func(int i, int j) : 3
void func(int i, int j) : (1, 2)
void func(int i, int j) : 3
小结
函数重写只可能发生在父类与子类之间
根据实际对象的类型确定调用的具体函数
virtual关键字是C++中支持多态的唯一方式
被重写的虚函数可表现出多态的特性
二.C++中的抽象类和接口
1.面向对象中的抽象类
- 可用于表现现实世界中的抽象概念
- 是一种只能定义类型,而不能产生对象的类
- 只能被继承并重写相关函数
- 直接特征是相关函数没有完整的实现
在现实中需要知道具体的图形类型才能求面积,所以对概念上的“图形”求面积是没有意义的!
class Shape
{
public:
double area()
{
retrun 0;
}
};
Shape只是一个概念上的类型,没有具体对象!
Shape是现实世界中各种图形的抽象概念:
程序中必须能够反映抽象的图形
程序中通过抽象类表示图形的概念
抽象类不能创建对象,只能用于继承
2.抽象类与纯虚函数
C++语言中没有抽象类的概念
C++中通过纯虚函数实现抽象类
纯虚函数是指只定义原型的成员函数
一个C++类中存在纯虚函数就成为了抽象类
纯虚函数的语法规则:
class Shape
{
public:
virtual double area() = 0;
};
“=0” 用于告诉编译器当前是声明纯虚函数,因此不需要定义函数体。
抽象类示例:
#include <iostream>
#include <string>
using namespace std;
class Shape
{
public:
virtual double area() = 0;
};
class Rect : public Shape
{
int ma;
int mb;
public:
Rect(int a, int b)
{
ma = a;
mb = b;
}
double area()
{
return ma * mb;
}
};
class Circle : public Shape
{
int mr;
public:
Circle(int r)
{
mr = r;
}
double area()
{
return 3.14 * mr * mr;
}
};
void area(Shape* p)
{
double r = p->area();
cout << "r = " << r << endl;
}
int main()
{
Rect rect(1,2);
Circle circle(10);
area(&rect);
area(&circle);
return 0;
}
运行结果:
r = 2
r = 314
程序分析:
同一条调用语句,不同的调用结果,显示了多态的特性,抽象类不能够定义具体的对象,
但是可以定义抽象类的指针,对应的指针指向抽象类的子类,
抽象类的子类肯定会把纯虚函数实现,
因此通过抽象类的指针来调用纯虚函数的语句是可以编译通过,没有问题的。
- 抽象类只能用作父类被继承
- 子类必须实现纯虚函数的具体功能
- 纯虚函数被实现后成为虚函数
- 如果子类没有实现纯虚函数,则子类成为抽象类
3.接口的定义
满足下面条件的C++类则称为接口
- 类中没有定义任何的成员变量
- 所有的成员函数都是共有的
- 所有的成员函数都是纯虚函数
- 接口是一种特殊的抽象类
接口示例:
#include <iostream>
#include <string>
using namespace std;
class Channel
{
public:
virtual bool open() = 0;
virtual void close() = 0;
virtual bool send(char* buf, int len) = 0;
virtual int receive(char* buf, int len) = 0;
};
int main()
{
return 0;
}
小结:
抽象类用于描述现实世界中的抽象概念
抽象类只能被继承不能创建对象
C++中没有抽象类的概念
C++中通过纯虚函数实现抽象类
类中只存在纯虚函数时成为接口
接口是一种特殊的抽象类
三.关于虚函数的深入探讨
问题一:构造函数是否可以成为虚函数?析构函数是否可以成为虚函数?
- 构造函数 不可能 成为虚函数
在构造函数执行结束后,虚函数表指针才会被正确的初始化
- 析构函数 可以 成为虚函数
建议在设计类时将析构函数声明为虚函数
构造,析构,虚函数示例:
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
Base() {
cout << "Base()" << endl;
}
virtual void func() {
cout << "Base::func()" << endl;
}
virtual ~Base() {
cout << "~Base()" << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived()" << endl;
}
virtual void func() {
cout << "Derived::func()" << endl;
}
~Derived() {
cout << "~Derived()" << endl;
}
};
int main()
{
Base* p = new Derived();//父类指针指向子类对象
// ...
delete p;
return 0;
}
运行结果:
(1)如果父类析构函数不是虚函数,运行结果
Base()
Derived()
~Base()
没有子类的析构函数,delete p,删除了父类的指针,析构函数不是虚函数,
因此编译器直接根据指针p决定调用父类的析构函数。
(2)如果父类析构函数是虚函数,运行结果
Base()
Derived()
~Derived()
~Base()
编译器不会简单暴力的根据指针p来判断调用父类还是子类的析构函数,编译器会根据指针p
所指向的实际对象来决定如何调用析构函数
编译器不会简单暴力的根据指针p来判断调用父类还是子类的析构函数,编译器会根据指针p
所指向的实际对象来决定如何调用析构函数
问题二:构造函数中是否可以发生多态?析构函数中是否可以发生多态?
- 构造函数中 不可能 发生多态行为
在构造函数执行时,虚函数表指针未被正确初始化
- 析构函数中 不可能发生多态行为
在析构函数执行时,虚函数表指针已经被销毁
构造函数和析构函数中不能发生多态行为,只调用当前类中定义的函数版本!!
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
Base() {
cout << "Base()" << endl;
func();
}
virtual void func() {
cout << "Base::func()" << endl;
}
virtual ~Base() {
cout << "~Base()" << endl;
func();
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived()" << endl;
func();
}
virtual void func() {
cout << "Derived::func()" << endl;
}
~Derived() {
cout << "~Derived()" << endl;
func();
}
};
int main()
{
Base* p = new Derived();//父类指针指向子类对象
// ...
delete p;
return 0;
}
运行结果:
Base()
Base::func() //不发生多态,直接调用当前版本父类中的func
Derived()
Derived::func()
~Derived()
Derived::func()
~Base()
Base::func()