Ok im having trouble compiling this. Its exactly what the book has written. I know that this book is out of standard (teach yourself c++ in 21 days).
Code:
#include <iostream>
typedef unsigned short int USHORT;
typedef unsigned long int ULONG;
enum BOOL { FALSE, TRUE};
enum CHOICE { DrawRect = 1, GetArea,
GetPerim, ChangeDimensions, Quit};
// Rectangle class declaration
class Rectangle
{
public:
// constructors
Rectangle(USHORT width, USHORT height);
~Rectangle();
// accessors
USHORT GetHeight() const { return itsHeight; }
USHORT GetWidth() const { return itsWidth; }
ULONG GetArea() const { return itsHeight * itsWidth; }
ULONG GetPerim() const { return 2*itsHeight + 2*itsWidth; }
void SetSize(USHORT newWidth, USHORT newHeight);
// Misc. methods
void DrawShape() const;
private:
USHORT itsWidth;
USHORT itsHeight;
};
// Class method implementations
void Rectangle::SetSize(USHORT newWidth, USHORT newHeight)
{
itsWidth = newWidth;
itsHeight = newHeight;
}
Rectangle::Rectangle(USHORT width, USHORT height)
{
itsWidth = width;
itsHeight = height;
}
Rectangle::~Rectangle() {}
USHORT DoMenu();
void DoDrawRect(Rectangle);
void DoGetArea(Rectangle);
void DoGetPerim(Rectangle);
int main ()
{
// initialize a rectangle to 10,20
Rectangle theRect(30,5);
USHORT choice = DrawRect;
USHORT fQuit = FALSE;
while (!fQuit)
{
choice = DoMenu();
if (choice < DrawRect || choice > Quit)
{
std::cout << "\nInvalid Choice, please try again.\n\n";
continue;
}
switch (choice)
{
case DrawRect:
DoDrawRect(theRect);
break;
case GetArea:
DoGetArea(theRect);
break;
case GetPerim:
DoGetPerim(theRect);
break;
case ChangeDimensions:
USHORT newLength, newWidth;
std::cout << "\nNew width: ";
std::cin >> newWidth;
std::cout << "New height: ";
std::cin >> newLength;
theRect.SetSize(newWidth, newLength);
DoDrawRect(theRect);
break;
case Quit:
fQuit = TRUE;
std::cout << "\nExiting...\n\n";
break;
default:
std::cout << "Error in choice!\n";
fQuit = TRUE;
break;
} // end switch
} // end while
return 0;
} // end main
USHORT DoMenu()
{
USHORT choice;
std::cout << "\n\n *** Menu *** \n";
std::cout << "(1) Draw Rectangle\n";
std::cout << "(2) Area\n";
std::cout << "(3) Perimeter\n";
std::cout << "(4) Resize\n";
std::cout << "(5) Quit\n";
std::cin >> choice;
return choice;
}
void DoDrawRect(Rectangle theRect)
{
USHORT height = theRect.GetHeight();
USHORT width = theRect.GetWidth();
for (USHORT i = 0; i<height; i++)
{
for (USHORT j = 0; j< width; j++)
std::cout << "*";
std::cout << "\n";
}
}
void DoGetArea(Rectangle theRect)
{
std::cout << "Area: " << theRect.GetArea() << std::endl;
}
void DoGetPerim(Rectangle theRect)
{
std::cout << "Perimeter: " << theRect.GetPerim() << std::endl;
}
COMPILER: DEV C++ (newest beta version)
ERRORS:
5 parse error before numeric constant
48 variable or field `DoDrawRect' declared
48 invalid conversion from `BOOL (*)(HDC__*,
49 variable or field `DoGetArea' declared
49 invalid conversion from `BOOL (*)(HDC__*,
50 variable or field `DoGetPerim' declared
50 invalid conversion from `BOOL (*)(HDC__*,
[Warning] In function `int main()':
55 `Rectangle' undeclared (first use this
++. many more.
Thanks,
arnis