-2

I am learning about overloaded functions in c++, and one of the examples in the book will not work. Here is the code,

#include <iostream>
using namespace std;
//rectangle class declaration
class Rectangle
{
public:
    //constructors
    Rectangle(int width, int height);
    ~Rectangle(){}

    //overload class function drawShape
    void DrawShape() const;
    void DrawShape(int aWidth, int aHeight) const;

private:
    int itsWidth;
    int itsHeight;
};

//constructor implementation
Rectangle::Rectangle(int width, int height)
{
    itsWidth;
    itsHeight;
}


//overloaded DrawShape - takes no values
//Draws based on current class member values
void Rectangle::DrawShape() const
{
    DrawShape(itsWidth, itsHeight);
}


//overloaded DrawShape - takes two values
//Draws shape based on the parameters
void Rectangle::DrawShape(int width, int height) const
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            cout << "*";
        }
        cout << "\n";
    }
}

//Driver program to demonstrate overloaded functions
int main()
{
    //Initialize a rectangle to 30, 5
    Rectangle theRect(30, 5);
    cout << "DrawShape(): \n";
    theRect.DrawShape();
    cout << "\nDrawShape(40, 2): \n";
    theRect.DrawShape(40, 2);
    system("pause");
    return 0;
}

What I am having trouble with is the initialization of the Rectangle theRect to 30, 5 does not output anything, yet I know the function works because it does output the 40, 2 rectangle.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218

2 Answers2

2

Your constructor doesn't do anything, it should be

//constructor implementation
Rectangle::Rectangle(int width, int height)
{
    itsWidth = width;
    itsHeight = height;
}

And since DrawShape is a function in the class Rectangle it probably shouldn't take any parameters, it should use the object's member variables

void Rectangle::DrawShape() const
{
    for (int i = 0; i < itsHeight; i++)
    {
        for (int j = 0; j < itsWidth; j++)
        {
            cout << "*";
        }
        cout << "\n";
    }
}

Note there is an alternative (and arguably preferred) syntax for your constructor that uses a member initialization list

//constructor implementation
Rectangle::Rectangle(int width, int height)
: itsWidth{width}, itsHeight{height} {}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • I changed my code to reflect yours and I am still getting the same issue, it doesn't draw the first rectangle when initialized to 30,5 – Kevin W. Feb 28 '15 at 22:11
1

your constructor is not initialising anything, it should be like this:

//constructor implementation
Rectangle::Rectangle(int width, int height)
    : itsWidth(width)
    , itsHeight(height)
{
}

after the change, your result look like this:

DrawShape(): 
******************************
******************************
******************************
******************************
******************************

DrawShape(40, 2): 
****************************************
****************************************
swang
  • 5,157
  • 5
  • 33
  • 55
  • Thanks, I changed it but I am still having the same issue. – Kevin W. Feb 28 '15 at 22:22
  • it works fine for me, i compiled and run it with clang600 on mac, are you sure the above is the exact code you are working with? – swang Feb 28 '15 at 22:27
  • Yes, I am using visual studio 2013, and the code was a direct copy and paste from here using both your code and cyber's – Kevin W. Feb 28 '15 at 22:32