-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInheritance_polymorphism.cpp
More file actions
133 lines (125 loc) · 2.26 KB
/
Inheritance_polymorphism.cpp
File metadata and controls
133 lines (125 loc) · 2.26 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
using namespace std;
class shape
{
protected:
float length;
public:
shape()
{
length = 0;
cout << "This is the base constructor!" << endl;
}
virtual float area()
{
cout << "area fucntion in base" << endl;
return 1;
}
virtual float parameter()
{
cout << "parameter fucntion in base" << endl;
return 1;
}
~shape()
{
cout << "This is the base destructor!" << endl;
}
};
class rectangle : public shape
{
protected:
float width;
public:
rectangle()
{
width = 0;
cout << "This is the derived class constructor!" << endl;
}
rectangle(float x, float y)
{
cout << "overloaded constructor of rectangle:" << endl;
width = x;
length = y;
}
float area()
{
return (width * length);
}
float parameter()
{
return (2 * (length + width));
}
~rectangle()
{
cout << "This is the derived class destructor!" << endl;
}
};
class triangle :public shape
{
private:
float height;
public:
triangle()
{
height = 0;
cout << "Constructor of class drived from rectangle!" << endl;
}
triangle(float x, float y)
{
cout << "overloaded constructor of triangle" << endl;
height = x;
length = y;
}
float area()
{
return ((length * height)/2);
}
float parameter()
{
return (3 * length);
}
~triangle()
{
cout << "destructor of class drived from rectangle!" << endl;
}
};
class circle : public shape
{
protected:
float radius;
public:
circle()
{
radius = 0;
}
circle(int x)
{
radius = x;
}
float area()
{
return (3.143*(radius*radius));
}
float parameter()
{
return (2 * 3.143*radius);
}
};
int main()
{
rectangle p1(3, 2);
cout << "The area of rectangle is: " << p1.area() << endl;
cout << "The parameter of rectangle is: " << p1.parameter() << endl;
triangle p2(3, 4);
cout << "The area of triangle is: " << p2.area() << endl;
circle p3(3);
cout << "The area of circle is: " << p3.area() << endl;
cout << "The parameter of circle is: " << p3.parameter() << endl;
shape *p;
rectangle d(3,2);
p = &d;
cout <<"The area of rectangle using polymorphism: " << p->area() << endl;
system("pause");
cout << "heloo" << endl;
return 0;
}