1.虚函数:
不同对象面对相同的操作会体现出各自不同的地方
静态多态:早绑定,名字相同,参数不同,函数重载;同一个对象接受不同的操作
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Class Rect{ 2 3 public: 4 5 int calArea(int width); 6 7 int calArea(int width,int height); 8 9 }
动态多态:晚绑定,以封装和继承为基础,;不同对象下达相同的操作,产生不同的实现;如何体现出这个多态
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 class Shape{ 2 public: 3 double calcArea(){ 4 cout<<"calcArea"<<endl; 5 } 6 }; 7 8 //圆类,继承了Shape类 9 class Circle :public Shape{ 10 public: 11 Circle(double r); 12 double calcArea(); 13 private: 14 double m_dR; 15 }; 16 double Circle::calcArea(){ 17 return 3.14*m_dR*m_dR; 18 } 19 20 //矩形类,继承了Shape类 21 class Rect : public Shape{ 22 public: 23 Rect(double width,double height); 24 double calcArea(); 25 private: 26 m_dWidth; 27 m_dHeight; 28 }; 29 double Rect:: caclArea(){ 30 return m_dWidth* m_dHeight; 31 } 32 33 int main(void){ 34 Shape *shape1 = new Circle(4.0);//打印的是calcArea 35 Shape *shape2 = new Rect(3.0,5.0);//打印的是calcArea 36 ... 37 return 0; 38 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 class Shape{ 2 public: 3 Shape(); 4 virtual double calcArea();//实现多态的函数定义为virtual函数 5 ~Shape(); 6 }; 7 Shape::Shape(){ 8 cout<<"Shape()"<<endl; 9 } 10 Shape::~Shape(){ 11 cout<<"~Shape()"<<endl; 12 } 13 double Shape:: calcArea(){ 14 cout<< "Shape->calcArea"<<endl; 15 } 16 17 class Circle:public Shape{ 18 public: 19 Circle(double r); 20 virtual double calcArea();//可加可不加,最好加 21 ~Circle(); 22 protected: 23 double m_radius; 24 }; 25 Circle::Circle(double r){ 26 m_radius = r; 27 cout<<"Circle"<<endl; 28 } 29 Circle::~Circle(){ 30 cout<<"~Circle"<<endl; 31 } 32 double Circle::calcArea(){ 33 cout<<"Circle->calcArea()"<<endl; 34 return 3.14*m_radius*m_radius; 35 } 36 37 class Rect:public Shape{ 38 public: 39 Rect(double width,double height); 40 ~Rect(); 41 virtual double calcArea();//可加可不加,最好加 42 protected: 43 double m_dWidth; 44 double m_dHeight; 45 }; 46 Rect::Rect(double width,double height){ 47 m_dWidth=width; 48 m_dHeight=height; 49 cout<<"Rect()"<<endl; 50 } 51 Rect::~Rect(){ 52 cout<<"~Rect()"<<endl; 53 } 54 double Rect::calcArea(){ 55 cout<<"Rect->calcArea()"<<endl; 56 return m_dWidth*m_dHeight; 57 } 58 59 60 //使用,注意和上面的代码作对比 61 #include<iostream> 62 #include<stdlib.h> 63 #include"Shape.h" 64 #include"Circle.h" 65 #include"Rect.h" 66 using namespace std; 67 68 int main() 69 { 70 Shape *p1 = new Circle(2); 71 Shape *p2 = new Rect(2,3); 72 73 p1->calcArea();//调用的应该是Circle的函数 74 p2->calcArea();//调用的应该是Rect的函数 75 76 delete p1; 77 p1 = NULL; 78 delete p2; 79 p2 = NULL; 80 //Worker *p = new Worker(); 81 //delete p; 82 // p = NULL; 83 // system("pause"); 84 return 0; 85 } 86 87 /*输出结果 88 Shape() 89 Circle 90 Shape() 91 Rect() 92 Circle->calcArea() 93 Rect->calcArea() 94 ~Shape() 95 ~Shape() 96 */