• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一蓑烟雨
C/C++,Linux,语音技术
博客园    首页    新随笔    联系   管理    订阅  订阅
覆盖与隐藏
 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 }
posted on 2012-10-23 12:25  lovemu  阅读(239)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3