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 349 350 351 352 353 354 355 356 357 358 359 360 361
|
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
class Shape
{
protected:
int numSides;
public:
Shape(); //default constructor
Shape(int); //constructor with parameter
Shape(const Shape &); //copy constructor
~Shape(); //deconstructor
double area(); //to be overridden by child classes
double perimeter(); //to be overridden by child classes
int getSides(); //returns the number of sides
};
//default constructor
Shape::Shape()
{
numSides = -1;
}
//normal case constructor
Shape::Shape(int sidesIn = -1)
{
numSides = sidesIn;
}
//copy constructor
Shape::Shape(const Shape &shapeIn)
{
numSides = shapeIn.numSides;
}
//deconstructor
Shape::~Shape()
{
// << "Shape deconstructor has run." << endl;
}
//function that will be typically overrided by childs function
double Shape::area()
{
return -1;
}
//function that will be typically overrided by childs function
double Shape::perimeter()
{
return -1;
}
//function that returns the number of sides
int Shape::getSides()
{
return numSides;
}
class Triangle : public Shape
{
private:
double side1;
double side2;
double side3;
public:
Triangle(); //default constructor
Triangle(double, double, double); //constructor with parameters
Triangle(const Triangle &); //copy constructor
~Triangle(); //deconstructor
double area(); //returns area
double perimeter(); //returns perimeter
void incSides(int); //increases each side recursively n times
double getSide1(); //returns side1 value
double getSide2(); //returns side2 value
double getSide3(); //returns side3 value
};
//default constructor
Triangle::Triangle() : Shape(3)
{
side1 = -1;
side2 = -1;
side3 = -1;
}
//normal case constructor w/default values
Triangle::Triangle(double side1In = -1, double side2In = -1, double side3In = -1) : Shape(3)
{
side1 = side1In;
side2 = side2In;
side3 = side3In;
}
//copy constructor
Triangle::Triangle(const Triangle &triangleIn)
{
side1 = triangleIn.side1;
side2 = triangleIn.side2;
side3 = triangleIn.side3;
}
//deconstructor
Triangle::~Triangle()
{
//cout << "Rectangle deconstructor has run.";
}
//increases each side recursively n times
void Triangle::incSides(int n)
{
if (n == 1)
{
return;
}
incSides(n - 1);
side1 += side1;
side2 += side2;
side3 += side3;
}
//calculates and returns area
double Triangle::area()
{
if (side1 == -1 || side2 == -1 || side3 == -1) return 0;
else
{
double s = (side1 + side2 + side3) / 2;
double area = sqrt(s* (s - side1)*(s - side2)*(s - side3));
return area;
}
}
//calculates and returns perimeter
double Triangle::perimeter()
{
if (side1 == -1 || side2 == -1 || side3 == -1) return 0;
else
{
return side1 + side2 + side3;
}
}
//getters for testing
double Triangle::getSide1()
{
return side1;
}
double Triangle::getSide2()
{
return side2;
}
double Triangle::getSide3()
{
return side3;
}
class Rectangle : public Shape
{
private:
double side1;
double side2;
public:
Rectangle(); //default constructor
Rectangle(double, double); //constructor with parameters
Rectangle(const Rectangle &); //copy constructor
~Rectangle(); //deconstructor
double area(); //returns area
double perimeter(); //returns perimeter
void incSides(int); //increases each side recursively n times
double getSide1(); //returns side1 value
double getSide2(); //returns side2 value
};
Rectangle::Rectangle() : Shape(4)
{
side1 = -1;
side2 = -1;
}
//normal case constructor w/default values
Rectangle::Rectangle(double side1In = -1, double side2In = -1) : Shape(4)
{
side1 = side1In;
side2 = side2In;
}
//copy constructor
Rectangle::Rectangle(const Rectangle &rectangleIn)
{
side1 = rectangleIn.side1;
side2 = rectangleIn.side2;
}
//deconstructor
Rectangle::~Rectangle()
{
//cout << "Rectangle deconstructor has run.";
}
//increases each side recursively n times
void Rectangle::incSides(int n)
{
if (n == 1)
{
return;
}
incSides(n - 1);
side1 += side1;
side2 += side2;
}
//calculates and returns area
double Rectangle::area()
{
if (side1 == -1 || side2 == -1) return 0;
else
{
return side1 * side2;
}
}
//calculates and returns perimeter
double Rectangle::perimeter()
{
if (side1 == -1 || side2 == -1) return 0;
else
{
return (2 * side1) + (2 * side2);
}
}
//getters for testing
double Rectangle::getSide1()
{
return side1;
}
double Rectangle::getSide2()
{
return side2;
}
class Circle : public Shape
{
private:
double radius;
public:
Circle(); //default constructor
Circle(double); //constructor with parameter
Circle(const Circle &); //copy constructor
~Circle(); //deconstructor
double getRadius(); //returns radius
void setRadius(double); //sets radius
double area(); //returns area
double perimeter(); //returns perimeter
void incSides(int); //increases length of the radius recursively n times
};
Circle::Circle() : Shape(0)
{
radius = -1;
}
//normal case constructor
Circle::Circle(double radiusIn = -1) : Shape(0)
{
radius = radiusIn;
}
//copy constructor
Circle::Circle(const Circle &circleIn)
{
radius = circleIn.radius;
}
//deconstructor
Circle::~Circle()
{
//cout << "Circle deconstructor has run.";
}
//gets radius
double Circle::getRadius()
{
return radius;
}
//sets radius
void Circle::setRadius(double radiusIn)
{
radius = radiusIn;
}
//returns area
double Circle::area()
{
if (radius == -1) return 0;
else
{
return 3.14 * (radius * radius);
}
}
//returns perimeter
double Circle::perimeter()
{
if (radius == -1) return 0;
else
{
return 2 * 3.14 * radius;
}
}
//increases length the radius recursively n times
void Circle::incSides(int n)
{
if (n == 1)
{
return;
}
incSides(n - 1);
radius += radius;
}
int main()
{
//circle tests
Circle newCircle = Circle(2);
cout << "newCircle radius is: " << newCircle.getRadius() << endl;
newCircle.incSides(3);
cout << "newCircle radius is: " << newCircle.getRadius() << endl;
std::cin.get();
//rectangle tests
Rectangle newRectangle = Rectangle(2, 3);
cout << "newRectangle side1, side2: " << newRectangle.getSide1() << " , " << newRectangle.getSide2() << endl;
newRectangle.incSides(3);
cout << "newRectangle side1, side2: " << newRectangle.getSide1() << " , " << newRectangle.getSide2() << endl;
std::cin.get();
//triangle tests
Triangle newTriangle = Triangle(5, 5, 5);
cout << "newTriangle side1, side2, side3: " << newTriangle.getSide1() << " , " << newTriangle.getSide2() << " , " << newTriangle.getSide3() << endl;
cout << "newTriangle's area: " << newTriangle.area() << endl;
newTriangle.incSides(3);
cout << "newTriangle side1, side2, side3: " << newTriangle.getSide1() << " , " << newTriangle.getSide2() << " , " << newTriangle.getSide3() << endl;
std::cin.get();
return 0;
}
|