Hey guys!
I am new at Class and object things. Before introducing the object oriented issues my life was easier! I wrote these codes here:
/*---------------------------------------------------------------------------------------
Source File: OOP4.Henri.cpp
Program name: Object Oriented program, Assignment 4
Author: Hassan Aghaei Baradaran (Henri)
Compiler: Bloodshd Dev-C++ 4.9.9.2
Purpose: To define and demonstrate a class representing a Rectangle
-----------------------------------------------------------------------------------------*/
#include <stdlib.h> // system pause
#include <iostream> // input output
#include <string> // to use string class
#include <iterator> // used for line_maker function
#include <sstream> // used for line_maer function
using namespace std;
class Rectangle // CLASS DEFINITION
{
private:
int height; // data members
int width;
char ch;
bool filled;
public: // Methods
Rectangle() // default constructor (part 1 of the program)
{
height = 0;
width = 0;
ch = ' ';
filled = false;
}
Rectangle(int h, int w, char c, bool f) // 4-argument constructor (part 2 of the program)
{ // allows the members to be initialized
height = h;
width = w;
ch = c;
filled = f;
}
// --------------------------------- MUTATOR FUNCTIONS:
void set(int h, int w, char c, bool f) // Set function (part 3 of the program)
{ // assigns valus to data
height = h;
width = w;
ch = c;
filled = f;
}
void flip(int); // (part 4 of the program)
void toggle(); // Prototype for toggling the flag
// (part 5 of the program)
int getheight() // (part 6 of the program)
{ // accessor (get) functions
return height; // to return private members
}
int getwidth()
{
return width;
}
char getch()
{
return ch;
}
bool getfilled()
{
return filled;
}
int calc_area(){return (width*height);}; // (part 7 of the program)
// Member function defined inside the class
void draw_rect(int, int, char, bool); // (part 8 of the program)
// prototype for drawing the Rectangle
}; // end of Class definition
//-----------------------------------------------------------------------------------------
// MEMBER FUNCTIONS DEFINED OUTSIDE THE CLASS
string line_maker(char ch, int cnt)
{
ostringstream os;
fill_n(ostream_iterator<char>(os, " "), cnt, ch);
return os.str();
}
void Rectangle::draw_rect(int height, int width, char ch, bool flag) // using scope operator
{
if (flag == false || (flag == true && (height < 3 && width < 3))) // filled
{
for (int row = 0; row < height; ++row)
cout << line_maker(ch, width) << '\n';
}
else // not filled
{
cout << line_maker(ch, width) << '\n';
for (int row = 1; row < height - 1 ; ++row)
cout << ch << string(width*2-3, ' ') << ch << '\n';
cout << line_maker(ch, width) << '\n';
}
}
void Rectangle::flip(int temp)
{
temp = width;
width = height;
height = temp;
}
void Rectangle::toggle()
{
char c1,c2;
cout << endl << endl << "Do you want to toggle the flag?(Y,N)" << endl << endl;
cin >> c1;
c2 = toupper(c1);
if (c2 == 'Y')
{
if (filled == true)
{
filled = false;
cout << "Flag is set off!";
}
else
filled = true;
cout << endl << "Flag is set on!";
}
else
{
cout << endl <<"Same Pattern as before! " ;
}
}
//-------------------------------------------------------------------------------------
int main()
{
int tempHeight = 0, tempWidth = 0; // temp variables to gather input
char tempCh;
bool tempFilled;
Rectangle rect1; // declare a blank object
Rectangle rect(4, 5, '*', false); // another object
cout << "Enter Height of the rectangle: "; // gather input into temp variables
cin >> tempHeight;
cout << "Enter Width of the rectangle: ";
cin >> tempWidth;
cout << "Choose your Character (*, @, ^, or others): ";
cin >> tempCh;
rect1.set(tempHeight, tempWidth, tempCh, false); // set data members
cout << "\n";
rect1.draw_rect(tempHeight, tempWidth, tempCh, false);
cout << endl << "The Height and Width are respectively: " << endl;
cout << endl << rect1.getheight() << " AND " << rect1.getwidth() << endl;
cout << endl << "Area of your rectangle is:" << rect1.calc_area() << endl ;
rect1.flip(0); // swap!
cout << "\n" << "The flipped pattern is:" << endl << endl;
rect1.draw_rect(rect1.getheight(), rect1.getwidth(), tempCh, false); // call teh flipped Height and wWight
Rectangle rect2(4, 13, char(1), false); // Using overloaded Constructor
cout << "\n" << "A random pattern is: " << endl << endl;
rect2.draw_rect(rect2.getheight(), rect2.getwidth(), rect2.getch(), false);
rect2.toggle();
cout << " So, the pattern you have chosen is: " << endl << endl;
rect2.draw_rect(rect2.getheight(), rect2.getwidth(), rect2.getch(), rect2.getfilled());
cout << "\n";
system("pause");
return 0;
}
My teacher's feedback:
You missed an important point. The rectangle "knows" its internal state. When you draw the rectangle, you do not need to send it any data. It already knows its own height, width, etc. (which ave already been set in the constructor or set function). So you just tell it to draw itself; no arguments necessary. Same for the flip.
And the toggle should not do any input or output. When you tell the rectangle to toggle itself, it just toggles the flag internally, that's all -- by calling the function, you are sending a message to the rectangle to change its internal state. To see the results, you ask it to draw itself again in a separate message.
Any suggestion on this please?
Thank you!