Init member variables from base class : Base Class « Class « C++






Init member variables from base class

Init member variables from base class
  

#include <iostream>
using namespace std;

class BaseClass {
  int i, j;
public:
  BaseClass(int x, int y) { 
     i = x; 
     j = y; 
  }
  void showij() { 
  
     cout << i << ' ' << j << '\n'; 
  }
};

class DerivedClass : public BaseClass {
  int k;
public:
  DerivedClass(int a, int b, int c) : BaseClass(b, c) {
    k = a;
  }
  void show() { 
    cout << k << ' '; showij(); 
  }
};

int main()
{
  DerivedClass ob(1, 2, 3);

  ob.show();

  return 0;
}


           
         
    
  








Related examples in the same category

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