Simple class with its derived class : Derived « Class « C++






Simple class with its derived class

Simple class with its derived class
  

#include <iostream>
#include <cstring>
using namespace std;

class mybase {
  char str[80];
public:
  mybase(char *s) { 
     strcpy(str, s); 
  }
  char *get() { 
     return str; 
  }
};

class myderived : public mybase {
  int len;
public:
  myderived(char *s) : mybase(s) {
    len = strlen(s);
  }
  int getlen() { 
    return len; 
  }
  void show() { 
    cout << get() << '\n'; 
  }
};

int main()
{
  myderived ob("hello");

  ob.show();
  cout << ob.getlen() << '\n';
 
  return 0;
}


           
         
    
  








Related examples in the same category

1.A base pointer to access derived objectsA base pointer to access derived objects
2.Demonstrate pointer to derived class.Demonstrate pointer to derived class.
3.call constructor from parent for three level extending
4.Using pointers on derived class objects.
5.how to pass arguments to the base classes of a derived class by modifying the preceding program