1. 11 
  2. 20 
  3. 练习:简单的继承 
  4. 未分类 
  5. 编辑 | 删除 
  6. #include <iostream> 
  7. using namespace std; 
  8.  
  9. class ClsA { 
  10. public
  11.     void show() { 
  12.         cout << "this is A" << endl; 
  13.     } 
  14.     void doSthA() { 
  15.         cout << "do sth A" << endl; 
  16.     } 
  17. }; 
  18.  
  19. class ClsB : public ClsA { 
  20. public
  21.     void show() { 
  22.         cout << "this is B" << endl; 
  23.     } 
  24.  
  25.     void doSthB() { 
  26.         cout << "do sth B" << endl; 
  27.     } 
  28. }; 
  29.  
  30.  
  31. int main() { 
  32.     ClsA a; 
  33.     ClsB b; 
  34.     a.show(); 
  35.     a.doSthA(); 
  36.     // a.doSthB();  这行必须注释掉,因为a对象中没有定义doSthB这个方法 
  37.     b.show(); 
  38.     b.doSthA(); 
  39.     b.doSthB();