Your problem is on line 75:
Rectangle test();
This does not create a Rectangle object, it is a
function prototype which declares a function named 'test' that returns a rectangle.
Get rid of those parenthesis:
Rectangle test; // this will work
As for this question....
however, i want to be able to ask the user to input the height and width - and i've tried to do this within the constructor. is that the correct place i should be doing it? |
No, that's not the right place to do it. At least not in the default constructor.
Building user input into a constructor for simple types is almost always a bad idea. The reason is because it assumes too much and therefore severely limits what the class can do.
Ask yourself what you would do if you wanted to get a rectangle from a file? Or if you wanted to build a rectangle that was a modified version of an existing rectangle? These rectangles would not need to get width/height from the user... however because you put that in your default ctor, they would still do it.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
// we want 1 rectangle 'a' that gets dims from user
// we want another rectangle 'b' that is 'x' times as wide as rectangle 'a'
// where 'x' is a scale factor provided by the user:
Rectangle a; // get rectangle dims from user
double scale;
cout << "Please advise scale value for 2nd rectangle: ";
cin >> scale;
// now we create 'b'
Rectangle b;
b.width = a.width * scale;
b.height = a.height;
|
The above code seems like it would work, right? But the problem is 'b's ctor is still asking the user for input even though it's unnecessary.
Good OOP keeps the class as "blind" as possible about where other information is coming from. This keeps it flexible. When you start getting too specific and assuming too much about where your data is coming from, you build yourself into this tiny box and your code has a hard time working outside that box.