c++入门(多态and纯虚函数and抽象类)

来源:互联网 发布:信达通证券软件下载 编辑:程序博客网 时间:2024/06/07 00:13

文件名:<1> shape.h ;<2> circle.h ;<3> circle.cpp ;<4> rectangular.h ;<5> rectangular.cpp ;<6> main.cpp

1、多态

是面向对象程序设计的关键技术。

调用同一个函数名,可以根据需要实现不同的功能。

编译时的多态性(函数重载);运行时的多态性(虚函数)。

2、纯虚函数

在函数基类中声明,在派生类中实现。

virtual关键字;声明时须在后面加上 =0;派生类中实现时无须写virtual。

3、抽象类

含有纯虚函数的类。

在派生类实现该纯虚函数后,定义抽象类对象的指针,并指向引用的子类对象


注:

可以把析构函数定义为虚函数,但不能将构造函数定义为虚函数。

因为定义为虚函数的构造函数只是声明了而没有实现,没有初始化,不能被继承。

每个类都要有自己的构造函数,对自己进行初始化。


<1> shape.h


  1 //文件名 shape.h  2   3 #ifndef SHAPE_H //防止重复包含的宏开关  4 #define SHAPE_H  5   6 #include <string>  7   8 /* virtual在基类中声明纯虚函数,其实现在派生类中  */  9 class i_Shape 10 { 11 public: 12         virtual float getArea() = 0;    //纯虚函数的声明:virtual关键字,声明之后写上 =0 13         virtual std::string getName() = 0; 14  15 }; 16  17 #endif

<2> circle.h


  1 //文件名 circle.h  2   3 #ifndef CIRCLE_H  4 #define CIRCLE_H  5   6 #include "shape.h"  7   8 class c_Circle : public i_Shape //继承i_Shape  9 { 10 public: 11         c_Circle(float radius); //含参数的构造函数 12 public: 13         virtual float getArea();        //抽象类的虚函数。此处需要关键字virtual,但不需要在后面加上 =0 14         virtual std::string getName(); 15 private: 16         float m_Radius; 17 }; 18  19 #endif

<3> circlr.cpp


  1 //文件名 circle.cpp  2   3 #include "circle.h"  4   5 c_Circle::c_Circle(float radius)        //含参数的虚构函数  6         :m_Radius(radius)       //初始化列表  7 {  8   9 } 10  11 float c_Circle::getArea()       //虚函数的实现。此处无须关键字virtual 12 { 13         return 3.14 * m_Radius * m_Radius; 14 } 15  16 std::string c_Circle::getName()         //虚函数的实现。此处无须关键字virtual 17 { 18         return "circle 圆形的"; 19 }

<4> rectangular.h


  1 //文件名 rectangualr.h  2   3 #ifndef RECTANGULAR_H   //防止重复包含的宏开关  4 #define RECTANGULAR_H  5   6 #include "shape.h"  7   8 class c_Rect : public i_Shape   //继承 i_Shape接口 interface  9 { 10 public: 11         c_Rect();       //不含参数的默认构造函数 12         ~c_Rect();      //析构函数 13         c_Rect(float width,float length);       //含参数的构造函数 14 public: 15         virtual float getArea();        //virtual虚函数。。此处无须写 =0 16         virtual std::string getName(); 17 private: 18         float m_Width;  //private成员 19         float m_Length; 20  21 }; 22  23 #endif


<5> rectangular.cpp


  1 //文件名 rectangular.cpp  2   3 #include "rectangular.h"  4   5 c_Rect::c_Rect()        //不含参数的默认构造函数  6 {  7   8 }  9  10 c_Rect::~c_Rect()       //析构函数 11 { 12  13 } 14  15 c_Rect::c_Rect(float width,float length)        //含参数的构造函数 16         :m_Width(width),m_Length(length)        //初始化列表:以冒号开头,以逗号分隔 17 { 18  19 } 20  21 float c_Rect::getArea() //虚函数的实现 22 { 23         return m_Width * m_Length; 24 } 25  26 std::string c_Rect::getName()   //虚函数的实现 27 { 28         return "Rectangular 方形的"; 29 }

<6> main.cpp


  1 //文件名 main.cpp  2 //  3 #include "rectangular.h"  4 #include "circle.h"  5   6 #include <iostream>  7   8 int main()  9 { 10         /* 定义抽象类对象i_Shape的指针p,并指向引用的子类对象c_Circle  */ 11         i_Shape* p = new c_Circle(10); 12         std::cout<< p -> getName() << "  " << p -> getArea() <<std::endl; 13         delete p; 14  15         /* 定义抽象类对象i_Shape的指针p,并指向引用的子类对象c_Ret  */ 16         p = new c_Rect(10,20); 17         std::cout<< p -> getName() << "  " << p -> getArea() <<std::endl; 18  19         return 0; 20 }

编译、链接、执行




原创粉丝点击