This is probably the most advanced thing that I have programmed so far... and its not very advanced yet but I've only been programming for about 2 weeks now.
typedef unsigned long int ULONG;
typedef unsigned short int USHORT;
#include
enum BOOL { FALSE , TRUE };
enum CHOICE { DrawRect = 1 , GetDims , GetArea , GetPerim,
ChangeDims , Quit };
class Rectangle
{
public:
//Rectangle Constructor
Rectangle( USHORT width , USHORT height );
//Rectangle Deconstructor
~Rectangle();
//accessors
void setSize( USHORT newWidth , USHORT newHeight );
USHORT getHeight() const { return itsHeight; }
USHORT getWidth() const { return itsWidth; }
ULONG getArea() const { return itsWidth * itsHeight; }
ULONG getPerim() const { return 2*itsWidth + 2*itsHeight; }
private:
USHORT itsWidth;
USHORT itsHeight;
};
//Rectangle Constructor
Rectangle::Rectangle( USHORT width , USHORT height )
{
itsWidth = width;
itsHeight = height;
}
//Rectangle Deconstructor
Rectangle::~Rectangle() {}
//Set the rectangle's size
void Rectangle::setSize( USHORT newWidth , USHORT newHeight )
{
itsWidth = newWidth;
itsHeight = newHeight;
}
USHORT menu();
void doGetArea( Rectangle );
void doGetPerim( Rectangle );
void doDrawRect( Rectangle );
void doGetDims( Rectangle );
int main()
{
Rectangle theRect(10,10);
USHORT choice = DrawRect;
USHORT qQuit = FALSE;
USHORT newHeight, newWidth;
//Loop
while (!qQuit)//While Quit is not true (or not equal to 1)
{
choice = menu();//Call the menu function
if (choice < DrawRect || choice > Quit)//Check to see if choice
//is valid
{
cout << "\nInvalid Choice, please try again.\n\n";
continue;
}
switch( choice )//Menu choices, default set to exit
{
case DrawRect:
doDrawRect(theRect);
break;
case GetDims:
doGetDims(theRect);
break;
case GetArea:
doGetArea(theRect);
break;
case GetPerim:
doGetPerim(theRect);
break;
case ChangeDims:
cout << "\nEnter new height: ";
cin >> newHeight;
cout << "\nEnter new width: ";
cin >> newWidth;
theRect.setSize( newWidth , newHeight );
cout << "\n";
break;
case Quit:
qQuit = TRUE;
break;
default:
cout << "Error in choice!\n";
qQuit = TRUE;
break;
}//End switch
}// End loop
}// End main
//Menu
USHORT menu()
{
USHORT choice;
cout << "--- Menu --- \n";
cout << "(1) Draw Rectangle\n";
cout << "(2) Dimensions\n";
cout << "(3) Area\n";
cout << "(4) Perimeter\n";
cout << "(5) Resize\n";
cout << "(6) Quit\n";
cout << "\nChoice: ";
cin >> choice;
return choice;
}
//Draw the rectangle
void doDrawRect( Rectangle theRect )
{
USHORT width = theRect.getWidth(); //Get rectangle width
USHORT height = theRect.getHeight(); //Get rectangle height
cout << "\n\n";
for( USHORT i = 0; i < height; i++ )
{
for( USHORT j = 0; j < width; j++ )
cout <<"*"; //draw *s until j is equal to the width
cout <<"\n"; //draw another row of *s until i is equal to the height
}
cout << "\n\n";
}
//Get and output the area of the rectangle
void doGetArea( Rectangle theRect )
{
ULONG area = theRect.getArea();
cout << "\nArea: " << area << "\n\n";
}
//Get and output the perimeter of the rectangle
void doGetPerim( Rectangle theRect )
{
ULONG perim = theRect.getPerim();
cout << "\nPerimeter: " << perim << "\n\n";
}
//Get and output the dimensions of the rectangle
void doGetDims( Rectangle theRect )
{
USHORT width = theRect.getWidth();
USHORT height = theRect.getHeight();
cout << "\nDimensions: ";
cout << "\nWidth: " << width;
cout << "\nHeight: " << height << "\n\n";
}