C++_练习—多态_纯虚函数与抽象类

纯虚函数与抽象类


 

含有纯虚函数的类,称为抽象基类,不可实列化。即不能创建对象,存在的意义就是被继承,提供族类的公共接口。

 

 1 // 纯虚函数与抽象类
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class shape {
 8 public:
 9     virtual double getarea() = 0;
10 };
11 
12 class cfx :public shape {
13 public:
14     cfx(int a, int b) {
15         this->a = a;
16         this->b = b;
17     }
18 
19     virtual double getarea() {
20         cout << "cfx =  " << a*b << endl;
21         return a * b ;
22     }
23 
24 private:
25     int a;
26     int b;
27 };
28 
29 class yx :public shape {
30 public:
31     yx(int r) {
32         this->r = r;
33     }
34 
35     virtual double getarea() {
36         cout << "yx =  " <<  3.14*r*r << endl;
37         return 3.14*r*r;
38     }
39 
40 private:
41     int r;
42 };
43 
44 int main(void)
45 {
46     shape *changfx = new cfx(2,3);
47     changfx->getarea();
48 
49     shape *yuan = new yx(1);
50     yuan->getarea();
51 
52     system("pause");
53 
54     return 0;
55 }

 

 

笔记


 

转载于:https://www.cnblogs.com/panda-w/p/11381811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值