Maplewing

12/12 C++: Inheritance

Dec 11th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Hello{
  5.     protected:
  6.         string name;
  7.        
  8.     public:
  9.         Hello(){
  10.             name = "NoName";
  11.         }
  12.        
  13.         Hello(string n){
  14.             name = n;
  15.         }
  16.        
  17.         void say(){
  18.             cout << "Hello, " << name << "!" << endl;
  19.         }
  20.        
  21.        
  22. };
  23.  
  24. class Hi : public Hello{
  25.     public:
  26.        
  27.     Hi(){
  28.     }
  29.    
  30.     Hi(string n):Hello(n){
  31.     }
  32.        
  33.        
  34.     void say(){
  35.         cout << "Hi, " << name << "!" << endl;
  36.     }
  37. };
  38.  
  39.  
  40.  
  41. int main(){
  42.     Hello a("Amy");
  43.     a.say();
  44.    
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment