覆盖与隐藏


 1 class A
 2 {
 3 public:
 4       A(int num);
 5       virtual ~A();
 6       int action(int info);
 7       int goaction();
 8       virtual int getlastValue();
 9 protected:
10       int generateValue();
11 private:
12       int value;
13       int flag;
14       int lastValue;
15 };
16 class B:public A
17 {
18 public:
19     B(int number):A(number){};
20     virtual  int action(int info);
21 protected:
22     int generateValue();
23 
24 };
 1 // ClassHide.cpp : 定义控制台应用程序的入口点。
 2 //覆盖,隐藏
 3 
 4 #include "stdafx.h"
 5 #include "ClassDefined.h"
 6 #include <iostream.h>
 7 //A类
 8 A::A(int num):flag(1)
 9 {
10    value = num;//value = 5
11 }
12 A::~A(){}
13 int A::action(int info)
14 {
15    flag+=info;//info = 6
16    return flag;//flag = 7
17 }
18 int A::goaction()
19 {
20    lastValue = generateValue();//12
21    return lastValue;
22 }
23 int A::getlastValue()
24 {
25   return this->goaction();//12
26 }
27 int A::generateValue()
28 {
29   return value+7;//12
30 }
31 //B类
32 int B::action(int info)
33 {
34    return info+10; 
35 }
36 int  B::generateValue()
37 {
38     return 7;
39 }
40 
41 
42 int _tmain(int argc, _TCHAR* argv[])
43 {
44     A a(5);
45     B b(6);
46     int m,n;
47     m = a.getlastValue();
48     cout<<"m = "<<m<<endl;//12
49     n = b.getlastValue();
50     cout<<"n = "<<n<<endl;//13
51     return 0;
52 }
优质内容筛选与推荐>>
1、【UOJ228】基础数据结构练习题(线段树)
2、python socket编程详细介绍
3、js中事件三阶段
4、反射与注解
5、python中is和==区别


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn