多态性

多态性

进入本节之前,建议你有一个正确的理解指针和类继承。   如果下面的语句似乎怪你,你应该检讨指定的章节:  

声明:解释:
的INT A :: B(C){}
A-> B数据结构
A级:公众B {};友谊和继承

基类的指针

派生类的主要特点之一是其基类的指针到派生类的指针类型兼容。   多态性是艺术趁着这个简单但功能强大和灵活的功能,带来了面向对象的方法,以充分发挥其潜力。  

我们要开始的矩形和三角形上一节考虑这指针兼容性属性重写我们的程序:  

1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 
 // pointers to base class #include <iostream> using namespace std; class CPolygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; } }; class CRectangle: public CPolygon { public : int area () { return (width * height); } }; class CTriangle: public CPolygon { public : int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << rect.area() << endl; cout << trgl.area() << endl; return 0; } 
  20 
  10 


main函数中,我们创建了两个指向,以对象类CPolygon(ppoly1 ppoly2)的。   然后我们分配引用rectTRGL的这些指针,因为两者都是CPolygon的派生类的对象,都是有效的分配操作。  

唯一的限制在使用* ppoly1* ppoly2代替rectTRGL的是,这两个* ppoly1* ppoly2CPolygon的*类型,因此我们只能使用这些指针来引用, 类CRectangle和 CTriangle的继承CPolygon的成员。   出于这个原因,当我们调用的区域()的节目结束时,我们不得不直接使用的对象rectTRGL的 ,而不是指针* ppoly1* ppoly2成员。  

CPolygon的指针以使用面积(),则该成员应也已宣布在类CPolygon,不仅在它的派生类,但问题是, 类CRectangle和CTriangle的实施地区的不同版本,因此我们不能执行,在基类中。   这是虚拟成员时变得得心应手:  

虚拟成员

被称为虚拟成员的类的一个成员在​​派生类可以重新定义。   为了申报一类作为虚拟的一员,我们必须先声明virtual关键字:  

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 
 // virtual members #include <iostream> using namespace std; class CPolygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; } virtual int area () { return (0); } }; class CRectangle: public CPolygon { public : int area () { return (width * height); } }; class CTriangle: public CPolygon { public : int area () { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; CPolygon poly; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trgl; CPolygon * ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; cout << ppoly3->area() << endl; return 0; } 
  20 
  10 
  0 


现在三类( 圈交 , 类CRectangle和 CTriangle的),有相同的成员: 宽度 , 高度中, 函数set_values()面积()。

区域()成员函数被声明为虚基类中的,因为它是后来每个派生类中重新定义。   您可以验证,如果你想,如果你删除这个虚拟的关键字从申报区域()CPolygon的范围内 ,然后运行程序,结果将是0三个多边形,而不是20,100。   ,而不是调用相应区域()函数的每个对象(CRectangle的区域(),CTriangle ::面积()圈交::面积 ,这是因为(),分别),将被称为圈交::面积()所有情况下,因为这些调用是通过指针的类型是CPolygon的*。  

因此, 虚拟关键字的作用是允许一个派生类的成员具有相同的名称作为一个在基类中被适当地称为从一个指针,和更精确地,当不同的指针是一个指针到基类但指向派生的类的一个对象,如在上面的例子。  

声明或继承了虚函数的类称为多态类 。  

请注意,尽管它的虚拟性,我们也能够CPolygon类型来声明对象,并呼吁自己的领域()函数,它总是返回0。  

抽象基类

抽象基类的东西非常类似我们前面的例子我们CPolygon的类。   唯一的区别是,在我们前面的例子中,我们定义的有效面积()函数的的类CPolygon(如对象 )为对象,用最少的功能,而在一个抽象基类,我们可以离开那个区域()成员在所有没有实现的功能。   这是通过附加到函数声明= 0(等于零)。  

一个抽象基CPolygon的类可能看起来像这样:  

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 
 // abstract class CPolygon class CPolygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; } virtual int area () =0; }; 


注意我们如何附加= 0虚拟的int区(),而不是指定一个实现的功能。   这种类型的函数称为纯虚函数 ,所有至少包含一个纯虚函数的类是抽象基类 。  

一个抽象基类和普通的多态类之间的主要区别是,因为至少有一个抽象基类,其成员缺乏实现,我们不能创建它的实例(对象)。  

但也不是完全无用的一类,不能实例化对象。   我们可以创建指向它的指针,并充分利用其所有的多态能力。   因此,声明如下:  

 
 CPolygon poly; 


不会有效的抽象基类,我们刚刚宣布,因为试图实例化一个对象。   然而,下面的指针:  

 1 
 2 
 
 CPolygon * ppoly1; CPolygon * ppoly2; 


将是完全有效的。  

之所以如此,是CPolygon的 ,只要有一个纯虚函数,因此它是一个抽象基类。   然而,可以使用这个抽象基类的指针指向派生类的对象。  

在这里,你有完整的例子:  

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 
 // abstract base class #include <iostream> using namespace std; class CPolygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; } virtual int area ( void ) =0; }; class CRectangle: public CPolygon { public : int area ( void ) { return (width * height); } }; class CTriangle: public CPolygon { public : int area ( void ) { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); cout << ppoly1->area() << endl; cout << ppoly2->area() << endl; return 0; } 
  20 
  10 


如果您查看程序,你会发现,我们称之为不同的,但相关的类的对象使用一个独特的类型的指针( 圈交*)。   这可以是相当有用的。   例如,现在我们可以创建一个函数成员的抽象基类CPolygon即能在屏幕上打印的区域()的结果CPolygon的本身功能,即使没有实现这个功能:  

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 
 // pure virtual members can be called // from the abstract base class #include <iostream> using namespace std; class CPolygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; } virtual int area ( void ) =0; void printarea ( void ) { cout << this ->area() << endl; } }; class CRectangle: public CPolygon { public : int area ( void ) { return (width * height); } }; class CTriangle: public CPolygon { public : int area ( void ) { return (width * height / 2); } }; int main () { CRectangle rect; CTriangle trgl; CPolygon * ppoly1 = &rect; CPolygon * ppoly2 = &trgl; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly1->printarea(); ppoly2->printarea(); return 0; } 
  20 
  10 


虚拟成员和抽象类出让C + +的多态性面向对象编程的特点,使大项目中的一个有用的工具。   当然,我们已经看到了非常简单的使用这些功能,但这些功能都可以应用到对象的数组或动态分配的对象。  

让我们再次结束,同样的例子,但这次与动态分配的对象:  

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 
 // dynamic allocation and polymorphism #include <iostream> using namespace std; class CPolygon { protected : int width, height; public : void set_values ( int a, int b) { width=a; height=b; } virtual int area ( void ) =0; void printarea ( void ) { cout << this ->area() << endl; } }; class CRectangle: public CPolygon { public : int area ( void ) { return (width * height); } }; class CTriangle: public CPolygon { public : int area ( void ) { return (width * height / 2); } }; int main () { CPolygon * ppoly1 = new CRectangle; CPolygon * ppoly2 = new CTriangle; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly1->printarea(); ppoly2->printarea(); delete ppoly1; delete ppoly2; return 0; } 
  20 
  10 


请注意,的ppoly指针:  

 1 
 2 
 
 CPolygon * ppoly1 = new CRectangle; CPolygon * ppoly2 = new CTriangle; 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值