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
|
// -----------------------------------------------------------------
// This program allows the user to create and delete shapes with dimensions
// of their choosing. They can then view the total number of shapes or view
// a textual and graphic rendering of all shapes created.
// -----------------------------------------------------------------
#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() // always add a virtual destructor in a class with virtual functions
{
--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;
public:
Triangle(int base): //constructor 2-args
base(base) //initialize member width and height
{
}
~Triangle(); //deconstructor
void print() //textual description of the shape
{
int height = (base-1)/2; //set height
cout << "Triangle with base " << base <<", and height " << height; //display description
}
void draw()
{
}
};
class Rectangle: public Shape
{
private:
int width;
int height;
public:
Rectangle(int width, int height): //constructor 2-args
width(width), height(height) //initialize member width and height
{
}
~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;
public:
Diamond(int width): //constructor 1-arg
width(width) //initialize member width
{
}
~Diamond();
void print()
{
int height = (width - 1);
cout << "Diamond with width " << width << " and height " << height <<endl;
}
void draw()
{
}
};
////////////////////////////////////
int main()
{
cout << "This program allows the user to create and delete shapes with dimensions\n"
<< "of their choosing. They can then view the total number of shapes or view\n"
<< "a textual and graphic rendering of all shapes created.\n\n";
int j;
Shape * shapeArr[20];
int ch, ch2, ch3; //choice variables for menu navigation
while(ch!=5)
{
cout << "Main menu:\n\n" //main menu listing
<< "1. Add a Shape\n" //user will enter in data about a shape of their choice
<< "2. Delete a Shape\n" //user will be presented with list of shapes entered so far, can choose one to delete
<< "3. Display total number of Shapes\n" //displays number of shapes (getNumShapes)
<< "4. Display all Shapes\n" //displays textual/graphic description (print() and draw()) for all shapes entered so far
<< "5. Quit\n\n"
<< "Enter your choice: "; cin >> ch; cout << "\n\n";
switch(ch) //main menu switch statement
{
case 1:
cout << "1. Add a Rectangle\n"
<<"2. Add a Square\n"
<<"3. Add a Triangle\n"
<<"4. Add a diamond\n"
<<"5. Cancel\n\n"
<<"Enter your choice: "; cin >> ch2; cout << "\n\n";
switch(ch2) //shape menu switch statement
{
case 1:
int width, height;
cout << "Enter rectangle width: ";
cin >> width;
cout << "Enter rectangle height: ";
cin >> height;
Rectangle* rect = new Rectangle(width, height); //make new rectangle
shapeArr[j] = rect; //add to shapeArr
break;
case 2:
int width; int height;
cout << "Enter square width: ";
cin >> width;
cout << "Enter square height: ";
cin >> height;
Square* sq = new Square; //make new square
shapeArr[j] = sq; //add to shapeArr
break;
case 3:
int base;
cout << "Enter triangle base: ";
cin >> base;
Triangle* tri = new Triangle(base); //make new triangle
shapeArr[j] = tri; //add to shapeArr
break;
case 4:
int width;
cout << "Enter diamond width: ";
cin >> width;
Diamond* di = new Diamond(width); //make new diamond
shapeArr[j] = di; //add to shapeArr
break;
case 5: break;
}//end shape menu switch statement
break;
case 2:
switch(ch3)
//display list of shapes entered so far using print()
//delete the one the user chooses
cout << "Choose shape to delete: ";
for(j = 0; j<20; j++) //print descriptions of shape array
{cout << j << " "; shapeArr[j] ->print(); cout << endl;}
//user chooses one to delete and program deletes it
break;
case 3:
//getNumShapes();
break;
case 4:
//textual and graphic displays of the shape array
for(j = 0; j <20; j++) //print shapes
shapeArr[j] -> print();
for(j = 0; j <20; j++) //draw shapes
shapeArr[j] -> draw();
break;
case 5:
//terminates program
break;
}//end main menu switch statement
}
}
|