0% found this document useful (0 votes)
12 views4 pages

Virtual Function

c++ program

Uploaded by

priya magesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Virtual Function

c++ program

Uploaded by

priya magesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

VIRTUAL FUNCTION

#include <iostream>
#include <cmath>
using namespace std;
class SHAPE
{
protected:
float area;
float perimeter;
public:
virtual void calculate_area() = 0;
virtual void calculate_perimeter() = 0;
virtual void getvalue() = 0;
void display()
{
getvalue();
calculate_area();
calculate_perimeter();
cout << "\nArea = " << area;
cout << "\nPerimeter = " << perimeter << endl;
}
};
class Square : public SHAPE
{
private:
float side;
public:
void getvalue()
{
cout << "\nSquare";
cout << "\nEnter value for side: ";
cin >> side;
}
void calculate_area()
{
area = side * side;
}
void calculate_perimeter()
{
perimeter = 4 * side;
}
};
class Rectangle : public SHAPE
{
private:
float width, height;
public:
void getvalue()
{
cout << "\nRectangle";
cout << "\nEnter the value for width: ";
cin >> width;
cout << "\nEnter the value for height: ";
cin >> height;
}
void calculate_area()
{
area = width * height;
}
void calculate_perimeter()
{
perimeter = 2 * (width + height);
}
};
class Triangle : public SHAPE
{
private:
float sideA, sideB, sideC;
public:
void getvalue()
{
cout << "\nTriangle";
cout << "\nEnter the value for side A: ";
cin >> sideA;
cout << "\nEnter the value for side B: ";
cin >> sideB;
cout << "\nEnter the value for side C: ";
cin >> sideC;
}
void calculate_area()
{
float s = (sideA + sideB + sideC) / 2.0;
float t = s * (s - sideA) * (s - sideB) * (s - sideC);
area = sqrt(t);
}
void calculate_perimeter()
{
perimeter = sideA + sideB + sideC;
}
};
int main()
{
Square s;
s.display();
Rectangle r;
r.display();
Triangle t;
t.display();
return 0;
}

You might also like