-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinherit_virtual.cpp
63 lines (57 loc) · 1.37 KB
/
inherit_virtual.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
#include <iostream>
#include <math.h>
// the code has errors.
/* Point at -1;-0.57735
Point at -1;-0.57735
Point at 0;1.1547
Point at -1;-1
Point at -1;1
Point at 1;1
Point at 1;-1 */
class IdealShape
{
protected:
double dimension;
public:
IdealShape(double _dim): dimension(_dim){};
virtual void draw (double x_center, double y_center) =0;
void setPoint(double x, double y)
{
std::cout<<"Point at "<<x<<";"<<y<<"\n";
}
};
class Square : public IdealShape
{
public:
Square(double _dim): IdealShape(_dim){};
void draw (double x_center, double y_center)
{
setPoint(x_center-dimension/2,y_center-dimension/2);
setPoint(x_center-dimension/2,y_center+dimension/2);
setPoint(x_center+dimension/2,y_center+dimension/2);
setPoint(x_center+dimension/2,y_center-dimension/2);
};
};
class Triangle : public IdealShape
{
public:
Triangle(double _dim): IdealShape(_dim){};
void draw (double x_center, double y_center)
{
setPoint(x_center-dimension/2.0,y_center-sqrt(3)/6.0*dimension);
setPoint(x_center-dimension/2.0,y_center-sqrt(3)/6.0*dimension);
setPoint(0,y_center+2.0*sqrt(3)/6.0*dimension);
};
};
int main()
{
//IdealShape I(2);
//I.draw(0,0);
Triangle(2).draw(0,0);
Square S(2);
S.draw(0,0);
IdealShape * I2 = new Square(2);
I2->draw(0,0);
delete I2;
return 0;
}