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
|
#include <iostream>
#include <string>
using namespace std;
///////////////////////////////////////
class Shape //parent class
{
protected:
static int numShapes; //keeps track of number of shapes that exist and are available to be rendered, class scope
public:
Shape() //constructor
{
++numShapes; //increment number of shapes when user adds a new one
}
virtual ~Shape() //virtual destructor
{
--numShapes; //decrement number of shapes when user deletes an existing one
}
static int getNumShapes() //retrieves value of the numShapes member, number of shapes entered by user
{
if(numShapes == 0)
{cout << "No shapes to display";}
else
return numShapes;
};
virtual void print() = 0; //display textual description of the shape object
virtual void draw() = 0; //renders shape on the console window
};
//////////////////////////////////////////////////////////////////////////////
//four concrete subclasses provide customized implementations for print and draw methods
//Shape class cannot be instantiated, objects can only be created from the subclasses
//////////////////////////////////////////////////////////////////////////////
class Triangle: public Shape
{
private:
int base, height;
public:
Triangle(int b, int h): //constructor 2-args
base(b), height(h) //initialize member width and height
{
numShapes++;
}
~Triangle(); //deconstructor
void print() //textual description of the shape
{
cout << "Triangle with base " << base <<", and height " << height; //display description
}
void draw()
{
}
};
//////////////////////////////////////////
class Rectangle: public Shape
{
protected:
int width;
int height;
public:
Rectangle(int w, int h): //constructor 2-args
width(w), height(h) //initialize member width and height
{
numShapes++;
}
~Rectangle(); //deconstructor
void print() //textual description of the shape
{
cout << "Rectangle with dimensions (" << width <<", " << height << ")"; //display description
}
void draw() //draws shape
{
cout << string(width, '+' ) << endl;
for( int j = 0; j < height - 2; ++j )
cout<< '+'<< string(width - 2, ' ')<< '+' << endl;
cout << string( width, '+' ) << endl;
}
};
//////////////////////////////////////////
class Square: public Rectangle
{
private:
int width;
int height;
public:
~Square(); //deconstructor
void print()
{
cout << "Square with demensions (" << width <<", " << height << ")";
}
void draw() //draws shape
{
cout << string(width, '+' ) << endl;
for(int j = 0; j < height - 2; ++j )
cout<< '+'<< string(width - 2, ' ')<< '+' << endl;
cout << string( width, '+' ) << endl;
}
};
//////////////////////////////////////////
class Diamond: public Shape
{
private:
int width, height;
public:
Diamond(int w, int h): //constructor 1-arg
width(w), height(h) //initialize member width
{
numShapes++;
}
~Diamond();
void print()
{
cout << "Diamond with width " << width << " and height " << height <<endl;
}
void draw()
{
}
};
|