Simple class question!!

I'm just starting out, and have attempted an extremely simple example of creating a class, with constructor, destructor, and a member function to calculate the area of a rectangle.

The program worked just fine when i made the constructor the following:
1
2
3
4
5
6
7
8
9
10
11
12



Rectangle::Rectangle (int w, int h) 

{

width = w; 
height = h; 

}


and then in the main i typed something like:

1
2
Rectangle test(5,7);
cout << "The area is: " << test.Area();



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? i know i could do this in the main, but i'd like to know how to do this outside of main, if that makes sense. Here is my attempt, which fails.....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

#include "stdafx.h"
#include <cstdlib>

using namespace std;

class Rectangle

{

public:

Rectangle (void); // The constructor

int Area(void); // The area

~Rectangle (void); // The destructor

private:

int width;
int height;

};





Rectangle::Rectangle (void)

{

int w, h;

cout << "Please enter the width: ";
cin >> h;

cout << endl << "Please enter the height: ";
cin >> w;

cout << endl;

height = h;
width = w;

}


int Rectangle::Area (void)

{

return (width * height);

}

Rectangle::~Rectangle (void)

{

cout << "Testing the goodbye!!";
// This destructor is empty

}





int _tmain(int argc, _TCHAR* argv[])
{

	
	Rectangle test();

	cout << "Area of rectangle is: " << test.Area() << endl;
	
	return 0;
}



hmmm..... as i've typed this, i think i've realised that the constructor is not the place to be trying to do all the user input stuff!! (as i said, i'm a very early beginner in OOP).
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.
Excellent advice, thanks.
Topic archived. No new replies allowed.