calculating the area of a square, circle, and rectangle

Hello all,

I am having an issue with my inheritance problem that I need help with. Everything is working except for my rectangle solution. Every time i biuld and run I get these three errors.

Error: no 'double Rectangle::getLength() member function declared in class 'Rectangles'

And certain other problems with the rectangle, getWidth() and getWidth(double). I was hoping you all could help me with this problem and also tell me if I am using inheritance right. Below is my code sorry if it is a little long. I also put my code that I am needing help with at the bottom of the list. If anyone could help me I would greatly appreciate it. Thank You.

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
main.cpp
#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 option;

    cout << "Calculating the Area" << endl << endl;

do
{
    cout << "Pick a shape in which you would like the area of:" << endl;
    cout << "1: Square" << endl;
    cout << "2: Circle" << endl;
    cout << "3: Rectangle" << endl;
    cout << "4: Exit" << endl;
    cout << "Please enter your choice: ";
    cin >> option;

    switch(option)
    {
    case 1:
        {
            cout << endl;
            cout << "Please enter the length of one side of the square: " << endl;
            cin >> length;
            Square square(length);
            cout << "The area of the square is: " << square.getArea() << "\n\n";
            break;
        }
    case 2:
        {
            cout << endl;
            cout << "Please enter the radius of the circle: ";
            cin >> radius;
            circle.setRadius(radius);
            cout <<  "The area of the circle is: " << circle.getArea() << "\n\n";
            break;
        }
    case 3:
        {
            cout << endl;
            cout << "Please enter the length of one side of the rectangle: ";
            cin >> length;
            rectangle.setLength(length);
            cout << "Please enter the width of one side of the rectangle: ";
            cin >> width;
            rectangle.setWidth(width);
            cout << "The area of the rectangle is: " << rectangle.getArea();
        }
    }
}
while (option != 4);
    cout << "Bye!" << endl;

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
circle.cpp
#include <iostream>
#include "Circle.h"

using namespace std;

Circle::Circle(int r)
{
    radius = r;
}

int Circle::getRadius() const {
return radius;
}

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
square.cpp
#include <iostream>
#include "Square.h"

using namespace std;

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


1
2
3
4
5
6
7
8
9
10
11
shape.cpp
#include "shape.h"

Shape::Shape() {
    area = 0;
}

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
square.h
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include <iostream>
#include "shape.h"

using namespace std;
	class Square {
	    public:
	        Square (int length =0);
	        double getLength();
		    void setLength(int length);
			double getArea();

        private:
            double length;
    };
#endif // SQUARE_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
circle.h
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include <iostream>
#include "shape.h"

using namespace std;
    class Circle {
        public:
            Circle (int radius =0);
            int getRadius() const;
		    void setRadius(int radius);
			double getArea() const;

        private:
            int radius;
    };
#endif // CIRCLE_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
11
shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED

class Shape {
public:
    Shape();
    double getArea();
};

#endif // SHAPE_H_INCLUDED 


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
rectangle.cpp
#include <iostream>
#include "Rectangle.h"

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::getWidth(double wid) {
    width = wid;
}
double Rectangle::getArea() {
    return width * length;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 = 0;
    double getWidth = 0;
    void setLength(double length);
    void setWidth(double width);
    double getArea();
private:
    double length;
    double width;
};


#endif // RECTANGLE_H_INCLUDED

double getLength = 0;
double getWidth = 0;

those are variables. ^^^ Not functions.

that confuses things with

