classes

we are learning about classes in my c++ class and we are currently assigned to work on a class that will basically output the area of two rectangles. We are required to set the initial height and width both to 0.0. Also, we must set height = h and width = w. I think the code will speak for itself:

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
Header:
#ifndef RECTANGLE_H
#define RECTANGLE_H

class rectangle
{
	float height;
	float width;
	float area;

public:
	//bool operator==(const rectangle&);
	friend istream &operator>>(istream &, rectangle&);
	friend ostream &operator<<(ostream &, const rectangle&);
	rectangle();
	rectangle(float h, float w);
   // rectangle (rectangle &r);
	float getarea(float, float);
	float getrectangle();
	

};
#endif





Function Implementation:
#include <iostream>
using namespace std;

#include "rectangle.h"

rectangle::rectangle()
{
	height = 0.00;
	width = 0.00;
	
}

rectangle::rectangle(float h, float w)
{
	height = h;
	width = w;
}

//rectangle::rectangle(rectangle &r)
//{
//	height = r.height;
//	width = r.width;
	
//}

float rectangle::getarea(float h, float w)
{
	area = width * height;
	return area;
}





//bool rectangle::operator==(const rectangle &r)
//{
	//if ()
		
	//	return false;
	//else 
		
	//	return true;
	
//}

istream &operator>>(istream &is, rectangle&r)
{
	cout << "Enter the height and width." << endl;
	is >> r.height >> r.width;
	return is;
	

}

ostream &operator<<(ostream &os, const rectangle&r)
{
	os << "The area is: " << r.area << endl;
	return os;

}





Test Program:
#include <iostream>
using namespace std;

#include "rectangle.h"

int main()
{
	
	rectangle r1, r2(5.0, 5.0);
	cin >> r1;
	cout << r1;
	cout << r2;
	

	//if(r1 == r2)
		//cout << "The rectangles are equal." << endl;
	//else
		//cout << "The rectangles are not equal." << endl;
	
	return 0;
}




Ignore the commented code....That is something I am working on adding on to it. The end result is that we are supposed to compare two rectangles through an overloaded "==" function and output if they are equal or not. However, before I add the extras, there seems to be a problem with initialization somewhere involving the height and width. The program compiles but the area comes out to a strange exponential number. thanx.


why passing parameters to
1
2
3
4
5
float rectangle::getarea(float h, float w)
{
	area = width * height;
	return area;
}



you have not assigned the value to area variable .

1
2
3
4
5
6
ostream &operator<<(ostream &os, const rectangle&r)
{
	os << "The area is: " << r.area << endl;
	return os;

}


directly printing the value of area which will be 0 or uninitialized .
why isnt area initialized when i do the calculation "area = width * height"? Im not sure I understand.
You don't do the calculation area = width * height in your constructor, so when you build the object, area is still unset.

You can change this by changing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

rectangle::rectangle(float h, float w)
{
	height = h;
	width = w;
}

//rectangle::rectangle(rectangle &r)
//{
//	height = r.height;
//	width = r.width;
	
//}

float rectangle::getarea(float h, float w)
{
	area = width * height;
	return area;
}

to

1
2
3
4
5
6
rectangle::rectangle(float h, float w)
{
	height = h;
	width = w;
	area = width * height;
}


It is also worth changing the default constructor to include a default area.
1
2
3
4
5
6
rectangle::rectangle()
{
	height = 0.00;
	width = 0.00;
        area = 0.00;
}


now in your main, whichever constructor was called, area will be defined. Before area was only defined when getarea was called (and you never called it).

Does that help?
yes i understand now thank you. now the only problem is that i enter two numbers....for the first object in the main.....and the area outputs zero all the time. the second object (r2(5.00, 5.00)) calculates perfectly but r1 outputs zero. I am guessing that it is because of setting area to zero in the constructor?

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
#include <iostream>
using namespace std;

#include "rectangle.h"


rectangle::rectangle()
{
	height = 0.00;
	width = 0.00;
	area = 0.00;
	
}

rectangle::rectangle(float h, float w)
{
	height = h;
	width = w;
	area = height * width;	
}

rectangle::rectangle(rectangle &r)
{
	height = r.height;
	width = r.width;
	area = r.height * r.width;
}

float rectangle::getarea()
{
	area = height * width;
	return area;
}

//float rectangle::getrectangle()
//{
	
//}

//bool rectangle::operator==(const rectangle &r)
//{
	//if ()
		
	//	return false;
	//else 
		
	//	return true;
	
//}

istream &operator>>(istream &is, rectangle&r)
{
	cout << "Enter the height and width." << endl;
	is >> r.height >> r.width;
	return is;
	

}

ostream &operator<<(ostream &os, const rectangle&r)
{
	os << "\nThe height is: " << r.height << "\nThe width is: " << r.width << endl;
	os << "\nThe area is: " << r.area << endl;
	return os;

}



of course if i cin the area in the main, then it will display the value that i put in. But that defeats the purpose really.
actually I would recommend you remove area completely as member variable and always calculate it "on the fly" in getarea.

Then you don't print out r.area in your cout<< statement, but call to r.getarea(). Sounds better to me.

1
2
3
4
5
6
7
8
9
10
11
...
class rectangle
{
	float height;
	float width;
	//float area;  <-- don't store it as class variable
...
	float getarea() const { return height*width; }
};
...
    os << "The area is: " << r.getarea() << endl;


Ciao, Imi.
Last edited on
I agree with imi. More importantly though make sure you do remove area from the constructor. The constructor is not there to perform calculations, it is there to initialize your object.
thank you imi and mcleano....it works perfectly now. thanks to everyone.
Topic archived. No new replies allowed.