From regines, 1 Year ago, written in Plain Text.
Embed
  1. #ifndef INHERITANCE_H_
  2. #define INHERITANCE_H_
  3. #include <iostream>
  4. using std::cout;
  5.  
  6. struct plant {
  7.         plant(){ cout << "a plant sprouts\n"; }
  8.         ~plant(){ cout << "plant withers\n"; }
  9.         void stem(){ cout << "none\n"; }
  10.         virtual void leaf(){ cout << "some green\n";}
  11. };
  12.  
  13. struct tree: plant {
  14.         tree(){ cout << "a tree grows\n";}
  15.         ~tree() { cout << "a tree fell\n";}
  16.         void stem(){ bark();}
  17.         virtual void bark(){ cout << "rough bark\n";}
  18. };
  19.  
  20. struct birch: tree { // Birke
  21.         birch():tree{}{ cout << "a beautiful birch\n";}
  22.         ~birch(){ cout << "an old birch collapses\n";}
  23.         virtual void bark(){ cout << "paper white\n";}
  24.         void leaf(){ cout << "small green leaf\n";}
  25. };
  26.  
  27. #endif /* INHERITANCE_H_ */
  28.  
  29. int main(){
  30.         cout << "a\n";
  31.         birch b;
  32.         tree &t=b;
  33.         plant p{t};
  34.        
  35.         cout << "b\n";
  36.         b.leaf();
  37.         t.leaf();
  38.         p.leaf();
  39.        
  40.         cout << "c\n";
  41.         b.stem();
  42.         t.stem();
  43.         p.stem();
  44.        
  45.         cout << "d\n";
  46.         b.bark();
  47.         t.bark();
  48.        
  49.         cout << "end \n";
  50.  }
  51.