Calculate Area of Circle, Square, Rectangle Using Headers Files

I added the shape Rectangle to my code that was previously working correctly. Now I am receiving errors

||=== Build: Debug in Final Area (compiler: GNU GCC Compiler) ===|
main.cpp|6|error: expected nested-name-specifier before 'namespace'|
main.cpp|65|error: expected '}' at end of input|
main.cpp||In member function 'int Rectangle::main()':|
main.cpp|65|warning: no return statement in function returning non-void [-Wreturn-type]|
main.cpp|65|error: expected unqualified-id at end of input|
||=== Build failed: 3 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

When I remove Rectangle it works again. I have monkeyed around with it for hours. Need new eyes on it please.

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
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Rectangle.h"

using namespace std;


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


do
    { // Starting of while loop
       cout << endl << "_______________Calculate Area______________" << endl;
       cout << "Select an Object" << endl;
       cout << " 1: Circle" << endl;
       cout << " 2: Square" << endl;
       cout << " 3: Rectangle" <<endl;
       cout << " 0: Exit" << endl;
       cout << " Enter Your Choice: ";
       cin  >> menuOption;

      switch(menuOption)
{

case 1:
    {
     cout<< "Enter the radius : " ;
     cin>> radius;
     circle.setRadius(radius);
     cout<< "Area of the circle is "<<circle.getArea();
     break;
    }
case 2:
    {
     cout<< "Enter length of one side of the square : "<<endl;
     cin >> length;
     square.setLength(length);
     cout<<"Area of the square is "<<square.getArea();
     }
case 3:
    {
     cout<< "Enter length of one side of the rectangle : "<<endl;
     cin >> length;
     square.setLength(length);
     cout<< "Enter width of one side of the rectangle  : "<<endl;
     cin>> width;
     rectangle.setWidth(width);
     cout<<"Area of the rectangle is "<<rectangle.getArea();
     }
}

}
   while(menuOption!=0);

   cout<<" ________________ THANK YOU ___________________" <<endl;
 }


//Circle.h
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include <iostream>

using namespace std;


	class Circle {



		public:

		    Circle (double radius =0);

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

        private:

            double radius;


	};

	#endif // CIRCLE_H_INCLUDED


//Circle.cpp

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


using namespace std;

Circle::Circle (double r)
    {

		radius = r;

	}

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

//Square.h

#ifndef SQUARE_H
#define SQUARE_H
#include <iostream>



using namespace std;

              // variables can be initialized during declaration
	class Square {


		public:

			Square (double length =0);

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

        private:

            double length;

	};
	#endif // SQUARE_H

//Square.cpp

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

using namespace std;

Square::Square(double len)
{

		length = len;

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


//Rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include <iostream>



using namespace std;

              // variables can be initialized during declaration
	class Rectangle {


		public:

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

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


        private:

            double length;
            double width;


#endif // RECTANGLE_H_INCLUDED


//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 is missing a very important };
Thank you.
I added the base class Shape. I am not sure about the inherence. Every change I make gives errors.
The other issue is the Rectangle is not calculating the area. However, the Circle and Square is calculating correctly.

The good news is there are no errors on the code as is but I am not sure about the inherence.

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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//main

#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Rectangle.h"
#include "shape.h"
using namespace std;


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

    int menuOption;


do
    { // Starting of while loop
       cout << endl << "_______________Calculate Area______________" << endl;
       cout << "Select an Object" << endl;
       cout << " 1: Circle" << endl;
       cout << " 2: Square" << endl;
       cout << " 3: Rectangle" <<endl;
       cout << " 0: Exit" << endl;
       cout << " Enter Your Choice: ";
       cin  >> menuOption;

      switch(menuOption)
{

case 1:
    {
     cout<< "Enter the radius : " ;
     cin>> radius;
     circle.setRadius(radius);
     cout<< "Area of the circle is "<<circle.getArea();
     break;
    }
case 2:
    {
     cout<< "Enter length of one side of the square : "<<endl;
     cin >> length;
     square.setLength(length);
     cout<<"Area of the square is "<<square.getArea();
     break;
     }
case 3:
    {
     cout<< "Enter length of one side of the rectangle : "<<endl;
     cin >> length;
     square.setLength(length);
     cout<< "Enter width of one side of the rectangle  : "<<endl;
     cin>> width;
     rectangle.setWidth(width);
     cout<<"Area of the rectangle is "<<rectangle.getArea();
     break;
     }
}

}
   while(menuOption!=0);

   cout<<" ________________ THANK YOU ___________________" <<endl;
 }

//circle.h

#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "shape.h"




	class Circle : public Shape

	{
		public:

		    Circle (double radius =0);

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

        private:

            double radius;


	};
#endif // CIRCLE_H_INCLUDED


//circle.cpp

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


using namespace std;

Circle::Circle (double r)
    {

		radius = r;

	}

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

//Square.h

#ifndef SQUARE_H
#define SQUARE_H
#include "shape.h"


using namespace std;


	class Square : public Shape

	{
		public:

			Square (double length =0);

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

        private:

            double length;

	};
	#endif // SQUARE_H


//Square.cpp

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

using namespace std;

Square::Square(double len)
{

		length = len;

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

//Rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#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;

	};
#endif // RECTANGLE_H_INCLUDED


//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;
    }


//Shape.h

#ifndef SHAPE_H
#define SHAPE_H
#include <string>



		class Shape{	// Base Class

	public:
		Shape();
		double getArea();

	private:
		double area;

};

#endif // SHAPE_H_INCLUDED


//Shape.cpp

#include "shape.h"



Shape::Shape(){
	area = 0;

}

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



Here is the lesson instructions:

Add a new Shape base class to the area calculation program that includes data members common to all shapes (such as a shape ID, a shape type, and a unit of measure). The Shape base class should also include a virtual getArea() member function.
• Revise the Circle and Square classes so that they inherit from the Shape base class.
• Add a third shape to the program that also inherits from the Shape class. The finished program should ask the user to enter the relevant information for each shape and then print the area and other information for each shape.
Shape in this case needs to be an abstract class with no implementation:
1
2
3
4
5
6
7
8
9
10
11
//Shape.h

#ifndef SHAPE_H
#define SHAPE_H

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

#endif // SHAPE_H_INCLUDED 


In my opinion, Shape would be a bad name for the class as it is. The only function it has is getArea. I can't think of a better English name to describe something that has an area. But after more Shape type stuff is added it will probably be the best name.

http://en.cppreference.com/w/cpp/language/virtual
Topic archived. No new replies allowed.