Maplewing

4/17 C++: Inheritance

Apr 16th, 2016
54
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 sayHello(){
  18.             cout << "Hello, " << name << "!" << endl;
  19.         }
  20. };
  21.  
  22. class Hi : public Hello{
  23.     public:
  24.         Hi(){
  25.            
  26.         }
  27.        
  28.         Hi(string n) : Hello(n){
  29.            
  30.         }
  31.        
  32.         void sayHi(){
  33.             cout << "Hi, " << name << "!" << endl;
  34.         }
  35. };
  36.  
  37. int main(){
  38.     Hi h("Sonic");
  39.     h.sayHi();
  40.    
  41.     return 0;
  42. }
Add Comment
Please, Sign In to add comment