-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseclassshape.cpp
More file actions
69 lines (69 loc) · 1.3 KB
/
Copy pathbaseclassshape.cpp
File metadata and controls
69 lines (69 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Implement a base class shape with derived classes circle, rectangle, and triangle. use virtual functions to calculate the area of each shape:
#include <iostream>
#include <cmath>
using namespace std;
class Shape
{
public:
virtual void area()
{
cout << "These are Shapes." << endl;
}
};
class Circle : public Shape
{
public:
float radius;
Circle(float r)
{
radius = r;
}
void area() override
{
cout << "Area of circle is: " << M_PI * radius * radius << endl;
}
};
class Rectangle : public Shape
{
public:
float length;
float breadth;
Rectangle(float l, float b)
{
length = l;
breadth = b;
}
void area() override
{
cout << "Area of rectangle is: " << length * breadth << endl;
}
};
class Triangle : public Shape
{
public:
float base;
float height;
Triangle(float b, float h)
{
base = b;
height = h;
}
void area() override
{
cout << "Area of triangle is: " << 0.5 * base * height << endl;
}
};
int main()
{
Shape *s;
Circle c(6.8);
Rectangle r(7, 4);
Triangle t(7.4, 8.5);
s = &c;
s->area();
s = &r;
s->area();
s = &t;
s->area();
return 0;
}