Another inheritance problem

This is a discussion on Another inheritance problem within the C++ Programming forums, part of the General Programming Boards category; Hi gang, Yet another inheritance problem. Can anyone tell me why, for the below code, the length_ variable of my ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    121

    Another inheritance problem

    Hi gang,

    Yet another inheritance problem. Can anyone tell me why, for the below code, the length_ variable of my Cylinder object is being output when I call it with Cylinder::disp() as -9.25596e+061? Can't figure this one out!

    Thanks!

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
        class Point
    	{
    	protected:
    		double x_, y_;
    	public:
    		Point(double, double); 
    		double area();
    		double distance(Point&, Point&);
    	};
    
    	double Point::area()
    	{
    		return 0;
    	}
    
    	Point::Point(double XX, double YY)
    	{
    		x_ = XX;
    		y_ = YY;
    	}
    
    	double Point::distance(Point& point1, Point& point2)
    	{
    		return sqrt(pow((point2.x_-point1.x_), 2)+pow((point2.y_-point1.y_), 2));
    	}
    
    	class Circle : public Point
    	{
    	protected:
    		double radius_;
    
    	public:
    		Circle(double rad_, double x_, double y_) : radius_(rad_), Point(x_, y_){}
    		double area();
    	};
    	
    	double Circle::area()
    	{
    		return radius_*radius_*3.1419;
    	}
    
    	class Cylinder : public Circle
    	{
    	protected:
    		double length_;
    	
    	public:
    		Cylinder(double l_, double r_, double x_, double y_) : length_(l_), Circle(radius_, x_, y_) {}
    	    double area();
    		void disp();
    	};
    
    	double Cylinder::area()
    	{
    		
    		return length_*radius_*x_*y_;
    	}
    
    	void Cylinder::disp()
    	{
    		cout << length_ << " " << radius_ << " " << x_ << " " << y_ << endl;
    	}
    
    	int main()
    	{
    
    		Point myPoint(3.1, 4.2);
    		Circle myCircle(4.3, 3.2, 2.3);
    		Cylinder myCylinder(1.3, 3.1, 3.4, 3.2);
    		
    		myCylinder.disp();
    		
    
    		cout << "The area of myPoint is " << myPoint.area() << endl;
    		cout << "The area of myCircle is " << myCircle.area() << endl;
    		cout << "The area of myCylinder is " << myCylinder.area() << endl;
    		return 0;
    	}

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Cylinder(double l_, double r_, double x_, double y_) : length_(l_), Circle(radius_, x_, y_)
    The radius_ in that line should be r_. That mistake would cause the radius to output as a strange (uninitialized) number, not the length. Are you sure it was the length that was bad?

    Generally, if you make your member variables end with an underscore, you should not make your function parameters also end with an underscore. The point is so that you can tell them apart. In this case I don't see any problems with that, but it is a hard-to-detect error waiting to happen.

    BTW, I know this an example in your book or class or whatever and it has been said before, but remember that while this might be a good example of how inheritance works, it is a bad example of when to use inheritance.
    Last edited by Daved; 04-07-2007 at 09:58 PM.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    OK, thanks. For some reason, I was looking as the base class variable names while writing the constructor for the derived class. It seemed logical to me at the time, but guess that's not how it works. Thanks for the style tips re: underscoring; our books did not cover this. I thought inheritance was mainly used to save time, when classes have the same variables/methods. When should I use inheritance in real-life?

    -Patrick

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Public inheritance should generally model an is-a relationship or an is-used-like-a relationship. In other words, whatever you can do with the base class should make sense to do with a derived class.

    In this case, you can find the area of a circle, but getting the area of a cylinder doesn't make sense. If you want to re-use the functionality of Circle in your Cylinder class, then you would use composition. That means you create a member variable of type Circle in your Cylinder class.

    A better example of public inheritance might be a base class called Shape2D. This class could have functions to return the area or the perimiter of the shape. It wouldn't actually implement those functions, but it would provide the interface for other code to deal with shapes. Then, you could create a Square and a Circle class, both deriving from Shape2D. The Square class can implement area() and perimeter() in one way, and the Circle can do it another. Somewhere else in your code, you can have a function that will work on any shape. You can pass that function a Circle or a Square and it will still work correctly, because both Circle and Square are "used-like-a" Shape2D.

    The point of public inheritance is not to re-use the code in the base class, it is to allow existing code that uses the base class to be re-used.

    For more reading, try C++ Coding Standards by Sutter and Alexandrescu, or try the C++ Faq Lite: http://www.parashift.com/c++-faq-lite/index.html (specifically sections 19-25 about inheritance).
    Last edited by Daved; 04-08-2007 at 05:39 PM.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Thanks Dave, I've seen that before here; as a matter of fact, someone suggested it as a solution to my inheritance problem before. Our texts did not cover this, but this is good stuff! You're right, area() would not be a good name for a function that returns the volume of a cylinder. Thanks!

    -Patrick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance problem
    By logicwonder in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2006, 11:14 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Multiple inheritance problem
    By Magos in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2006, 09:27 AM
  4. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21