-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.cpp
More file actions
348 lines (347 loc) · 7.13 KB
/
Inheritance.cpp
File metadata and controls
348 lines (347 loc) · 7.13 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include<iostream>
#include<math.h>
#include<graphics.h>
#define pi 3.14159
using namespace std;
class shape
{
protected:
int x,y;
public:
virtual void getdata()=0;
virtual void display()=0;
virtual void area()=0;
virtual void perimeter()=0;
};
class point : public shape
{
void getdata()
{
cout<<"Give The Co-ordinate of Point:";
cin>>x>>y;
}
void display()
{
putpixel(x,y,WHITE);
}
void area()
{
cout<<"A point has Zero area.\n";
}
void perimeter()
{
cout<<"A point has Zero perimeter.\n ";
}
};
class Ellip : public shape
{
protected:
int a,b;
public:
void getdata()
{
cout<<"Give The Co-ordinates of Center:";
cin>>x>>y;
cout<<"Give Length of Axes in X & Y direction Respectively:";
cin>>a>>b;
}
void display()
{
fillellipse(x,y,a,b);
}
void area()
{
cout<<" Area is "<<pi*a*b<<" units.\n";
}
void perimeter()
{
cout<<" Perimeter is "<<pi*(3*(a+b)-sqrt((3*a+b)*(a+3*b)))<<" units.\n";
}
};
class Circle : public Ellip
{
public:
void getdata()
{
cout<<"Give The Co-ordinates of Center:";
cin>>x>>y;
cout<<"Give Radius:";
cin>>a;
b=a;
}
};
class Rect : public shape
{
protected:
int a,b;
public:
void getdata()
{
cout<<"Give Co-ordinates for top left corner:";
cin>>x>>y;
cout<<"Give Co-ordinates for bottom right corner:";
cin>>a>>b;
}
void display()
{
rectangle(x,y,a,b);
}
void area()
{
cout<<"Area is "<<abs((a-x)*(y-b))<<" units.\n";
}
void perimeter()
{
cout<<"Perimeter is "<<2*(abs(a-x)+abs(y-b))<<" units.\n";
}
};
class Square : public Rect
{
int l;
public:
void getdata()
{
cout<<"Give Co-ordinates for top left corner:";
cin>>x>>y;
cout<<"Give the length of side:";
cin>>l;
a = l+x;
b = l+y;
}
};
class quadrilateral : public shape
{
public:
int x2,y2,x3,y3,x4,y4;
void getdata()
{
cout<<"Give the Four Co-ordinates of Quadrilateral [x , y]:";
cin>>x>>y>>x2>>y2>>x3>>y3>>x4>>y4;
}
void display()
{
line(x,y,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
line(x4,y4,x,y);
}
void area()
{
cout<<"The Area is "<<abs((x*(y2 - y3) + x2*(y3 - y) + x3*(y - y2))/2.0)+abs((x*(y4 - y3) + x4*(y3 - y) + x3*(y - y4))/2.0)<<" units.\n";
}
void perimeter()
{
cout<<"The Perimeter is "<<2*(sqrt((x2 - x)*(x2 - x) + (y2 - y)*(y2 - y)) + sqrt((x4 - x)*(x4 - x) + (y4 - y)*(y4 - y)))<<" units.\n";
}
};
class parallelogram : public quadrilateral
{
public:
void getdata()
{
cout<<"Give Three Cyclic Co-ordinates of Parallelogram [ x , y ]:";
cin>>x>>y>>x2>>y2>>x3>>y3;
x4 = x + x3 - x2;
y4 = y + y3 - y2;
}
};
class rhombus : public parallelogram
{
public:
void getdata()
{
cout<<"Give Three Cyclic Co-ordinates of Parallelogram [ x , y ]:";
cin>>x>>y>>x2>>y2>>x3>>y3;
x4 = x + x3 - x2;
y4 = y + y3 - y2;
//int a=abs(((y4-y2)*(y3-y))/((x3-x)*(x4-x2)));
if( sqrt((x2 - x)*(x2 - x) + (y2 - y)*(y2 - y)) != sqrt((x3 - x2)*(x3 - x2) + (y3 - y2)*(y3 - y2)) )
{
cout<<"For Given Co-ordinates Rhombus is Not Possible\nSo ";
x=x2=x3=x4=0;
y=y2=y3=y4=0;
}
}
};
class polygon:public shape
{
protected:
int *pt,*x,*y,num,i,j,k;
double ar=0,pr;
public:
void getdata()
{
cout<<"\n\nEnter The No. Of Vertex Of Polygon:";
cin>>num;
pt = new int [2*num+2];
x = new int [num+1];
y = new int [num+1];
cout<<"\n\nEnter The Coordinates Of The Points :-";
for(i=0;i<(2*num);i+=2)
{
cin>>pt[i]>>pt[i+1];
}
pt[i]=pt[0];
pt[i+1]=pt[1];
}
void display()
{
drawpoly(num+1,pt);
}
void area()
{
j=0;k=0;
for(i=0;i<(2*num);i++)
{
if(i%2==0)
{
x[j]=pt[i];
j++;
}
else
{
y[k]=pt[i];
k++;
}
}
j=num-1;
for(i=0;i<num;i++)
{
ar=ar+(x[j]+x[i])*(y[j]-y[i]);
j=i;
}
cout<<"\n\n\t Area Of Given Shape Is: "<<abs(ar/2)<<" .\n";
}
void perimeter()
{
j=num-1;
pr=0;
for(i=0;i<num;i++)
{
pr=pr+sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
j=i;
}
cout<<"\n\n\t Perimeter Of Given Shape Is: "<<pr<<" .\n";
}
};
class Line : public shape
{
int a,b;
public:
void getdata()
{
cout<<"Give Co-ordinates of initial point:";
cin>>x>>y;
cout<<"Give Co-ordinates of Final Point:";
cin>>a>>b;
}
void display()
{
line(x,y,a,b);
}
void area()
{
cout<<"A line has Zero area\n";
}
void perimeter()
{
cout<<"The Perimeter of line is "<<sqrt((a-x)*(a-x)+(b-y)*(b-y))<<" units.\n";
}
};
class Triangle : public shape
{
int x2,y2,x3,y3;
void getdata(){
cout<<"Give The Co-ordinates For Triangle\n First Point:";
cin>>x>>y;
cout<<"Second Point:";
cin>>x2>>y2;
cout<<"Third Point:";
cin>>x3>>y3;
}
void display()
{
line(x,y,x2,y2);
line(x3,y3,x2,y2);
line(x,y,x3,y3);
}
void area()
{
cout<<"The Area Of Triangle is "<<abs((x*(y2 - y3) + x2*(y3 - y) + x3*(y - y2)) / 2)<<" units.\n";
}
void perimeter()
{
cout<<"The Perimeter Is "<<sqrt((x2 - x)*(x2 - x) + (y2 - y)*(y2 - y)) + sqrt((x3 - x)*(x3 - x) + (y3 - y)*(y3 - y))+sqrt((x2 - x3)*(x2 - x3) + (y2 - y3)*(y2 - y3))<<" units.\n";
}
};
int main()
{
int gd = DETECT ,gm,num;
initgraph(&gd ,&gm ,"..\\BGI");
setcolor(BLUE);
shape *s;
cout<<"Choose the Figure for Analysis:\n1.Point \t\t\t2.Line\n3.Circle \t\t\t4.Rectangle\n5.Ellipse\t\t\t6.Triangle\n7.Square \t\t\t8.Parallelogram\n9.Quadrilateral\t\t\t10.Rhombus\n11.Polygon\n";
cin>>num;
if(num==1)
{
point obj;
s = &obj;
}
else if(num==2)
{
Line obj;
s = &obj;
}
else if(num==3)
{
Circle obj;
s = &obj;
}
else if(num==4)
{
Rect obj;
s = &obj;
}
else if(num==5)
{
Ellip obj;
s = &obj;
}
else if(num==6)
{
Triangle obj;
s = &obj;
}
else if(num==7)
{
Square obj;
s = &obj;
}
else if(num==8)
{
parallelogram obj;
s = &obj;
}
else if(num==9)
{
quadrilateral obj;
s = &obj;
}
else if(num==10)
{
rhombus obj;
s = &obj;
}
else if(num==11)
{
polygon obj;
s = &obj;
}
s -> getdata();
s -> display();
s -> area();
s -> perimeter();
getch();
closegraph();
return 0;
}