Sunday, June 1, 2014

C++ Program to calculate area using inheritance

#include <iostream>
#include<conio.h>
using namespace std;

// Base class
class Sample
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Sample
{
   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;

getch();
   return 0;
}



No comments:

Post a Comment