Too much output & switching of name?

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
/*
 * room.cpp
 *
 *  Created on: 21/10/2010
 *      Author: User
*/
#include <iostream>
#include <string>
using namespace std;
//declaration section
class room
{
private:
	int rmLength;
	int rmWidth;
	string colour;
public:
	room (int = 5, int = 4, string = "green");
	int calArea ();
	void printArea ();
	~ room ();
};
room::room(int rmL, int rmW, string col)
{
	rmLength = rmL;
	rmWidth = rmW;
	colour = col;
}
void room::printArea()
{
	cout << "Area of Room A is " << calArea() << " come in " << colour << " colour" << endl;
	}
int room::calArea()
{
	int calArea;
	calArea = rmLength * rmWidth;
	return calArea;
}
room::~room()
{
	cout << "Room Destroyed!" << endl;
}
int main ()
{
	room rooma, roomb (4, 4, "white");
	rooma.printArea();
	roomb.printArea();
	return 0;
}


Basically it's a simple console program that will print out the room area & colour.

1
2
3
4
Area of Room A is 20 come in green colour
Area of Room A is 16 come in white colour
Room Destroyed!
Room Destroyed!


However, I would like the room name to change to Room B for the second line.
Also my destructor seem to be printing out two times. Anyway to resolve this.

Much Appreciated.





However, I would like the room name to change to Room B for the second line.

Then you can't hard code the name "A" into the program. Have each room object hold a "name" variable and have printArea() print the variable.

Also my destructor seem to be printing out two times. Anyway to resolve this.

You created two room objects. The destructor is called whenever the object is destroyed. Two objects are being destroyed, hence two destructor calls.
Last edited on
// Well as for the making Room B, couldn't you make say a char roomLetter = 'A';
and then change roomLetter = 'B'; at a certain point in there?
// It would definitely have to be something like cout << "Area of Room " << roomLetter << " is " << calArea() right? That's what filipe meant by not hard-coding it I believe.

// What filipe said also makes sense and what I was sort of wondering. Though shouldn't it destruct after each Room output? Instead of having two "Room Destroyed!" next to each other, should it be in between? Totally unsure!
Last edited on
No. Objects allocated on the stack are destroyed when they go out of scope. In the above case, they only go out of scope when the program ends.
Hi filipe, thanks for the valuable comment.
Below is the updated codes.

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
/*
 * room.cpp
 *
 *  Created on: 21/10/2010
 *      Author: Alpha
*/
#include <iostream>
#include <string>
using namespace std;
//declaration section
class room
{
private:
	int rmLength;
	int rmWidth;
	string colour;
	string rmName;
public:
	room (int = 5, int = 4, string = "green", string = "A");
	int calArea ();
	void printArea ();
	~ room ();
};
room::room(int rmL, int rmW, string col, string rmN)
{
	rmLength = rmL;
	rmWidth = rmW;
	colour = col;
	rmName = rmN;
}
void room::printArea()
{
	cout << "Area of Room " << rmName << " is " << calArea() << " come in " << colour << " colour" << endl;
}
int room::calArea()
{
	int calArea;
	calArea = rmLength * rmWidth;
	return calArea;
}
room::~room()
{
	cout << "Room Destroyed!" << endl;
}
int main ()
{
	room rooma, roomb (4, 4, "white", "B");
	rooma.printArea();
	roomb.printArea();
	return 0;
}
Topic archived. No new replies allowed.