Inheritance?

Hi! I'm very new to inheritance and for a task that seems so simple, I'm having so much trouble figuring out how to go about this! My assignment is to have the user input a radius and side length and then the program will output the area and volume of a circle and sphere, along with a square and cube.
I can't alter the Shape and 3DShape header files.
I wrote main.cpp and some of 3DShape... I'm having trouble figuring out how to go about this. My main issue is that I'm really not sure which class(es) I should be returning the area and volume calculations in. Any guidance would be greatly appreciated!

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
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>

using namespace std;


int main(){

	sphere sphereObject;	// creating sphere object
	cube cubeObject;		// creating cube object
	float s, r;		// side and radius to be inputted

	cout << "Please input the length of the side: ";
	cin >> s;
	cubeObject.set(s);	// set the length to the side for cube

	cout << "\nPlease input the radius: ";
	cin >> r;
	sphereObject.set(r);	// set the length to the radius for the cube


	cout << "\nThe area of the sphere: " << sphereObject.print_area();
	cout << "\nThe volume of the sphere: " << sphereObject.print_volume() << "\n";

	cout << "\nThe area of the cube: " << cubeObject.print_area();
	cout << "\nThe volume of the cube: " << cubeObject.print_volume() << "\n";


}



Shape.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SHAPE_H
#define SHAPE_H


class Shape(){

	public:
		Shape();

	private:
		float area, perimeter;

};

#endif 



Shape.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>

using namespace std;

Shape::Shape(){}

// Not sure what I should include here 



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


class 3Dshape : public Shape{

	public:
		3Dshape();
		float print_volume();
		float print_area();
		void set;
		float get;

	private:
		float volume;

};

#endif 



3Dshape.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
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>

using namespace std;


3Dshape::3Dshape(){}

float 3Dshape::print_volume(){
	cout << volume;
}



float 3Dshape::print_area(){
	cout << area;
}



void 3Dshape::set(float s){
	side = s;
}

float 3Dshape::get(){
	return side;
}



sphere.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef SPHERE_H
#define SPHERE_H


class sphere : public 3Dshape{

	public:
		sphere();

	private:

};

#endif 



sphere.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>

using namespace std;

long float pi = 3.14159265359;

sphere::sphere(){}



cube.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef CUBE_H
#define CUBE_H


class cube : public 3Dshape{

	public:
		cube();

	private:

};

#endif 



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

using namespace std;


cube::cube(){}
Okay, so both sphere and cube are 3Dshapes.
And a 3Dshape is a shape.

Therefore, sphere and cube will have area, perimeter and volume.

The part that's missing in your code, is how the values for the sphere's volume and area, and the values for the cube's volume and area, are calculated - which you could do in the cube and sphere constructors respectively.

Note that, technically the calculation for the perimeter is also missing, but the main function only expects to print the volume and area.

There is a problem with your printing functions, however.
Are the member functions of the 3Dshape supposed to print, or supposed to return area and volume? The fact that the functions expect to return floats suggests that they should be returning the area and volume, but you said you aren't allowed to alter the header files... it seems strange that your instructor would suggest that the function returns a member, but then give it a misleading name that suggests that it "prints" instead. Additionally and probably more importantly, these member functions don't return anything - which is illegal, and result in compilation errors.

Woopsie wasn't paying attention
Last edited on
Your inheritance only inherits the constructor. Will be hard to find the volume area with only the volume.

Consider this example:

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
#include <iostream>

class Polygon
{
    public:
        Polygon(unsigned const width, unsigned const height);
        virtual unsigned area(void) const = 0;
    protected:
        unsigned width;
        unsigned height;
};

class Rectangle : public Polygon
{
    public:
        Rectangle(unsigned const width, unsigned const height);
        unsigned area(void) const;
};

class Triangle : public Polygon
{
    public:
        Triangle(unsigned const width, unsigned const height);
        unsigned area(void) const;
};

Polygon::Polygon(unsigned const width, unsigned const height)
: width(width), height(height){}

Rectangle::Rectangle(unsigned const width, unsigned const height)
: Polygon(width, height){}

unsigned Rectangle::area(void) const
{
    return width * height;
}

Triangle::Triangle(unsigned const width, unsigned const height)
: Polygon(width, height){}

unsigned Triangle::area(void) const
{
    return width * height / 2;
}

int main()
{
    Rectangle rec(4, 10);
    Triangle  tri(4, 10);

    std::cout << "4x10 Rectangle area: " << rec.area() << std::endl;
    std::cout << "4x10 Triangle  area: " << tri.area() << std::endl;

    return 0;
}
4x10 Rectangle area: 40
4x10 Triangle  area: 20


Also, please check out this link: http://www.cplusplus.com/doc/tutorial/inheritance/
Last edited on
In sphere and cube, would I be able to have something like
1
2
3
4
5
6
7
sphere::sphere(float radius){

	volume = (4/3)*pi*(pow(radius,3));

	area = 4*pi*(pow(radius,2));

}

