I have Warning and the result after debugging is rubbish values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • batoool
    New Member
    • Oct 2013
    • 2

    #1

    I have Warning and the result after debugging is rubbish values

    Code:
    #include<iostream>
    using namespace std;
    
    class Circle_computations
    {
    
    protected:
    	double area,circumference,radius,pi;
    	
    public:
    
    
     Circle_computations()
    {
     }	
      Circle_computations(double radi)
    {
    pi = 3.14;
    
    set_r(radi);
    
    }
    
    
    
      double set_r(double radi)
      {
      
    try
    {
    if(radi<=0.0)
    {
    throw "PLESE ENTER positive NUMBERS";
    
    }
    
    radius=radi;
    
    
    }
    
    catch ( const char* strException )
        {
            cerr << "Error: " << strException << "\n";
            return 1 ;
    		
        }
    
    
    
    
    
    
    
    
      }
    
    double get_r()
    {
    
    return radius;
    
    }
    
    
    
    double circle_area()
    {
    
    area = pi*radius*radius;
    return area;
     
    }
    
    double circle_circumferunce()
    {
    
    circumference =2*pi*radius;
    return circumference;
    
    }
    void print_circleoutput()
    {
    cout<<"****The  Area OF The Circle  is****\n"<<area<<"\n";
    
    cout<<"****The Circle Circumferunce is****\n"<<circumference<<"\n";
    
    }
    
    
    
    
    
    };
    
    class Cylinder:public Circle_computations
    {
    
    protected:
    	double height,volume,cyarea;
    
    public:
    Cylinder(double r, double h) :
        Circle_computations(r)
    {
        set_height(h);
    }
    
    	
    
    double set_height(double h)
      {
      
    try
    {
    if(h<=0.0)
    {
    throw "PLESE ENTER positive NUMBERS";
    
    }
    
    height=h;
    
    
    }
    
    catch ( const char* strException )
        {
            cerr << "Error: " << strException << "\n";
            return 1 ;
    		
        }
    
    
    
    
      }
    
    double get_height()
    {
    
    return height;
    
    }
    
    
    
    	double cylindersurface_area()
    {
    	cyarea=(2 * (Circle_computations::circle_area()))+( circle_circumferunce()* height) ;
    
    	return cyarea;
    
    }
    
    
    double c_volume()
    {
    	volume = (Circle_computations::circle_area())* height;
    
    	return volume;
    
    }
    
    
    void print_cylinderoutput()
    {
    cout<<"****The cylinder surface  Area   is****\n"<<cyarea;;
    
    cout<<"****The cylinder volume   is****\n"<<volume;
    }
    
    
    };
    
    
    int main()
    
    {
    
    Circle_computations  compute(1.5);
    Cylinder computecylider(1.5,2.5);
    cout<<"****The  Radius  is****\n";
    
    computecylider.get_height();
    computecylider.get_r();
    computecylider.set_r(3.5);
    computecylider.set_height(2.6);
    computecylider.print_cylinderoutput();
    
    
    }
    Last edited by Rabbit; Oct 30 '13, 09:52 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, the inheritance is not required. A cylinder is not a circle. A cylinder contains a circle. That is to say a cylinder HAS-A circle. The HAS-A relationship is implemented using a circle member variable in the cylinder.

    You can pass the height an diameter of the cylinder to the cylinder methods who in turn pass the diameter to circle methods that maintain the circle object inside the cylinder object.

    Second, do not use protected unless you know what protected is for. This is not it. You need protected data members when developing a derived class using multiple bass classes. That is, by using multiple inheritance. In this problem all member data is private and all class methods are public.

    Using protected here is an attempt to do and end run on data security by avoiding private access. This violates the encapsulation and data hiding rules of object programming.

    If you want the area of the ends of the cylinder then call a cylinder method that returns the area. The cylinder method just calls the circle area method using the private circle object that is inside the cylinder.

    Third, write your circle class by itself and get it working. Then write your cylinder class to use the already working circle class. You will find your data issues disappear.

    BTW: Don't forget to write constructors to initialize your data members to avoid garbage values.

    Comment

    Working...