-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopied.cpp
More file actions
116 lines (116 loc) · 2.34 KB
/
Copy pathcopied.cpp
File metadata and controls
116 lines (116 loc) · 2.34 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
// virtual functions with shapes
#include <iostream>
using namespace std;
#include <graphics.h>
//for graphics functions
////////////////////////////////////////////////////////////////
class shape
//base class
{
protected:
int xCo, yCo;
//coordinates of center
color fillcolor;
//color
fstyle fillstyle;
//fill pattern
public:
//no-arg constructor
shape() : xCo(0), yCo(0), fillcolor(cWHITE),
fillstyle(SOLID_FILL)
{ }
//4-arg constructor
shape(int x, int y, color fc, fstyle fs) :
xCo(x), yCo(y), fillcolor(fc), fillstyle(fs)
{ }
virtual void draw()=0
//pure virtual draw function
{
set_color(fillcolor);
set_fill_style(fillstyle);
}
};
////////////////////////////////////////////////////////////////
class ball : public shape
{
private:
int radius;
//(xCo, yCo) is center
public:
ball() : shape()
//no-arg constr
{ }
//5-arg constructor
ball(int x, int y, int r, color fc, fstyle fs)
: shape(x, y, fc, fs), radius(r)
{ }
void draw()
//draw the ball
{
shape::draw();
draw_circle(xCo, yCo, radius);
}
};
////////////////////////////////////////////////////////////////
class rect : public shape
{
private:
int width, height;
//(xCo, yCo) is upper left corner
public:
rect() : shape(), height(0), width(0)
//no-arg ctor
{ }
//6-arg ctor
rect(int x, int y, int h, int w, color fc, fstyle fs) :
shape(x, y, fc, fs), height(h), width(w)
{ }
void draw()
//draw the rectangle
{
shape::draw();
draw_rectangle(xCo, yCo, xCo+width, yCo+height);
set_color(cWHITE);
//draw diagonal
draw_line(xCo, yCo, xCo+width, yCo+height);
}
};
////////////////////////////////////////////////////////////////
class tria : public shape
{
private:
int height;
//(xCo, yCo) is tip of pyramid
public:
tria() : shape(), height(0) //no-arg constructor
{ }
//5-arg constructor
tria(int x, int y, int h, color fc, fstyle fs) :
shape(x, y, fc, fs), height(h)
{ }
void draw()
//draw the triangle
{
shape::draw();
draw_pyramid(xCo, yCo, height);
}
};
////////////////////////////////////////////////////////////////
int main()
{
int j;
init_graphics();
//initialize graphics system
shape* pShapes[3];
//array of pointers to shapes
//define three shapes
pShapes[0] = new ball(40, 12, 5, cBLUE, X_FILL);
pShapes[1] = new rect(12, 7, 10, 15, cRED, SOLID_FILL);
pShapes[2] = new tria(60, 7, 11, cGREEN, MEDIUM_FILL);
for(j=0; j<3; j++)
pShapes[j]->draw();
for(j=0; j<3; j++)
delete pShapes[j];
set_cursor_pos(1, 25);
return 0;
}