Base class and derived class : Base Class « Class « C++






Base class and derived class

Base class and derived class
  
#include <iostream>
using namespace std;

class BaseClass {
  int i, j;
public:
  void set(int a, int b) { 
     i = a; 
     j = b; 
  }
  void show() { 
     cout << i << " " << j << endl; 
  }
};

class DerivedClass : public BaseClass {
  int k;
public:
  DerivedClass(int x) { 
     k = x; 
  }
  void showk() { 
     cout << k << endl; 
  }
};

int main()
{
  DerivedClass ob(3);

  ob.set(1, 2); // access member of BaseClass
  ob.show();    // access member of BaseClass

  ob.showk();   // uses member of DerivedClass class

  return 0;
}


           
         
    
  








Related examples in the same category

1.Using base pointers on DerivedClass class objects.Using base pointers on DerivedClass class objects.
2.Pass arguments to base classPass arguments to base class
3.Init member variables from base classInit member variables from base class
4.class to represent a book
5.Use Base keyword to call method in parent class from subclass