Access member of base and use member of derived class : derived class « Class « C++ Tutorial






#include <iostream>
using namespace std;

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

class derived : public base {
  int k;
public:
  derived(int x) { 
     k=x; 
  }
  void showk() { 
     cout << k << "\n"; 
  }
};

int main()
{
  derived ob(3);

  ob.set(1, 2);
  ob.show();

  ob.showk();  

  return 0;
}
1 2
3








9.18.derived class
9.18.1.Add a constructor to derived class
9.18.2.Call base's constructor and destructor from derived class
9.18.3.Access member of base and use member of derived class
9.18.4.Overriding a base class method in a derived class
9.18.5.Hiding methods
9.18.6.Calling base method from overridden method.
9.18.7.Using virtual methods
9.18.8.Inherit base class twice
9.18.9.dynamic_cast: Cast from Derived * to Base *