A base pointer to access derived objects : Derived « Class « C++






A base pointer to access derived objects

A base pointer to access derived objects
  

#include <iostream>
using namespace std;
class BaseClass {
  int i;
public:
  void setInt(int num) { 
    i = num; 
  }
  int getInt() { 
    return i; 
  }
};
class derived: public BaseClass {
  int j;
public:
  void setJ(int num) { 
     j = num; 
  }
  int getJ() { 
     return j; 
  }
};
int main()
{
  BaseClass *bp;
  derived d;
  bp = &d;                   // BaseClass pointer points to derived object
                             // access derived object using BaseClass pointer
  bp->setInt(10);
  cout << bp->getInt() << " ";

  return 0;
}

           
         
    
  








Related examples in the same category

1.Demonstrate pointer to derived class.Demonstrate pointer to derived class.
2.Simple class with its derived classSimple class with its 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