problem in code

the question is
Create a Sphere class, define its attributes for the radius x coordinate, y coordinate, z coordinate as Center points using double data type, include pi as a constant variable in class. Get the Center values from the user, Calculate its volume by using formula:


Change the radius of the Sphere by specifying the radius value, and display the Center values of a Sphere.
Write a main function () to test it.

#include<iostream.h>
#include<math.h>
class sphere
{
private:
double rx, ry, rz;
const pi;
float result;
public:
void get()
{
cout<<"\nEnter Radius of sphere::"<<endl;
cout<<"\nX Coordinate::";
cin>>rx;
cout<<"\nY Coordinate::";
cin>>ry;
cout<<"\nZ Coordinate::";
cin>>rz;
}
void volume()
{
result=4*(3.14*rx*ry*rz)/3;
}
void show1()
{
cout<<"\nVolume of Sphere is::"<<result<<endl;
}
void set()
{
rx=4;
ry=7;
rz=5;
}
void show2()
{
cout<<"\nCenter Values of Sphere are::"<<endl;
cout<<"\n\nX Coordinate::"<<rx;
cout<<"\n\nY Coordinate::"<<ry;
cout<<"\n\nZ Coordinate::"<<rz<<"\n\n";;
}
};
int main()
{
sphere sph;
cout<<"\t\t---------------Input From User---------------";
sph.get();
sph.volume();
sph.show1();
cout<<"\t\t---------------Specified Values---------------"<<endl;
sph.set();
sph.show2();
return 0;
}

in this code i have to declare pi as constant variable.But it gives me this error
error C2512: 'sphere' : no appropriate default constructor available
and a warning
warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
Please help me out..
Last edited on
The class specifies variables for the co-ordinates of the centre (x,y,z).
The variable for the radius is missing.

There may be multiple instances of the sphere object, but just one value of pi. Hence pi should be specified as static.
 
    static const double pi;

The value needs to be initialised separately.
const double sphere::pi = 3.1415926535897932385;

and of course it is this constant which should be used in the calculations, rather than the hard-coded value of 3.14. Incidentally, the calculation of volume should use the value of the radius, (currently omitted).
Last edited on
i am working on visual c++ 6.0 on windows 7 and i am having problem in running a simple hello world program it gives me following error

fatal error LNK1104: cannot open file "Debug/j.exe"

please guide me what to do...
Assuming your executeable is named "j", that would indicate you're trying to execute a project that didn't build. Fix your compile errors and that error shold go away.


ive written a simple hello world program it gives me no error when i compile it but at the time of buliding i get this error...
If you've previously compiled the source and ran the executable from within the IDE, the linker will be unable to open the target file until the console the executable was run in before is closed and the file is no longer in use.
Last edited on
Topic archived. No new replies allowed.