include a derive class named sphere from the base circle class. the only additional class members of sphere should be s constructor and a calcval () function that returns the volume of the sphere... please kindly help me in correcting the code..

#include <iostream>
#include <cmath>
using namespace std;

const double PI=2.0 * asin(1.0);
//class declaration

class sphere
{
protected:
    double radius;
public:
    sphere(double =1.0);
    double calcval();
};

// implementation section for sphere
sphere::sphere(double r)   // constructor
{
    radius =r;
}

// caculate the area of a sphere
double sphere::calcval()
{
    return(PI * radius * radius);
}

// class declaretion for derive class
// cylinder which is derived from sphere
class cylinder : public sphere
{
protected:
    double length; // add one additional data member and 
public:          // two additional function members
    cylinder(double r=1.0, double 1 =1.0) : sphere (r), length(1) {}
    double calcval();
};

//implementation for sphere
double sphere::calcval()        // this calculate the volume
{
    return (4/3(PI * radius * radius * radius)); // note the base function call
}


int main()
{
    sphere  sphere_1, sphere_2(2); // creat two sphere objects
    cylinder cylinder_1(3,4) // create one cylinder object

        cout << "The area of sphere_1 is " << sphere_1.calcval() << end1;
        cout << "The area of sphere_2 is " << sphere_2.calcval() << end1;
        cout << "The volume of cylinder_1 is " << cylinder_1.calcavl()<< end1;

    sphere_1 = cylinder_1;  // assign a cylinder to a sphere

    cout << "\nThe area of sphere_1 is now" << sphere_1.calcval() << end1;

    return 0;
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

>>const double PI=2.0 * asin(1.0);

Just Do : const double PI = 3.14159265;

no reason for the call to asin.


>>include a derive class named sphere from the base circle class.

Means :

class Sphere : public Circle {
}

>>class cylinder : public sphere

Make no sense.

This article has been dead for over six months. Start a new discussion instead.