Pass arguments to base class : Base Class « Class « C++






Pass arguments to base class

Pass arguments to base class
  
#include <iostream>
using namespace std;

class BaseClass {
  int i;
public:
  BaseClass(int n) { 
    cout << "Constructing base class\n"; 
    i = n;
  }
  ~BaseClass() { 
    cout << "Destructing base class\n"; 
  }
  void showi() { 
    cout << i << '\n'; 
  }
};

class DerivedClass : public BaseClass {
  int j;
public:
  DerivedClass(int n, int m) : BaseClass(m) { 
    cout << "Constructing DerivedClass class\n"; 
    j = n;
  } 
  ~DerivedClass() { 
    cout << "Destructing DerivedClass class\n"; 
  }
  void showj() { 

    cout << j << '\n'; 
  }
};

int main()
{
  DerivedClass o(10, 20);

  o.showi();
  o.showj();
  
  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.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