-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolygons
More file actions
132 lines (106 loc) · 3.79 KB
/
Polygons
File metadata and controls
132 lines (106 loc) · 3.79 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
// CPSC 298-6 "Programming in C++"
// chukim@champman.edu
// In-class Programming Project: Polygons
// File: Polygons_incomplete.cpp
#include <iostream>
#include <string>
// //////////////////////////////////////////////////////////////////////////////////////////////
// Base Class Polygon - an Abstract Base Class, can't be instantiated
// //////////////////////////////////////////////////////////////////////////////////////////////
class Polygon {
public:
Polygon(double dWidth, double dHeight);
virtual ~Polygon();
virtual double area() = 0; // Pure virtual member function - no implementation in base class
virtual void displayProperties();
protected:
double m_dWidth;
double m_dHeight;
std::string m_strType;
};
Polygon::Polygon(double dWidth, double dHeight) : m_dWidth(dWidth), m_dHeight(dHeight), m_strType("Polygon")
{
}
Polygon::~Polygon()
{
std::cout << "Polygon Destructor called" << std::endl;
}
void Polygon::displayProperties()
{
std::cout << "Polygon Type: " << this->m_strType << ", Width: " << this->m_dWidth << ", Height: " << this->m_dHeight << std::endl;
return;
}
// //////////////////////////////////////////////////////////////////////////////////////////////
// Derived Class Rectangle ("Concrete" class, can be instantiated)
// //////////////////////////////////////////////////////////////////////////////////////////////
class Rectangle : public Polygon
{
public:
Rectangle(double dWidth, double dHeight);
~Rectangle();
double area();
};
Rectangle::Rectangle(double dWidth, double dHeight) : Polygon(dWidth, dHeight)
{
this->m_strType = "Rectangle";
}
Rectangle::~Rectangle()
{
std::cout << "Rectangle Destructor called" << std::endl;
}
double Rectangle::area()
{
return (this->m_dHeight * this->m_dWidth);
}
// //////////////////////////////////////////////////////////////////////////////////////////////
// Derived Class Triangle ("Concrete" class, can be instantiated)
// //////////////////////////////////////////////////////////////////////////////////////////////
/* TODO: Define the Triangle class */
class Triangle: public Polygon{
public:
Triangle(double dWidth, double dHeight);
~Triangle();
double area();
};
/* TODO: Define the Triangle constructor; make sure to set m_strType to "Triangle" */
Triangle::Triangle(double dWidth, double dHeight): Polygon(dWidth, dHeight){
this->m_strType = "Triangle";
}
Triangle::~Triangle()
{
std::cout << "Triangle Destructor called" << std::endl;
}
double Triangle:: area(){
return .5 * this->m_dWidth * this->m_dHeight;
}
/* TODO: Define the Triangle destructor; have it print out "Triangle Destructor called" */
/* TODO: Define the Triangle area member function; Area of a triangle = 0.5 * m_dWdith * m_dHeight */
int main()
{
Polygon* p_polygonRectangle = new Rectangle(2.0, 4.0);
/* TODO: Declare a pointer to a Polygon variable (e.g. p_polygonTriangle)
* and assign it to a Triangle object allocated using the new operator;
* the width of the triangle should be 3.0 and the height 6.0.
*/
Polygon* p_polygonTriangle = new Triangle(3.0,6.0);
Polygon* p_polygon[2]; // You can create pointers to Polygon, but not a Polygon object
p_polygon[0] = p_polygonRectangle;
/* TODO: Assign the triangle object pointer to array element 1 of p_polygon. */
p_polygon[1] = p_polygonTriangle;
/* TODO: change "i < 1" to "i < 2" so that the loop iterates over both the Rectangle
* and the Triangle.
*/
for (int i = 0; i < 2; i++)
{
// Polymorphic invocation
p_polygon[i]->displayProperties(); // take advantage of reuse; use base class function
double dArea = p_polygon[i]->area();
std::cout << "Area: " << dArea << std::endl;
}
delete p_polygonRectangle;
/* TODO: Delete the allocated Triangle object via the pointer to the Triangle object
* that you declared.
*/
delete p_polygonTriangle;
return 0;
}