Review List for the first week of 21-day student C ++

Source: Internet
Author: User

Here: html "> http://www.bkjia.com/ebook/201007/19457.html

# Include <iostream>

Using namespace std;

Enum CHOICE {DrawRect = 1, GetArea, GetPerim,

ChangeDimensions, Quit };

 

// Rectangle class declaration

Class Rectangle

{

Public:

// Construcors

Rectangle (int width, int height );

~ Rectangle ();


// Accessors

Int GetHeight () const {return itsHeight ;}

Int GetWidth () const {return itsWidth ;}

Int GetArea () const {return itsHeight * itsWidth ;}

Int GetPerim () const {return 2 * itsHeight + 2 * itsWidth ;}

Void SetSize (int newWidth, int newHeight );


// Misc. methods


Private:

Int itsWidth;

Int itsHeight;

};

 

// Class method implementations

Void Rectangle: SetSize (int newWidth, int newHeight)

{

ItsWidth = newWidth;

ItsHeight = newHeight;

}

 

Rectangle: Rectangle (int width, int height)

{

ItsWidth = width;

ItsHeight = height;

}

 

Rectangle ::~ Rectangle (){}

 

Int DoMenu ();

Void DoDrawRect (Rectangle );

Void DoGetArea (Rectangle );

Void DoGetPerim (Rectangle );

 

Int main ()

{

// Initialize a rectangle to 30,5

Rectangle theRect (30, 5 );


Int choice = DrawRect;

Int fQuit = false;


While (! FQuit)

{

Choice = DoMenu ();

If (choice <DrawRect | choice> Quit)

{

Cout <"Invalid Choice, please try again .";

Continue;

}

Switch (choice)

{

Case DrawRect:

DoDrawRect (theRect );

Break;

Case GetArea:

DoGetArea (theRect );

Break;

Case GetPerim:

DoGetPerim (theRect );

Break;

Case ChangeDimensions:

Int newLength, newWidth;

Cout <"New width :";

Cin> newWidth;

Cout <"New height :";

Cin> newLength;

TheRect. SetSize (newWidth, newLength );

DoDrawRect (theRect );

Break;

Case Quit:

FQuit = true;

Cout <"Exiting ...";

Break;

Default:

Cout <"Error in choice! ";

FQuit = true;

Break;

} // End switch

} // End while

Return 0;

} // End main

 

Int DoMenu ()

{

Int choice;

Cout <"*** Munu ***";

Cout <"(1) Draw Rectangle ";

Cout <"(2) Area ";

Cout <"(3) Perimeter ";

Cout <"(4) Resize ";

Cout <"(5) Quit ";


Cin> choice;

Return choice;

}

 

Void DoDrawRect (Rectangle theRect)

{

Int height = theRect. GetHeight ();

Int width = theRect. GetWidth ();


For (int I = 0; I

{

For (int j = 0; j <width; j ++)

Cout <"*";

Cout <"";

}

}

 

Void DoGetArea (Rectangle theRect)

{

Cout <"Area:" <theRect. GetArea () <endl;

}

 

Void DoGetPerim (Rectangle theRect)

{

Cout <"Perimeter:" <theRect. GetPerim () <endl;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.