Not getting my expected output

I'm not getting my expected output and have no idea where to begin with fixing the code. When I compile and run the code it outputs:
Area of the rectangle is: 5.56729e+123
Area of the Square is: 5.56729e+123
Area of the Ellipse is 2.69013e+124
Area of the Circle is: 2.69013e+124

This is my first time working with inheritence and I thought I had it down mostly but not sure where to go from here.

Shape.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
#ifndef SHAPE_H
#define SHAPE_H

using namespace std; 

class Shape {
public:
	Shape(int);
	double computeArea();
private:
	int color;
};

#endif

Shape.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Shape.h"

using namespace std;

Shape::Shape(int color){
}

double Shape::computeArea() {
	return (-1.0);
}

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

using namespace std;

class Rectangle : public Shape
{
public:
	Rectangle(int, double, double);
	double computeArea();
private:
	double side1;
	double side2;
	int color;
};

#endif 

Rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Rectangle.h"


using namespace std;

Rectangle::Rectangle(int color, double side1, double side2) : Shape(color) {
}

double Rectangle::computeArea() {
	return (side1 * side2);
};

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

using namespace std;

class Square : public Rectangle
{
public:
	Square(int, double);
	double computeArea();
private:
	double side1;
	double side2;
	int color;
};

#endif 

Square.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Square.h"


using namespace std;

Square::Square(int color, double side1) : Rectangle(color, side1, side2) { 
}

double Square::computeArea() {
	return(side1 * side1);
}

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

using namespace std;

class Ellipse : public Shape
{
public:
	Ellipse(int, double, double);
	double computeArea();
private:
	double radi1;
	double radi2;
	int color;
};

#endif 

Ellipse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Ellipse.h"

using namespace std;

Ellipse::Ellipse(int color, double radi1, double radi2) : Shape(color) {

}

double Ellipse::computeArea() {
	return (radi1 * radi2 * 3.14);
};

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

using namespace std;

class Circle : public Ellipse
{
public:
	Circle(int, double);
	double computeArea();
private:
	double radi1;
	double radi2;
	int color;
};

#endif 

Circle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Circle.h"

using namespace std;

Circle::Circle(int color, double radi1) : Ellipse(color, radi1, radi2) {
	
}

double Circle::computeArea() {
	return((radi1 * radi1) * 3.14);
}

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

using namespace std;

int main() {
	Rectangle r(100, 20, 10);
	Square s(200, 15);
	Ellipse e(300, 12, 14);
	Circle c(400, 10);

	cout << "Area of the rectangle is: " << r.computeArea() << endl;
	cout << "Area of the Square is: " << s.computeArea() << endl;
	cout << "Area of the Ellipse is: " << e.computeArea() << endl;
	cout << "Area of the Circle is: " << c.computeArea() << endl;

	getchar();
	return 0;
}
1) Circle should not be derived from Ellipse and Square should not be derived from rectangle.
Read till the end: https://isocpp.org/wiki/faq/proper-inheritance#circle-ellipse

2) You never set members in Rectangle and Ellipse constructors. You get some values but throw them out.

3) You duplicate base class data with derived class data.
It your quare class has both rectangle::side1 and square::side1 variables for example.

4) In circle and square class you pass values to base class constructor which should set base class members (but doesn't due to (2)) but in computeArea you use uninitialized derived class data.
closed account (SECMoG1T)
@minnippa told you about everything you messed there . For emphasis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 ///example 
 class Rectangle : public Shape 
 { 
    public: 	
         Rectangle(int, double, double); 	
         double computeArea(); 
     private: 	
         double side1; 	
         double side2;/// this innocent variables are never intialized by your constructors they contain 
         int color;        //garbage you have to use the constructor to do thet
  };

 ///in rectangle.cpp
 Rectangle::Rectangle (int color,  double side1, double side2): Shape (color){} /// your constructor ignores your
                                                              /// Rectangle private variables

 double Rectangle::computeArea() 
   { 	
      return (side1 * side2); /// multplying some garbage isn't what you wanted
   };
 


Last edited on
Ahhhhh thank you guys so much!
Just an example how you can use inheritance properly (Uses C++14):
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
#include <iostream>
#include <cmath>
#include <memory>
#include <string>
#include <vector>

using namespace std::literals;

const double Pi = atan2(0, -1);

class Shape
{
public:
    virtual double area() = 0;
    virtual std::string name() = 0;
    virtual ~Shape() = default;
};

class Rectangle: public Shape
{
public:
    Rectangle(double x_, double y_) : x(x_), y(y_) {}
    virtual double area() {return x*y;};
    bool is_square() {return x == y;}
    virtual std::string name() {return is_square() ? "Square"s : "Rectangle"s;}
private:
    double x;
    double y;
};

class Ellipse: public Shape
{
public:
    Ellipse(double x_, double y_) : r1(x_), r2(y_) {}
    virtual double area() {return r1*r2*Pi;}
    bool is_circle() {return r1 == r2;}
    virtual std::string name() {return is_circle() ? "Circle"s : "Ellipse"s;};
private:
    double r1;
    double r2;
};

std::unique_ptr<Shape> create_rectangle(double x, double y)
{ return std::make_unique<Rectangle>(x, y); }

std::unique_ptr<Shape> create_square(double side)
{ return std::make_unique<Rectangle>(side, side); }

std::unique_ptr<Shape> create_ellipse(double x, double y)
{ return std::make_unique<Ellipse>(x, y); }

std::unique_ptr<Shape> create_circle(double r)
{ return std::make_unique<Ellipse>(r, r); }

int main()
{
    std::vector<std::unique_ptr<Shape>> shapes;
    shapes.push_back(create_rectangle(20, 10));
    shapes.push_back(create_square(15));
    shapes.push_back(create_ellipse(12, 14));
    shapes.push_back(create_circle(10));
    auto end = shapes.size();
    for(decltype(end) i = 0; i < end; ++i)
        std::cout << "Shape " << i << " is a " << shapes[i]->name() <<
                     " and its area is " << shapes[i]->area() << '\n';
}
Shape 0 is a Rectangle and its area is 200
Shape 1 is a Square and its area is 225
Shape 2 is a Ellipse and its area is 527.788
Shape 3 is a Circle and its area is 314.159
http://coliru.stacked-crooked.com/a/f14ebb9dd2a5d59c
Topic archived. No new replies allowed.