C++ Program to find area perimeter of a rectangle using virtual function


In this program, You will learn how to find area perimeter of a rectangle using virtual function in C++.


virtual void calculate() { }


Example: How to find area perimeter of a rectangle using virtual function in C++

Run it Now
#include<iostream>
using namespace std;

class First {
public:

   virtual void calculate() {
       cout << "Area Perimeter of a Rectangle";
   }
};

class Second : public First {
public:

   int width, height, area, perimeter;

   void calculate() {
       cout << "Enter  Width of Rectangle :";
       cin>>width;

       cout << "\nEnter  Height of Rectangle :";
       cin>>height;

       area = height*width;
       cout << "\nArea of Rectangle :" << area;

       perimeter = 2 * (height + width);
       cout << "\nPerimeter of Rectangle are :" << perimeter;
   }
};

int main() {

   First *f;
   Second s;
   f = &s;
   f->calculate();

   return 0;
}

Output:

Enter  Width of Rectangle :4
Enter  Height of Rectangle :3
Area of Rectangle :12
Perimeter of Rectangle are :14