Author : -=-=[ccoder]=-=-
Compiler : Microsoft Visual Studio 2008
Purpose : Object-Oriented program that defines and demonstrates a class representing a Rectangle
Limitations : No validation provided
----------------------------------------------------------------------------------------------------------------------*/
#include
using namespace std;
class Rectangle
{
private:
int height;
int width;
char ch; // character use for drawing rectangle
bool flag; // flag when rectangle is filled
public:
Rectangle(): height (0), width (0), ch (' '), flag (true) // default constructor
{}
Rectangle( int h, int w, char c, bool f ) // overloaded constructor
{
height = h;
width = w;
ch = c;
flag = f;
}
void flip_rectangle() // flip the orientation of rectangle
{
int temp;
temp = height;
height = width;
width = temp;
}
void filled_flag() // toogle the Filled flag
{
if (height != 1 && width != 1)
{
if ( flag == true)
{
flag = false;
}
else
{
flag = true;
}
}
}
int calculate_area() // calculate and returns area
{
int area;
area = height * width;
return area;
}
int getHeight(){return height;} // accessor functions to return private data
int getWidth(){return width;}
char getChar(){return ch;}
bool getFlag(){return flag;}
void set( int h, int w, char c = ' ', bool f = true ); // prototype only
void draw_rectangle();
};
void Rectangle::set( int h, int w, char c, bool f ) // set data members accordingly
{
height = h;
width = w;
ch = c;
flag = f;
}
void Rectangle::draw_rectangle() // draw rectangle
{
if (flag == true)
{
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
cout << ch << " ";
}
cout << endl;
}
}
else
if (flag == false)
{
for(int i=1; i <= width; i++)
{
cout << ch << " ";
}
cout << endl;
for(int i=1; i <= (height-2); i++)
{
cout << ch << " ";;
for(int j=1; j <= ((width*2) - 4); j++)
{
cout << " ";
}
cout << ch << endl;
}
for(int y=1; y <= width; y++)
{
cout << ch << " ";;
}
}
}
int main()
{
int tempHeight, tempWidth; // temp variable to gather input
char tempCh;
Rectangle rect1; // declare a blank object
Rectangle rect2 ( 8, 16, '*', false ); // declare/initialize another
cout << "Enter Height of rectangle: ";
cin >> tempHeight;
cout << "Enter Width of rectangle: ";
cin >> tempWidth;
cout << "Enter the character for rectangle drawing: ";
cin >> tempCh;
rect1.set(tempHeight, tempWidth, tempCh);
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is Filled ";
system("pause");
rect1.filled_flag();
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is UnFilled ";
system("pause");
rect1.flip_rectangle();
rect1.draw_rectangle();
cout << endl << "Rectangle 1 is flipped ";
system("pause");
rect1.calculate_area();
cout << endl << "The area of Rectangle 1 is: " << rect1.calculate_area() << " ";
system("pause");
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is UnFilled ";
system("pause");
rect2.filled_flag();
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is Filled ";
system("pause");
rect2.flip_rectangle();
rect2.draw_rectangle();
cout << endl << "Rectangle 2 is flipped ";
system("pause");
rect2.calculate_area();
cout << endl << "The area of Rectangle 2 is: " << rect2.calculate_area() << " ";
system("pause");
cout << endl << "The Rectangle 1: " << endl;
cout << "Height: " << rect1.getHeight() << endl;
cout << "Width: " << rect1.getWidth() << endl;
cout << "Character use to draw: " << rect1.getChar() << endl;
system("pause");
cout << endl << "The Rectangle 2: " << endl;
cout << "Height: " << rect2.getHeight() << endl;
cout << "Width: " << rect2.getWidth() << endl;
cout << "Character use to draw: " << rect2.getChar() << endl;
system("pause");
return 0;
}
No comments:
Post a Comment