014_linuxC++之_不同类型的继承

  1 #include <iostream>
  2 #include <string.h>
  3 #include <unistd.h>
  4 
  5 using namespace std;
  6 
  7 class Father {
  8 private:
  9     int money;
 10 
 11 protected:
 12     int room_key;
 13     
 14 public:
 15     int address;
 16     
 17     void it_skill(void)
 18     {
 19         cout<<"father's it skill"<<endl;
 20     }
 21 
 22     int getMoney(void)
 23     {
 24         return money;
 25     }
 26 
 27     void setMoney(int money)
 28     {
 29         this->money = money;
 30     }
 31 };
 32 
 33 class Son_pub : public Father {
 34 private:
 35     int toy;
 36 public:
 37     
 38     void play_game(void)
 39     {
 40         int m;
 41         
 42         cout<<"son play game"<<endl;
 43 
 44         /* money -= 1; 
 45          * 错: 不能直接拿父亲的私房钱
 46          */
 47 
 48         /*
 49          * 但是可以问他要
 50          */
 51         m = getMoney();
 52         m--;
 53         setMoney(m);
 54 
 55         room_key = 1; 
 56     }
 57 };
 58 
 59 
 60 class Son_pro : protected Father {
 61 private:
 62     int toy;
 63 public:
 64     
 65     void play_game(void)
 66     {
 67         int m;
 68         
 69         cout<<"son play game"<<endl;
 70 
 71         /* money -= 1; 
 72          * 错: 不能直接拿父亲的私房钱
 73          */
 74 
 75         /*
 76          * 但是可以问他要
 77          */
 78         m = getMoney();
 79         m--;
 80         setMoney(m);
 81 
 82         room_key = 1; 
 83     }
 84 };
 85 
 86 
 87 class Son_pri : private Father {
 88 private:
 89     int toy;
 90 public:
 91     
 92     void play_game(void)
 93     {
 94         int m;
 95         
 96         cout<<"son play game"<<endl;
 97 
 98         /* money -= 1; 
 99          * 错: 不能直接拿父亲的私房钱
100          */
101 
102         /*
103          * 但是可以问他要
104          */
105         m = getMoney();
106         m--;
107         setMoney(m);
108 
109         room_key = 1; 
110     }
111 };
112 
113 
114 int main(int argc, char **argv)
115 {
116     Son_pub s_pub;
117     Son_pro s_pro;
118     Son_pri s_pri;
119 
120     s_pub.play_game();
121     s_pro.play_game();
122     s_pri.play_game();
123 
124 
125     s_pub.it_skill();
126     //s_pro.it_skill();  // error
127     //s_pri.it_skill();     // error
128 
129     return 0;
130 }
View Code

 

原文地址:https://www.cnblogs.com/luxiaoguogege/p/9693798.html