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!
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
usingnamespace 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";
}
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>
usingnamespace std;
Shape::Shape(){}
// Not sure what I should include here
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.
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.
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
usingnamespace 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";
}
#include "Shape.h"
#include "3Dshape.h"
#include "sphere.h"
#include "cube.h"
#include <iostream>
#include <cmath>
usingnamespace 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