double Rectangle::getWidth() {
return width;

which is a function of the same name that isn't declared in the class but is scoped to it. So its several errors all at once, but the fix is to turn those 2 variables into function prototypes:

double getLength();
double getWidth();
Last edited on
Ok now I'm getting two errors on shape.cpp. They are:

definition of implicitly-declared 'Shape::Shape()'
area was not declared in this scope.

Please Help I have been workng on this since monday. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
shape.cpp
#include "shape.h"

Shape::Shape() {
    area = 0;
}

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


1
2
3
4
5
6
7
8
9
10
shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED

class Shape {
public:
    double getArea();
};

#endif // SHAPE_H_INCLUDED 

Hi all,

Can anybody please help me with this I can't seem to figure this out; and I have beat my head against the wall trying to figure this out.
The variable area was not initialized in your shape class.
If you want functions in your inherited classes to work differently than the functions in tha base class, remember to use the keyword virtual. This means that the declaration of the getArea function in your Shape class is not correct. What you should do instead is:
1
2
3
4
5
class Shape
{
public:
    virtual double getArea(); //This way, the getArea function in your derived classes overrides the implementation in your Shape class
}


For more information on the virtual keyword see http://en.cppreference.com/w/cpp/language/virtual

Also, I noticed something strange with your indents in the Circle and Square classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include <iostream>
#include "shape.h"

using namespace std;
    class Circle {
        public:
            Circle (int radius =0);
            int getRadius() const;
		    void setRadius(int radius);
			double getArea() const;

        private:
            int radius;
    };
#endif // CIRCLE_H_INCLUDED  

Doesn't it make much more sense to write:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include <iostream>
#include "shape.h"

using namespace std;
class Circle 
{
public:
    Circle (int radius =0);

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

private:
     int radius;
};
#endif // CIRCLE_H_INCLUDED  
Last edited on
Something I just noticed: you aren't inheriting anything at all. This is how inheritance works:
1
2
3
4
5
6
7
8
9
10
11
class Base
{
public:
   void doSomething() { std::cout << "I am a base!\n"; }
}

class Derived : public Base //This tells the program that Derived inherits from Base, and public means it can use all non-protected members of Base
{
public:
    void doSomething() { std::cout << "I am a derived\n!"; }
}

This creates two classes, a Base class and a Derived class.

If you have any more questions on inheritance, I recommend you to look at this page:

http://www.learncpp.com/cpp-tutorial/111-introduction-to-inheritance/

and further pages on this website.

Good luck!

Shape.h:
1
2
3
4
5
6
7
8
9
10
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED

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

#endif // SHAPE_H_INCLUDED 


Shape.cpp:
1
2
3
4
#include "Shape.h"

Shape::Shape()
{}


Circle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED

#include "Shape.h"

class Circle : public Shape {
public:
    Circle (int radius = 0);
    int getRadius() const;
    void setRadius(int radius);
    double getArea() override;

private:
    int radius;
};

#endif // CIRCLE_H_INCLUDED 


Circle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Circle.h"

Circle::Circle(int r)
{
    radius = r;
}

int Circle::getRadius() const
{
    return radius;
}

void Circle::setRadius(int r)
{
    radius = r;
}

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


Square.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED

#include "Shape.h"

class Square : public Shape {
public:
    Square (int length = 0);
    double getLength() const;
    void setLength(int length_arg);
    double getArea() override;

private:
    double length;
};

#endif // SQUARE_H_INCLUDED 


Square.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Square.h"

Square::Square(int len)
{
    length = len;
}

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

void Square::setLength(int length_arg)
{
    length = length_arg;
}

double Square::getArea()
{
    return length * length;
}


Rectangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED

#include "Shape.h"

class Rectangle : public Shape {
public:
    Rectangle (double length = 0, double width = 0);
    double getLength() const;
    double getWidth() const;
    void setLength(double length);
    void setWidth(double width);
    double getArea() override;
private:
    double length;
    double width;
};

#endif // RECTANGLE_H_INCLUDED 


Rectangle.cpp:
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
#include "Rectangle.h"

Rectangle::Rectangle(double len, double wid)
{
    length = len;
    width = wid;
}

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

void Rectangle::setLength(double len)
{
    length = len;
}

double Rectangle::getWidth() const
{
    return width;
}

void Rectangle::setWidth(double wid)
{
    width = wid;
}

double Rectangle::getArea()
{
    return width * length;
}


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

using namespace std;

int main()
{
    cout << "Calculating the Area\n\n";

    int option = 0;
    do
    {
        cout << "Pick a shape in which you would like the area of:"
                "\n1: Square"
                "\n2: Circle"
                "\n3: Rectangle"
                "\n4: Exit"
                "\nPlease enter your choice: ";
        cin >> option;

        switch(option)
        {
        case 1:
            {
                cout << "\nPlease enter the length of one side of the square: ";
                double length = 0;
                cin >> length;
                Square square(length);
                cout << "The area of the square is: " << square.getArea() << "\n\n";
                break;
            }
        case 2:
            {
                cout << "\nPlease enter the radius of the circle: ";
                double radius = 0;
                cin >> radius;
                Circle circle;
                circle.setRadius(radius);
                cout <<  "The area of the circle is: " << circle.getArea() << "\n\n";
                break;
            }
        case 3:
            {
                cout << "\nPlease enter the length of one side of the rectangle: ";
                double length = 0;
                cin >> length;
                Rectangle rectangle;
                rectangle.setLength(length);
                cout << "Please enter the width of the other side of the rectangle: ";
                double width = 0;
                cin >> width;
                rectangle.setWidth(width);
                cout << "The area of the rectangle is: " << rectangle.getArea() << "\n\n";;
            }
        }
    }
    while (option != 4);
    cout << "Bye!\n";
}

Thank you to goldenchicken and Enoizat for their very good looks into inheritance. I would also like to thank everybody for their support in this assignment, so thank you guys very much and I hope to hear from you guys again.
Topic archived. No new replies allowed.