-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram of templated class derived from another templated class.cpp
More file actions
189 lines (162 loc) · 3.34 KB
/
Program of templated class derived from another templated class.cpp
File metadata and controls
189 lines (162 loc) · 3.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
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
#include <iostream>
#include <string>
#include<math.h>
using namespace std;
double M_PI = 3.14;
enum eColor { none = 0, red, white, blue, yellow, green, black };
class Color
{
public:
Color(eColor color);
void setColor(eColor color);
eColor getColor() { return mColor; };
std::string getStrColor();
protected:
eColor mColor;
};
Color::Color(eColor _color)
{
mColor = _color;
}
void Color::setColor(eColor _color)
{
mColor = _color;
}
std::string Color::getStrColor()
{
switch(mColor)
{
case red:
return "red";
case white:
return "white";
case blue:
return "blue";
case yellow:
return "yellow";
case green:
return "green";
case black:
return "black";
case none:
default:
return "none";
}
}
template <typename T>
class Circle : public Color
{
public:
Circle(T centerX, T centerY, T radius, eColor color);
Circle(T centerX, T centerY, T radius);
Circle(T radius);
Circle();
T area();
T circumference();
T getX();
T getY();
T getRadius();
protected:
T x;
T y;
T radius;
};
template <typename T>
Circle<T>::Circle(T _x, T _y, T _radius, eColor _color)
: Color(_color)
{
x = _x;
y = _y;
radius = _radius;
}
template <typename T>
Circle<T>::Circle(T _x, T _y, T _radius)
: Color(none)
{
x = _x;
y = _y;
radius = _radius;
}
template <typename T>
Circle<T>::Circle(T _radius)
: Color(none)
{
x = static_cast<T>(0);
y = static_cast<T>(0);
radius = _radius;
}
template <typename T>
Circle<T>::Circle()
: Color(none)
{
x = static_cast<T>(0);
y = static_cast<T>(0);
radius = static_cast<T>(1);
}
template <typename T>
T Circle<T>::area()
{
return M_PI * radius * radius;
}
template <typename T>
T Circle<T>::circumference()
{
return static_cast<T>(2) * M_PI * radius;
}
template <typename T>
class Sphere : public Circle<T>
{
public:
Sphere(T centerZ, T centerX, T centerY, T radius, eColor color);
Sphere(T radius);
Sphere();
T surfaceArea();
T volume();
T getZ();
private:
T z;
};
template <typename T>
Sphere<T>::Sphere(T _x, T _y, T _z, T _radius, eColor _color)
: Circle<T>::Circle (_x, _y, _radius, _color)
{
this->z = _z;
}
template <typename T>
Sphere<T>::Sphere(T _radius)
: Circle<T>::Circle (_radius)
{
// Defaults from Circle(_radius) constructor can also initialize x, y, z
this->x = static_cast<T>(0);
this->y = static_cast<T>(0);
this->z = static_cast<T>(0);
this->radius = _radius;
}
template <typename T>
Sphere<T>::Sphere()
{
// Defaults from Circle() default constructor can also initialize values
this->x = static_cast<T>(0);
this->y = static_cast<T>(0);
this->z = static_cast<T>(0);
this->radius = static_cast<T>(1);
}
template <typename T>
T Sphere<T>::surfaceArea()
{
return static_cast<T>(4) * M_PI * this->radius * this->radius;
}
template <typename T>
T Sphere<T>::volume()
{
T three = 3;
T four = 4;
return four * M_PI * this->radius * this->radius * this->radius / three;
}
int main(int argc, char* argv[])
{
Sphere<float> sphereA(0.0, 0.0, 0.0,10.0, blue);
cout << "\nVolume of SphereA :: " << sphereA.volume() << endl;
cout << "\nColor of SphereA :: " << sphereA.getStrColor() << endl;
return 0;
}