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!

Next time put code in code tags. Also the teacher said that you don't need set() function. You first ask user for input like hight and width and then you make a new rectangle with hight and width. So it should look SOMETHING like this

cout << "enter hight: ";
int hight;
cin >> hight;
cout << "enter width: ";
int width;
cin >> width;
cout << "enter star";
char sign;
cin >> sign;
Rectangle rect(hight, width, sign, false);

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.

Your class private member variables do not need to passed as parameters in the function draw_rect. Only arguments need to be be passed through a class function is when the argument does not belong to that specific class; i.e. non-member data types and their variables.

Your member variables are the 4 that you listed, height, width, filled, ch. So any class member function (mutator/accessor/etc) do not require you to list the height, width, filled, or ch (Unless it belongs to another class! And in that case, you'd likely pass the whole class and not likely the single variable of that class).

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.

The programmer who is using your class and chooses to use toggle function is expecting to just toggle, not a reconfirmation of wanting to toggle or not. It's like turning on the lights in the room and a pop up asks "Are you sure you wanted to turn the lights on?" -We don't need this reconfirmation that we want to turn the lights on, we just expect to turn it on; we physically made the attempt to toggle the switch and now just expect it to turn on (and off as we toggle it again as we leave the room).

Next time put code in code tags. Also the teacher said that you don't need set() function. You first ask user for input like hight and width and then you make a new rectangle with hight and width. So it should look SOMETHING like this

cout << "enter hight: ";
int hight;
cin >> hight;
cout << "enter width: ";
int width;
cin >> width;
cout << "enter star";
char sign;
cin >> sign;
Rectangle rect(hight, width, sign, false);

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;
}

Seeing as how this is part 3 of the program, as required by the teacher in the comments of the original poster, set function is required. But you are right in the terms of using the constructor to first make your object; the rectangle. What set function will do is change the contents of the already made object instead of making a new object with the changed settings.

I guess you are right but in this program he doesnt really need to change it. He created the object before asking for input and it was unnecessary.

I guess you are right but in this program he doesnt really need to change it. He created the object before asking for input and it was unnecessary.

You think so? I think what he did was just fine. First creating the empty object and then setting the values to the users choice. This set up will be handy when he learns about pointers and dynamic arrays/classes/structures that are soon to come. Note that there is a second object that was made with the constructor of 4 parameters.

I suppose it comes down to personal preference with this specific example that the OP has given.

commented: You are correct! +0

Maybe you are right. I just skimmed through the code, and now that I read it again more carefully, I agree with you

Hey guys thank you for your guidelines! I modified it 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();                            // (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();     // (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()   // using scope operator
{
 if (filled == false || (filled == 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 = 0;
            temp   = width;
            width  = height;
            height = temp;     
}

void Rectangle::toggle()
{

    cout << endl << "<<Toggling>>" << endl;

    if (filled == true)
    {
    filled = false;  
    cout << endl <<"Flag is set off!" << endl << endl;         
    }
    else
    {filled = true;
    cout << endl << "Flag is set on!" << endl << endl;
    }


}

//-------------------------------------------------------------------------------------
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();

       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();                        // swap!
       cout << "\n" << "The flipped pattern is:" << endl << endl;
       rect1.draw_rect();                  // call teh flipped Height and wWight

       Rectangle rect2(4, 13, char(1), true);        // Using overloaded Constructor 
       cout << "\n" << "A random pattern is: " << endl << endl;
       rect2.draw_rect();

       rect2.toggle();
       rect2.draw_rect();
       cout << "\n";

       system("pause");
       return 0;

}

My teacher's feedback:

Now you've got the idea! But no bonuses at the moment (or maybe 1/2 a bonus for the draw).

Any idea to get the full bonus mark plz?

Thanks!

I'm not sure what you mean by full bonus mark. Is the teacher requesting you to continue the code beyond the scope of what is required by having an 8th or more part of the program?

Also, when you submit code, please submit it with the code tags, it makes it much easier for the rest of us to read.

[ CODE] <<-- beginning of the tag

[/ CODE] <<-- end of the tag Just don't have the spaces

/*---------------------------------------------------------------------------------------

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(); // (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(); // (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() // using scope operator
{
if (filled == false || (filled == 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 = 0;
temp = width;
width = height;
height = temp;
}

void Rectangle::toggle()
{

cout << endl << "<<Toggling>>" << endl;

if (filled == true)
{
filled = false;
cout << endl <<"Flag is set off!" << endl << endl;
}
else
{filled = true;
cout << endl << "Flag is set on!" << endl << endl;
}


}

//-------------------------------------------------------------------------------------
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();

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(); // swap!
cout << "\n" << "The flipped pattern is:" << endl << endl;
rect1.draw_rect(); // call teh flipped Height and wWight

Rectangle rect2(4, 13, char(1), true); // Using overloaded Constructor
cout << "\n" << "A random pattern is: " << endl << endl;
rect2.draw_rect();

rect2.toggle();
rect2.draw_rect();
cout << "\n";

system("pause");
return 0;

}

The above is SOOOOOOO much easier to read.

If the bonus would include extra work, you can make another constructor that asks for the width and height and will automatically give a default

char = 'X';   //and
filled = false;

// and a constructor of two arguments
Rectangle key(4,5);      // width, height respectably

Thank you for your help but I guess the extra work is not focused on defining constructor. He said that:

Me: Oh you mean the part 5 for toggling needs to be modified to have a complete bonus mark?

The teacher: yup, and you shouldn't be printing anything in there.


And I eliminated if in toggle function as follows but not sure about other issues!

void Rectangle::toggle()
{
     
   filled = (! filled);
    
}

at the main:

rect2.toggle();
       cout << endl << "<<Toggling>>" << endl << endl;
       rect2.draw_rect();
       cout << "\n";

Your logic is flawed in draw_rect(), for how you decide whether to draw a filled rect or a hollow rect. If you can say it to yourself clearly, you can get it right without any additional help.

Also, consider using your line_maker function for the empty space inside a non-filled rectangle:

cout << ch << line_maker([something]) << ch << '\n';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.