? I figured since sphere and cube have access to 3Dshape's variables and functions, it would be alright for the volume calculation. But, I'm not sure if I can alter the area, or not. Would inheriting 3Dshape's variables and functions include those that 3Dshape inherits from Shape?


And if I do this, I'm confused on how to accommodate my main function.
I would make an object like sphere sphereObject(r), where the input would be the radius, and then the area and volume from the base classes would be calculated in the constructor.
But then if this works, I'm not sure what I would be using the set and get functions for and how I would be demonstrating inheritance.




Thanks for the guidance and link! They were really helpful.

Also, about the print function, I'm really not sure what my professor meant by that either. I'm most likely just going to alter it to return void and comment why.
I figured since sphere and cube have access to 3Dshape's variables and functions They only have access to the public and protected things not the private ones.

I can't alter the Shape and 3DShape header files.
I'm not sure what I would be using the set and get functions
You may want to fix the header even though you said you can't because, right now they are variables not functions.
1
2
		void set;
		float get;
The set shouldn't compile unless it is a pointer or a function.

If I changed my header so that set and get were functions, how would I be using them?
Since the sphere and cube's constructors calculate the area and volume using the user's input as a parameter, I wouldn't think using the set function is necessary to set the radius and side values.
Would it make sense for me to use the set and get functions to get the area and volume calculations from sphere and cube?
Well according to your last code none of your constructors took any parameters so the only way would be to use setters. If you could show the current code it would help.
My current code:

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
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>

using namespace std;


int main(){

	float s, r;		// side and radius to be inputted

	cout << "Please input the length of the side: ";
	cin >> s;
	cube cubeObject(s);	// creates a cube object with side = user input s

	cout << "\nPlease input the radius: ";
	cin >> r;
	sphere sphereObject(r); // creates a cube object with radius = user input r


	cout << "\nThe area of the sphere: " << sphereObject.print_area();
	cout << "\nThe volume of the sphere: " << sphereObject.print_volume() << "\n";

	cout << "\nThe area of the cube: " << cubeObject.print_area();
	cout << "\nThe volume of the cube: " << cubeObject.print_volume() << "\n";


}


Shape.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef SHAPE_H
#define SHAPE_H


class Shape(){

	public:
		Shape();

	private:
		float area, perimeter;

};

#endif 



Shape.cpp:
1
2
3
4
5
6
7
8
9
10
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>

using namespace std;

Shape::Shape(){}



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


class 3Dshape : public Shape{

	public:
		3Dshape();
		void print_volume();
		void print_area();
		void set();
		float get();

	protected:
		float volume;

};

#endif 



3Dshape.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
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>

using namespace std;


3Dshape::3Dshape(){}

void 3Dshape::print_volume(){
	cout << volume;
}



void 3Dshape::print_area(){
	cout << area;
}



void 3Dshape::set(float a){	// Takes in the area from sphere and cube, maybe?
// Not sure what we want to set
}

float 3Dshape::get(){	// Gets the area from sphere and cube, maybe?
// Not sure what we're getting.
}



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


class sphere : public 3Dshape{

	public:
		sphere();
		sphere(float);

	protected:
		float area_s;
		float volume_s;

};

#endif 



sphere.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>

using namespace std;

long float pi = 3.14159265359;

sphere::sphere(){}

sphere::sphere(float radius){

	volume = (4/3)*pi*(pow(radius,3));

	area = 4*pi*(pow(radius,2));

}



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


class cube : public 3Dshape{

	public:
		cube();

		cube(float);

	protected:


};

#endif 



cube.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>

using namespace std;


cube::cube(){}

cube::cube(float side){

	volume = pow(side,3);
	area = 6*(pow(side,2));

}
Can anyone please help me understand how the set and get functions should be used in order to be using inheritance?
It seems to be used for setting and getting the volume? It seems a bit questionable how you have the shape and 3dshape setup.
How is it questionable?

The instructions said the classes should be set up like this:

shape <--- 3Dshape <--- sphere
<--- cube
Did the instructions say to have the classes setup like that though?

I would do something like:

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
class Shape
{
    public:
        Shape(void);
        ~Shape(void);
        virtual double area(void) const = 0;
        virtual double perimeter(void) const =0;
};

class Shape3D : public Shape
{
    public:
        Shape3D(void);
        ~Shape3D(void);
        virtual double volume(void) const = 0;
};

class Sphere : public Shape3D
{
    public:
        Sphere(double const radius);
        ~Sphere(void);
        double area(void) const;
        double perimeter(void) const;
        double volume(void) const;
    private:
        double radius;
        static double constexpr pi = 3.14159;
};

class Cube : public Shape3D
{
    public:
        Cube(double const width);
        ~Cube(void);
        double area(void) const;
        double perimeter(void) const;
        double volume(void) const;
    private:
        double width;
};
Though it is probably missing a few things and there might be an easier way to setup.
Topic archived. No new replies allowed.