74 lines
1.1 KiB
C++
74 lines
1.1 KiB
C++
#include <iostream>
|
|
#include <math.h>
|
|
using namespace std;
|
|
|
|
class Shape {
|
|
int stub;
|
|
|
|
public:
|
|
virtual void getData() { cout << "stub"; }
|
|
|
|
virtual double area() {
|
|
cout << "stub";
|
|
return 0;
|
|
}
|
|
};
|
|
|
|
class Triangle : public Shape {
|
|
private:
|
|
double sides[3];
|
|
|
|
public:
|
|
void getData();
|
|
double area();
|
|
};
|
|
|
|
void Triangle::getData() {
|
|
cout << "Enter 3 integers for the sides of triangle: \n";
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
cin >> sides[i];
|
|
}
|
|
|
|
double Triangle::area() {
|
|
double s = (sides[0] + sides[1] + sides[2]) / 2;
|
|
|
|
return sqrt(s * (s - sides[0]) * (s - sides[1]) * (s - sides[2]));
|
|
}
|
|
|
|
class Rectangle : public Shape {
|
|
private:
|
|
double l;
|
|
double b;
|
|
|
|
public:
|
|
void getData();
|
|
double area();
|
|
};
|
|
|
|
void Rectangle::getData() {
|
|
cout << "Enter length of rectangle: ";
|
|
cin >> l;
|
|
cout << "Enter breadh of rectangle: ";
|
|
cin >> b;
|
|
}
|
|
|
|
double Rectangle::area() { return l * b; }
|
|
|
|
int main() {
|
|
Shape *s;
|
|
Triangle t;
|
|
Rectangle r;
|
|
|
|
s = &t;
|
|
|
|
s->getData();
|
|
cout << "Area of the Shape: " << s->area() << endl;
|
|
|
|
s = &r;
|
|
|
|
s->getData();
|
|
cout << "Area of the Shape: " << s->area() << endl;
|
|
|
|
}
|