INHERITANCE


                     PREVIOUS                                                                                                          NEXT

Example:-Consider a base class Shape and its derived class Rectangle as follows:

#include < iostream.h > 
using namespace std;
	// Base class 
class Shape
{
   public: 
      void setWidth(int w) 
      { 
         width = w; 
      } 
      void setHeight(int h) 
      { 
         height = h; 
      } 
   protected: 
      int width; 
      int height; 
};
	// Derived class 
class Rectangle: public Shape 
{ 
   public: 
      int getArea() 
      {  
         return (width * height);  
      } 
}; 

int main(void) 
{ 
   Rectangle Rect; 
   Rect.setWidth(5); 
   Rect.setHeight(7); 
     // Print the area of the object. 
   cout << "Total area: " << Rect.getArea() << endl; 
   return 0; 
} 

When the above code is compiled and executed, it produces following result:
Total area: 35