Area and volume of rectangle using classes C++

Bug!

Guys, programming is 90% debugging, and 10% writing bugs, I have been debugging this code for nearly 3 hours. I have a question down below after the code.

Brief info about code:
I used default constructor, initialization constructor (doesn't work, but no bugs). I also integrated setters and getters including creating an object in main function to access the member functions of public class.

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
118
119
120
121
122
123
124
125
126
127

#include <iostream>
#include <cassert>
using namespace std;

class other{

	private:
	        double width;
		double length;
		double height;
		double area;
		double volume;

	public: 
		// default constructor:
		other()
		:width(0), length(0), height(0), area(0), volume(0)
		{}

		// initialization constructor:
		other(double x, double y, double z)
		:width(x), length(y), height(z)
		{
			area = width * length;
			volume = length * width * height;
		}

		// Setters:
		void set_width(double w);
		void set_length(double l);
		void set_height(double h);
		void set_area(double &saved_area);
		void set_volume(double &saved_area);

		// Getters:
		double get_width(void);
		double get_length(void);
		double get_height(void);
		double get_area(void);
		double get_volume(void);

		~other(){
			cout << "Constructor is destroyed! " << endl;
		}
};

// Having the member functions outside of the public class.
// Setters:
void other::set_width(double w){
	width = w;
}
void other::set_length(double l){
	length = l;
}
void other::set_height(double h){
	height = h;
}
void other::set_area(double &saved_area){
	area = saved_area;
}
void other::set_volume(double &saved_area){
    volume = saved_area;
}


// Getters:
double other::get_width(){
	return width;
}
double other::get_length(){
	return length;
}
double other::get_height(){
	return height;
}
double other::get_area(){
	return area;
}
double other::get_volume(){
	return volume;
}


void test1(other& use){

	cout << "Width is: ";
	double x;
	x = use.get_width();
	cout << x << endl;

	cout << "Length is: ";
	double y = use.get_length();
	cout << y << endl;

	cout << "Height is: ";
	double w = use.get_height();
	cout << w << endl;
	
	cout << "Area is: ";
	double q = use.get_area();
	cout << q << endl;

	cout << "Volume is: ";
	double r = use.get_volume();
	cout << r << endl;
}


int main(){

    other use;
    use.set_width(5.0);
    use.set_length(10.0);
    use.set_height(2.0);

    double save_area = use.get_area();
    use.set_area(save_area);

    double save_volume = use.get_volume();
    use.set_volume(save_volume);

	test1(use);


	return 0;
}
Last edited on
Possible solution to fix the above code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){

    other use;
    use.set_width(5.0);
    use.set_length(10.0);
    use.set_height(2.0);

    double save_area =(use.get_length()) * (use.get_width()); 
    use.set_area(save_area);

    double save_volume = use.get_length() * use.get_width() * use.get_height(); 
    use.set_volume(save_volume);

	test1(use);
	return 0;
}



My Question now is: How can I fix this problem using the initialization constructor or parametrized constructor ?
Last edited on
Make your getters const, so that a const variable of type other can call them.

1
2
void set_area(double &saved_area);
void set_volume(double &saved_area);

Why pass by reference when in all your other functions you pass by value?
Try to keep your code consistent.

 
void test1(other& use)

Prefer to pass by const reference when you aren't modifying the object in the function. Note that your getters need to be const functions.

 
double save_area = use.get_area();

but get_area() just returns the member variable area, which isn't initialised. You also don't calculate the area, which is why you get a garbage value.
1
2
3
4
5
6
7
other use;
use.set_width( 5.0 );
use.set_length( 10.0 );
double save_area = -3.14;
use.set_area( save_area );

assert( (use.get_length() * use.get_width()) == use.get_area() ); // why this fails? 

The properties 'area' and 'volume' of class other are functions of properties length, width, and height, but allow the user to change them independently and without checking. This leads to logical inconsistencies.

If you want to let the user to change the area, then the set_area() has to update length, width, and volume too to keep the other consistent.

The other does not need member variables for area and volume; the computations are quite cheap. Without those members you don't have to keep them up to date.
Topic archived. No new replies allowed.