对于C++多态性的认识

 

1.说在前面:

项目大体上解决了,现在可以腾出时间来优化项目和学习新的知识

2.C++多态性

1.简述:(多态)polymorphism

       对于C++的多态性,这是一项很灵活的技术,用法十分灵巧,有难度;简单来说:多态性就是适当的使用接口函数,通过一个接口来使用多种方法,(相当于上级说一个命令,A,B,C,D等人都做出反应,一个命令,多个反应;

2.怎样实现多态性

1.采用虚函数和指针来实现多态性

通过虚函数来定义要实现的方法,通过基类的指针指向不同的子类的虚函数方法

2.多态性的优点是实现接口的重用

3.虚函数:

1.虚函数采用关键字virtual修饰,虚函数允许子类重新去定义成员函数;

2.重写(函数名一致,但是内容不一致)//又称覆盖(override)

3.重载(函数名一致,但是参数表列不一致)

4.多态性一般就是通过对虚函数进行重写,然后用基类指针指向不同的子对象,调用不同的虚函数

4.代码实现

[cpp]  view plain  copy
 
  1. #include<iostream>  
  2. #include<stdlib.h>  
  3. #include<string>  
  4. using namespace std;  
  5. class Ishape  
  6. {  
  7. public:  
  8.     //多态性实现需要虚方法  
  9.     virtual double get_Area() = 0;  
  10.     virtual string get_Name() = 0;  
  11. };  
  12. //定义类1 CCircle  
  13. class CCircle :public Ishape  
  14. {  
  15.     //声明的时候需要带上Virtual关键字 显示多态性  
  16. public:  
  17.     CCircle(double radius) {this->c_radius = radius;}  
  18.     CCircle(){}  
  19.     virtual double get_Area();  
  20.     virtual string get_Name();  
  21. private:  
  22.     double c_radius;//定义圆的半径  
  23.   
  24. };  
  25. //定义方法  
  26.   
  27. double CCircle::get_Area()  
  28. {  
  29.     return 3.14*c_radius*c_radius;  
  30. }  
  31. string CCircle::get_Name()  
  32. {  
  33.     return "CCircle";  
  34. }  
  35. class CRect :public Ishape  
  36. {  
  37. public:  
  38.     CRect(double length, double width) { this->m_length = length, this->m_width = width;}  
  39.     CRect(){};  
  40.     virtual double get_Area();  
  41.     virtual string get_Name();  
  42. private:  
  43.     double m_length;  
  44.     double m_width;  
  45. };  
  46. double CRect::get_Area()  
  47. {  
  48.     return m_length*m_width;  
  49. }  
  50. string CRect::get_Name()  
  51. {  
  52.     return  "Rectangle";  
  53. }  
  54.   
  55. //通过指针指向不同的类从而使用不同的方法  
  56. #include"text.h"  
  57.   
  58.   
  59. void main()  
  60. {  
  61. <span style="white-space:pre;"> </span>Ishape *point = NULL;//建立指针  
  62. <span style="white-space:pre;"> </span>point = new CCircle(10);  
  63. <span style="white-space:pre;"> </span>//圆类  
  64.   
  65.   
  66. <span style="white-space:pre;"> </span>cout << point->get_Name() << "的面积是:" << point->get_Area() << endl;  
  67. <span style="white-space:pre;"> </span>delete point;  
  68. <span style="white-space:pre;"> </span>//矩形类  
  69. <span style="white-space:pre;"> </span>point = new CRect(10, 20);  
  70. <span style="white-space:pre;"> </span>cout << point->get_Name() << "的面积是:" << point->get_Area() << endl;  
  71. <span style="white-space:pre;"> </span>delete point;  
  72. <span style="white-space:pre;"> </span>system("pause");  
  73. }  
  74.   
  75.   
  76.   
  77.   
  78.   
  79.   
  80.   
  81.   
  82. <span style="white-space:pre;"> </span>  
  83.   
  84.   
  85.   
  86.   
  87. <span style="white-space:pre;"> </span>  
  88.   
  89.   
  90. <span style="white-space:pre;"> </span>  
  91. <span style="white-space:pre;"> </span>  

猜你喜欢

转载自www.cnblogs.com/h12l4/p/9038501.html