C++多态
2015-09-10 10:52
204 查看
Polymorphism(多态)
Upcast:take an object of the derived class as an object of the base one.
-Ellipse can be treated as a Shape
Dynamic binding(动态绑定)
-Binding:which function to be called
-Static binding: call the function as the code(静态绑定,编译的时已经确定,)
-Dynamic binding: call the function of the object(动态绑定,运行时根据指针所指向的对象来确定call哪个函数k,跟virtual render()有关系)
Upcast:take an object of the derived class as an object of the base one.
-Ellipse can be treated as a Shape
Dynamic binding(动态绑定)
-Binding:which function to be called
-Static binding: call the function as the code(静态绑定,编译的时已经确定,)
-Dynamic binding: call the function of the object(动态绑定,运行时根据指针所指向的对象来确定call哪个函数k,跟virtual render()有关系)
class XYPos{};//xy,point class Shape { public: Shape(); virtual ~Shape();//虚函数,子类 和 父类的函数有关系 virtual void render(); void move(const XYPos &); virtual void resize(); protected: XYPos center; }; class Ellipse : public Shape { public: Ellipse(float maj, float minr); virtual void render();//will define own protected: float major_axis, minor_axis; }; class Circle : public Ellipse { public: Circle(float radius) : Ellipse(radius, radius){}; virtual void render(); }; void render(Shape* p) { p->render();//根据给定的形状调用正确的render函数; } void func() { Ellipse ell(10,20); ell.render(); circle circ(40); circ.render(); render(&ell); render(&circ); }
相关文章推荐
- C++11 并发指南五(std::condition_variable 详解)
- C语言|&|| &&
- C++ 基础知识回顾(I/O)
- 浅析C++中的static
- 浅析C++中的static
- c++文件操作:2深入
- C语言中的越界问题解析
- windows平台 c++ 线程编程浅析
- 黑马程序员——ios开发基础之C语言概述
- c++ stl之关联式容器 set
- java 与 C/C++ 高字节 低字节的区别
- C++成员函数存储方式
- GMM混合高斯模型的C++代码(一)
- Effective C++读书笔记版-条款03、04
- C语言面试题大汇总之华为面试题
- 在VS2010下,用C语言编写pthread多线程程序的配置
- 已知两圆圆心坐标及半径求两圆交点 (C语言|参数方程求解)
- iOS开发之SQLite-C语言接口规范(一)——Ready And Open Your SQLite
- C++重载流插入运算符和流提取运算符
- C++builder XE 安装控件