Keep getting 0 for the area of a rectangle HELP!

Everything is working fine and the program runs, the only issue is I keep getting 0 for the area when it does the calculation for rectangle. Can someone point me in the right direction on why that is?

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  //main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Shape.h"
#include "Rectangle.h"

using namespace std;



int main( )
 {
    double radius = 0;
    double length = 0;
    double width = 0;
    Square square;
    Circle circle;
    Rectangle rectangle;


    //When user puts in 1 this will ask for the radius of the circle then it will call the function for the calculation.

     cout<<"Enter the radius of the circle : " ;
     cin>> radius;
     circle.setRadius(radius);
     cout<< "\nArea of the circle is "<<circle.getArea();



    //When user puts in 2 this will ask for the length of the side of the square then it will call the function for the calculation.


     cout<< "\nEnter length of one side of the square : "<<endl;
     cin >> length;
     square.setLength(length);
     cout<<"\nArea of the square is "<<square.getArea();


     cout<< "\nEnter length of one side of the rectangle : "<<endl;
     cin >> length;
     square.setLength(length);
     cout<< "\nEnter width of one side of the rectangle  : "<<endl;
     cin>> width;
     rectangle.setWidth(width);
     cout<<"\nArea of the rectangle is "<<rectangle.getArea();



 }

//Circle.cpp

#include "Circle.h"
#include <iostream>


using namespace std;
// Makes the Calculation for the user and outputs it.
Circle::Circle (double r)
    {

		radius = r;

	}

	double Circle::getRadius()const {
	return radius;
	}
    void Circle::setRadius (double r){
    radius = r;
    }
    double Circle::getArea()const{
    return radius * radius * 3.1415;
    }

//Circle.h

#define CIRCLE_H_INCLUDED
#include <iostream>
#include "Shape.h"

using namespace std;


	class Circle {

// Gets called and stores the input from user.

    public:

    Circle (double radius =0);

    double getRadius()const;
    void setRadius(double radius);
    double getArea ()const;

    private:

    double radius;


	};




//rectangle.cpp

#include "Rectangle.h"
#include <iostream>


using namespace std;

Rectangle::Rectangle(double len, double wid)
{

		length = len;
		width = wid;

	}
    double Rectangle::getLength() {
	return length;
	}
	void Rectangle::setLength(double len){
    length = len;
    }
    double Rectangle::getWidth() {
	return width;
	}
	void Rectangle::setWidth(double wid){
    width= wid;
    }
    double Rectangle::getArea(){
    return length * width;
    }

//Rectangle.h

#define RECTANGLE_H_INCLUDED
#include <iostream>
#include "Shape.h"


	class Rectangle : public Shape

	{
		public:

			Rectangle (double length =0, double width =0);

			double getLength();
			double getWidth();
		    void setLength(double length);
		    void setWidth(double width);
			virtual double getArea ();


        private:

            double length;
            double width;

	};




//shape.cpp

#include "shape.h"



Shape::Shape(){
	area = 0;

}

double Shape::getArea(){
	return area;
}


//Shape.h

#ifndef SHAPE_H
#define SHAPE_H
#include <string>



		class Shape{	// Base Class

	public:
		Shape();
		double getArea();

	private:
		double area;

};

#endif





//square.cpp

#include "Square.h"
#include <iostream>

using namespace std;
// Makes the Calculation for the user and outputs it.
Square::Square(double len)
{

    length = len;

}
    double Square::getLength()const {
	return length;
	}
	 void Square::setLength (double len){
    length = len;
    }
	 double Square::getArea()const{
    return length * length;
    }


//square.h

#define SQUARE_H
#include <iostream>
#include "Shape.h"


using namespace std;

    // Gets called and stores the input from user.
	class Square {


    public:

    Square (double length =0);

    double getLength()const;
    void setLength(double length);
    double getArea ()const;

    private:

    double length;

};


1
2
3
cout<< "\nEnter length of one side of the rectangle : "<<endl;
cin >> length;
square.setLength(length);
Wow.. I looked at this 100 times and missed that. Thanks!
You could have debugged it yourself by logging the values of interest:
1
2
3
4
5
6
double Rectangle::getArea()
{
    cout << "Rectangle::getArea() called. \n";
    cout << "Length: "<< length << "   Width: " << width << '\n';
    return length * width;
}

Thirty seconds to type and then you would have known for sure what was happening. If you're getting a value in your code and you don't know why, find where it comes from and start logging out the variables that make it until you see the mistake.
Your indentation could use some work. I suggest always using spaces instead of tabs (you should be able to set your editor to insert spaces instead of tabs for indentation. Four spaces is average these days.

The base class Shape is not being used properly. The proper use of the base class in this situation is to define the abstract interface of the Shape objects (with virtual functions), Something like this:
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
#include <iostream>
#include <vector>
using namespace std;

class Shape
{
public:
    virtual double area() = 0;
};

class Rectangle : public Shape
{
    double width, height;
public:
    Rectangle(double w, double h) : width(w), height(h) { }
    double area() { return width * height; }
};

class Square : public Rectangle
{
public:
    Square(double side) : Rectangle(side, side) { }
};

class Circle : public Shape
{
    double radius;
public:
    constexpr static double Pi = 3.14159265359;
    Circle(double radius) : radius(radius) { }
    double area() { return radius * radius * Pi; }
};

int main()
{
    // You can access the virtual functions of the derived classes
    // through a pointer to the base class.
    vector<Shape*> v;
    v.push_back(new Rectangle(5, 10));
    v.push_back(new Circle(10));
    v.push_back(new Square(15));
    
    for (Shape* shape: v)
        cout << shape->area() << '\n';
}

Last edited on
Topic archived. No new replies allowed.