Hi everyone,
I am very new to programming and I tried to write this code in c++ to calculate mass, volume, and area for a sphere and a solid. It compiles but I get nonsense answers everytime I run the program..I know there is a error in my logic somewhere but I just can't see it.
Here is the code
Code:
#include<iostream>
using namespace std;
class TSolid{
public:
TSolid(){};
virtual ~TSolid(){};
protected:
double mass, volume, density, area;
public:
double getArea(){return area;}
double getVolume(){return volume;}
double getMass(){return mass;}
void setDensity(double d){density=d;}
virtual void ComputeMass(){;};
virtual void ComputeVolume(){;};
virtual void ComputeArea(){;};
};
class TSphere:public TSolid{
public:
TSphere (){};
virtual ~TSphere (){};
double radius,d;
void ComputeArea (){area= 4*3.14*radius*radius;}
void ComputeVolume (){volume=1.33*3.14*radius*radius*radius;}
void ComputeMass () {mass=d*3.14*1.33*radius*radius*radius;}
};
class TCube:public TSolid{
public:
TCube(){};
virtual ~TCube(){};
double side,d;
void ComputeArea (){area=6*side*side;}
void ComputeVolume (){volume=side*side*side;}
void ComputeMass (){mass=d*side*side*side;}
};
int main ()
{
double side,d,radius;
d=30;
radius=15;
side=12;
TCube box;
TSphere ball;
cout<<"d="<<d<<endl;
cout<<"radius="<<radius<<endl;
cout<<"side="<<side<<endl;
cout << "sphere info:"<<endl;
cout << "area = " << ball.getArea()<<endl;
cout <<"area=" <<4*3.14*radius*radius<<endl;
ball.ComputeVolume();
cout << "volume =" << ball.getVolume()<<endl;
ball.ComputeMass();
cout << "mass = " <<ball.getMass()<<endl;
cout<< "cube info: "<<endl;
box.ComputeArea();
cout<< "area = " << box.getArea()<<endl;
box.ComputeVolume();
cout << "volume = " << box.getVolume()<<endl;
box.ComputeMass();
cout << "mass = " << box.getMass()<<endl;
cout<<"side="<<side<<endl;
return 0;
}
thanks for the help