0% found this document useful (0 votes)
15 views2 pages

Question 3

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

Question 3

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

#include <iostream>

#include <cmath>

using namespace std;

class Shape {
public:
virtual float calculate_Area() = 0;
virtual float calculate_Perimeter() = 0;
};

class Square : public Shape {


private:
float side;
public:
Square(float s) {
side = s;
}
float calculate_Area() {
return side * side;
}
float calculate_Perimeter() {
return 4 * side;
}
};

class Rectangle : public Shape {


private:
float length;
float width;
public:
Rectangle(float l, float w) {
length = l;
width = w;
}
float calculate_Area() {
return length * width;
}
float calculate_Perimeter() {
return 2 * (length + width);
}
};

class Triangle : public Shape {


private:
float a;
float b;
float c;
public:
Triangle(float x, float y, float z) {
a = x;
b = y;
c = z;
}
float calculate_Area() {
float s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
float calculate_Perimeter() {
return a + b + c;
}
};

int main() {
Shape *s;
Square sq(4);
Rectangle rec(4, 6);
Triangle tri(3, 4, 5);

s = &sq;
cout << "Area of square: " << s->calculate_Area() << endl;
cout << "Perimeter of square: " << s->calculate_Perimeter() << endl;

s = &rec;
cout << "Area of rectangle: " << s->calculate_Area() << endl;
cout << "Perimeter of rectangle: " << s->calculate_Perimeter() << endl;

s = &tri;
cout << "Area of triangle: " << s->calculate_Area() << endl;
cout << "Perimeter of triangle: " << s->calculate_Perimeter() << endl;

return 0;
}

You might also like