-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_extend_01.cpp
84 lines (72 loc) · 2.02 KB
/
02_extend_01.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// // Extend the above program to display the area of circles. This requires the addition of a new derived class 'Circle' that computes the area of a circle. Remember, for a circle we need only one value, its radius, but the setData() function in the base class requires two values to be passed. (Hint: Make the second argument of setData() function as a default one with zero value.)
// // Header files
#include <iostream>
// // use namespace
using namespace std;
// // define class Shape
class Shape
{
protected:
// // instance member variables
double d1, d2;
public:
// // instance member function to set data
void setData(double d1, double d2 = 0)
{
this->d1 = d1;
this->d2 = d2;
}
// // virtual instance member function to display the area
virtual void displayArea() const
{
cout << "\nNot Implemented this for class Shape...";
}
};
// // define class Triangle by inheriting class Shape
class Triangle : public Shape
{
public:
// // override base class function displayArea
void displayArea() const override
{
cout << "\nArea of Triangle => " << 0.5 * d1 * d2;
}
};
// // define class Rectangle by inheriting class Shape
class Rectangle : public Shape
{
public:
// // override base class function displayArea
void displayArea() const override
{
cout << "\nArea of Rectangle => " << d1 * d2;
}
};
// // define class Circle by inheriting class Shape
class Circle : public Shape
{
public:
// // override base class function displayArea
void displayArea() const override
{
cout << "\nArea of Circle => " << 3.14159 * d1 * d1;
}
};
int main()
{
// // create an instance of Triangle
Triangle t1;
t1.setData(4, 3);
t1.displayArea();
// // create an instance of Rectangle
Rectangle r1;
r1.setData(4, 5);
r1.displayArea();
// // create an instance of Circle
Circle c1;
c1.setData(7.5);
c1.displayArea();
cout << endl; // Add new line
cin.ignore();
return 0;
}