C++【多态】和【覆盖】
来源:互联网 发布:电视阿里云与安卓区别 编辑:程序博客网 时间:2024/06/10 03:31
多态=虚函数+指针/引用
关于多态:
形状:位置,绘制
矩形:宽度、高度,绘制
圆形:半径,绘制
Shape
/ \
Rect Circle
如果将基类中的某个成员函数声明为虚函数,那么其子类中与该函数具有相同原型的成员函数就也成为虚函数,并对基类中的版本构成覆盖(override)。通过一个指向子类对象的基类指针,或者引用子类对象的基类引用,调用这个虚函数时,实际被调用的将是子类中的覆盖版本。这种特性被称为多态。
关于覆盖
1.基类中成员函数必须是虚函数。
2.子类中成员函数必须与基类中的虚函数拥有完全相同的函数名、形参表和常属性。
3.如果基类中的虚函数返回基本类型,那么子类覆盖版本的返回类型必须与基类完全相同。如果基类中的虚函数返回类类型的指针或者引用,那么子类覆盖版本的返回类型可以是基类返回类型的子类。
class X { … };
class Y : public X { … };
class A {
virtual int foo (void) { … }
virtual X* bar (void) { … }
};
class B : public A {
int foo (void) { … }
Y* bar (void) { … }
};
4.子类中的覆盖版本不能比基类版本抛出更多的异常。
5.子类中覆盖版本与基类版本的访控属性无关。
class A {
public:
virtual int foo (void) { … }
};
class B : public A {
private
int foo (void) { … }
};
B b;
b.foo (); // ERROR !
A* p = &b;
p -> foo (); // OK !
多态的实现离不开基类与子类之间的覆盖关系
示例代码:
#include <iostream>using namespace std;class Shape {public: Shape (int x, int y) : m_x (x), m_y (y) {} virtual void draw (void) const = 0;protected: int m_x; int m_y;};class Rect : public Shape {public: Rect (int x, int y, int w, int h) : Shape (x, y), m_w (w), m_h (h) {} void draw (void) const { cout << "矩形:" << m_x << "," << m_y << "," << m_w << "," << m_h << endl; }private: int m_w; int m_h;};class Circle : public Shape {public: Circle (int x, int y, int r) : Shape (x, y), m_r (r) {} void draw (void) const { cout << "圆形:" << m_x << "," << m_y << "," << m_r << endl; }private: int m_r;};void render (Shape* shapes[]) { for (size_t i = 0; shapes[i]; i++) shapes[i] -> draw ();}int main (void) { size_t i = 0; Shape* shapes[1024] = {NULL}; shapes[i++] = new Rect (1, 2, 3, 4); shapes[i++] = new Rect (5, 6, 7, 8); shapes[i++] = new Circle (9, 10, 11); shapes[i++] = new Circle (12, 13, 14); shapes[i++] = new Rect (15, 16, 17, 18); render (shapes);// Shape shape (0, 0);//virtual 返回类型 函数名 (形参表) = 0;//的虚函数称为纯虚函数。//至少含有一个纯虚函数的类,称为抽象类。抽象类不能实例化对象。 return 0;}
下面是运行结果:
矩形:1,2,3,4
矩形:5,6,7,8
圆形:9,10,11
圆形:12,13,14
矩形:15,16,17,18
- C++【多态】和【覆盖】
- C++:重载和覆盖
- C++-函数覆盖和隐藏
- C++:重载、覆盖和隐藏
- 多态,重载和覆盖
- 【C#】——重载和重写、覆盖
- java(或C++)继承、重载、覆盖、多态
- 【C++】对象函数的覆盖、重载、多态
- 【C/C++】覆盖
- Objective-C 中方法的重载,覆盖和隐藏
- Objective-C 的方法原型及重载和覆盖
- Objective-C 的方法原型及重载和覆盖
- C++_重载、重写(覆盖)和隐藏的区别:
- 【C++】【学习笔记】【019】覆盖方法和重载方法
- C/C++中同名覆盖原则和赋值兼容原则
- 【c++】访问控制、覆盖方法和重载方法
- 判定覆盖和条件覆盖
- java 多态,和方法覆盖分析
- 图的最短路径(迪杰斯特拉和Floyd)
- tabbaritem选中时的图片设置
- redis 跨机房 连接失败导致一致链接不上
- 学习PHP到底要学习哪些东西?
- ORACLE故障处理
- C++【多态】和【覆盖】
- android 界面布局-各个布局的属性介绍
- 关于VS2015Winform程序连接acess2013数据库的问题
- papa的儿子 matlab plot3 color peaks
- 【程序1】看商品猜价格
- Spring笔记——Bean后处理器
- crsctl stat res -t 和crsctl stat res -t -init 中STATE_DETAILS的解释
- 自定义Label的行间距
- Core Data