Using the concept of Inheritance write a C++ Program to calculate the area and perimeter of rectangle



Sumit Kar, Timus Rak,CPP



/* C++ Program to calculate the area and perimeter of rectangles using concept of inheritance. */

#include 
using namespace std;
class Rectangle
{
    protected:
       float length, breadth;
    public:
        Rectangle(): length(0.0), breadth(0.0)
        {
            cout<<"Enter length: ";
            cin>>length;
            cout<<"Enter breadth: ";
            cin>>breadth;
        }

};


/* Area class is derived from base class Rectangle. */

class Area : public Rectangle   
{
    public:
       float calc()
         {
             return length*breadth;
         }

};


/* Perimeter class is derived from base class Rectangle. */

class Perimeter : public Rectangle
{
    public:
       float calc()
         {
             return 2*(length+breadth);
         }
};

int main()
{
     cout<<"Enter data for first rectangle to find area.\n";
     Area a;
     cout<<"Area = "<

Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C