Find volume and surface area of a sphere

I made a program to find the surface area or volume of a sphere and it works, but my teacher says its not object oriented and does not use classes. Can someone help me because i dont know how to do it.

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>

using namespace std;

char s = '\0';
const char SENTINEL = 's';

float radius, answer;

void get_radius (float&);
float surface_area (float);
float volume (float);

const float PI = 3.14;

int main(){
    cout << "This program will let you input the radius of a sphere to find its volume or surface area." << endl << endl;
    cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
    cout << "'s' to stop" << endl;
    cin >> s;
    while (s != SENTINEL){
        get_radius (radius)
        if(s == 'v')
        {
            volume (radius);
        }
        else if(s == 'a')
        {
            surface_area (radius);
        }
        cout << "Enter 'v' for volume or 'a' for surface area of a sphere" << endl;
        cout << "'s' to stop" << endl;
        cin >> s;
    }
    
    system("PAUSE");
    return 0;
}
void get_radius (float& radius)
{
    cout << "Please enter the radius of the sphere: " << endl;
    cin >> radius;
}
float volume (float radius){
    float answer;
    answer = 4.0/3.0 * PI * pow (radius, 3);
    cout << "The volume is: " << answer << endl;
    return answer;
}
float surface_area (float radius){
    float answer;
    answer =  4.0 * PI * pow(radius, 2);
    cout << "The surface area is: " << answer << endl;
    return answer;
}
I'd suggest implementing the following using the code you already have:
1
2
3
4
5
6
7
8
9
class sphere{
private:
  float radius;
public:
  sphere(float Radius);
  float get_radius();
  float get_volume();
  float get_surface_area();
};


If you define these functions (should be easy using your pre-existing code), then in main, ask for a radius, construct a sphere with that radius and then use its member functions to return things like volume and surface area. If you're confused about how to do this, there are some very good tutorials on OOP in the tutorials section of this site.
I dont quite understand how to set it up :/
Okay. Outside of main, put the code I gave, which is the code for a class called sphere that contains a radius and has functions get_radius(), get_volume() and get_surface_area();
After the class declaration, define each of these functions as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
float sphere::get_radius()
{
  return radius;
}

float sphere::get_volume()
{
  return 4.0/3.0 * PI *pow(radius,3);
}

float sphere::get_surface_area()
{
  return 4.0 * PI * pow(radius,2);
}


Then in main(), use cout and cin to get a radius input from the user, then do:
 
sphere Sphere(theRadiusYouGotFromTheUser);


and when you want to find the volume or surface area, simply use:
1
2
3
Sphere.get_surface_area();
//or
Sphere.get_volume();

These can be put in cout if you want to give them to the user.
Topic archived. No new replies allowed.