classes to inherit from the Shape base class

What I am attempting to do is have the classes to inherit from the Shape base class. I am receiving errors for the rectangle class. I missed something. Also the debugger on my codeblocks is not working and I cannot figure out how to fix it. 1. Does that affect the compiler? 2. Can you please look at the rectangle class and tell me what I missed? 3. I am not sure if I coded correctly for the classes to inherit from the Shape base class. Any feedback would be great. Here are the errors:

||=== Build: Debug in IT-FP3348_Area (compiler: GNU GCC Compiler) ===|
Rectangle.cpp|18|error: prototype for 'double Rectangle::getlength() const' does not match any in class 'Rectangle'|
Rectangle.h|25|error: candidate is: double Rectangle::getlength()|
Rectangle.cpp|19|error: prototype for 'double Rectangle::getwidth() const' does not match any in class 'Rectangle'|
Rectangle.h|26|error: candidate is: double Rectangle::getwidth()|
Rectangle.cpp|19|error: expected unqualified-id before ')' token|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
  #include <iostream>
#include <string>
#include "Circle.h"  //Defines Circle
#include "Square.h"  //Defines Square
#include "Rectangle.h" //Defines Rectangle



int main()
{   // Start main function
using namespace std;

 int menuOption;




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


      switch(menuOption)

{

case 1:
{
    double radius;
    cout << "\nEnter the radius in inches. ";
    cin >> radius;
    string name = "myCircle";
    string unit = "in";
    Circle C(name, radius, unit);
    cout << "\nThe area of the circle is " << C.getArea() << " inches." << endl;
    break;
}

case 2:
{
    double length;
    cout << "\nEnter the length of one side in inches. ";
    cin >> length;
    string name = "mySquare";
    string unit = "in";
    Square S(name,length,unit);
    cout << "\nThe area of the square is " << S.getArea() << " inches." << endl;
    break;
}

case 3:

    {
    double length;
    double width;

        cout << "\nEnter the length of one side in inches. ";
        cin >> length;
        cout << "\nEnter the width of one side in inches. ";
        cin >> width;
        string name = "myRectangle";
        string unit = "in";
        Rectangle R(name,length, width, unit);
        cout << "\nThe area of the rectangle is " << R.getArea() << " inches." << endl;
    }
}


}
   while(menuOption!=0);

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

    return 0;
}   // This ends the main function

//circle.cpp

#include "Circle.h"
using namespace std;

const double PI = 3.14159;

//Set radius
 Circle::Circle (string &name, double r, string &unit) :
    Shape(name, "Circle", unit)
    {
        radius = r;
    }

//calculate area

    double Circle::getradius() { return radius; }

    double Circle::getArea() const
    {
        return (PI * (radius * radius));
    }

    void Circle::setradius(double r) { radius = r; }

//circle.h

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

class Circle : public Shape      //Class Circle inherits from Base Class Shape
{
//Member declarations
    private:

        double radius;

// Constructors
    public:

        Circle(std::string &, double, std::string &);

// Parent Virtual function

        double getArea() const;

//Function prototypes

        double getradius();     //Get the Radius (getter)
        void setradius(double);   //Sets Radius (setter)
};



#endif // CIRCLE_H_INCLUDED


//square.cpp

#include "Square.h"
using namespace std;

//Set length
Square::Square(string &name, double len, string &unit) :
    Shape(name, "Square", unit)
    {
        length = len;
    }

//Calculate Area
    double Square::getArea() const
    {
        return length * length;
    }

    double Square::getLength() const { return length; }

    void Square::setLength(double len) { side = len; }

//square.h

#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include "Shape.h"

class Square : public Shape   //Class Square inherits from Base Class Shape
{

//Member declarations
    private:

        double length;

//Constructors

    public:

        Square(std::string &, double, std::string &);

//Parent Virtual function

        double getArea() const;

//Function prototypes

        double getside() const;     //Get length (getter)
        void setside(double);      //Set length (setter)
};

#endif // SQUARE_H_INCLUDED

//rectangle.cpp

#include "Rectangle.h"
using namespace std;

//Set length, width
Rectangle::Rectangle(string &name, double len, double wid, string &unit) :
    Shape(name, "Rectangle", unit)
    {
        length = len;
        width = wid;
    }

//Calculate Area
    double Rectangle::getArea() const
    {
        return length * width;
    }

    double Rectangle::getlength() const { return length;}
    double Rectangle::getwidth() const (return width;)

    void Rectangle::setlength(double len) { length = len;}
    void Rectangle::setwidth(double wid) {width = wid;}


//rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"

class Rectangle : public Shape      //Class Rectangle inherits from Base Class Shape
{
//Member declarations
    private:

        double length;
        double width;

//Constructors

    public:

        Rectangle(std::string &, double,double, std::string &);

// Parent Virtual function

        double getArea() const;

//Function prototypes

        double getlength(); //Get length
        double getwidth(); //Get width

        void setlength(double); //Set length
        void setwidth(double); //Set width
};




#endif // RECTANGLE_H_INCLUDED

//shape.cpp

#include "Shape.h"
#include <string>

Shape::Shape() {}

Shape::Shape(std::string &name, const std::string &shape, std::string &unit)
{
    shapeID = name;
    shapeName = shape;
    unitOfMeasure = unit;
}
    std::string Shape::getShapeID() const
    { return shapeID; }

    std::string Shape::getShapeName() const
    { return shapeName; }

    std::string Shape::getUnitOfMeasure() const
    { return unitOfMeasure; }

    void Shape::setShapeID(std::string &name)
    { shapeID = name; }

    void Shape::setShapeName(std::string &shape)
    { shapeName = shape; }

    void Shape::setUnitOfMeasure(std::string &unit)
    { unitOfMeasure = unit; }


//shape.h

#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <string>

class Shape        //Class Name
{

//Member declarations
    private:

        std::string shapeID;        //name of instance
        std::string shapeName;      //Circle, Square, Rectangle
        std::string unitOfMeasure;  //Centimeters or Inches

//Member prototypes
    public:

        Shape();

// derived class constructor will pass a constant string to the base class' constructor.

        Shape(std::string &, const std::string &, std::string &);

//The "get" functions will not allowed to modify any variables in the class.

        std::string getShapeID() const;
        std::string getShapeName() const;
        std::string getUnitOfMeasure() const;

//Each derived class must provide its own getArea().

        virtual double getArea() const = 0;

        void setShapeID(std::string &);
        void setShapeName(std::string &);
        void setUnitOfMeasure(std::string&);
        void print();           //Prints the information
};

#endif // SHAPE_H_INCLUDED



Hi,

You need to be consistent with functions in the class declaration and the definition of the function. The reason is that a const function is like a kind of overload to a non const function.

The compiler gives a huge hint on why there is a problem:

Rectangle.h|25|error: candidate is: double Rectangle::getlength()|

That is a hint, but what you should really do is mark the function const in the class.


Thank you. I correct that error.

1
2
double getlength() const; //Get length
         double getwidth() const; //Get width 
Thank you so much.. I found a couple more mistakes and fixed those. It is working now.. Thank you sooooo much!
Topic archived. No new replies allowed.