# need help finding where the problem is for this code

• 06-18-2007
edmorgan
need help finding where the problem is for this code
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
• 06-18-2007
anon
side and radius seem to be unitialized in the classes.

It doesn't help to use variables of the same name in main(). That doesn't magically set the data members of an object. That's what constructors are for.

From a design point of view: why are the compute() and get() functions separate? What if I forget to call the compute() function first?

Either a single function should calculate and return the result directly and the results are not stored in the class itself, or the compute() functions are private (or protected) and called automatically only by the class itself (in the constructor) before the user of the class gets a chance to call the get methods.
• 06-18-2007
simpleid
It seems that you meant to pass 'radius' and 'side' as an argument to ComputerArea/Volume/Mass. That probably has something to do with it.

this bit:
Code:

`void ComputeArea (){area= 4*3.14*radius*radius;}`
might want to be this like so:
Code:

`void ComputeArea (double radius){area= 4*3.14*radius*radius;}`
etc...

(anon beat me to it *shakes fist*)
• 06-18-2007
edmorgan
yep, that was definitely the issue. i finally get the right answers! this is exciting, thanks a lot