Inheritance problem

This is a discussion on Inheritance problem within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me why I'm getting the following error for the below program? It doesn't like my reuse of ...

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

    Inheritance problem

    Can anyone tell me why I'm getting the following error for the below program? It doesn't like my reuse of the base class, for some reason:

    error C2664: 'Point::Point(const Point &)' : cannot convert parameter 1 from 'double' to 'const Point &'
    error C2437: 'Point' : already initialized
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    const double PI = 3.14159;
    
    class Point
    {
    protected:
    	double x, y;
    public:
    	Point(double, double);
    	virtual int area();
        double distance(Point&, Point&);
    };
    
    Point::Point(double XX, double YY)
    {
    	x = XX;
    	y = YY;
    }
    
    int Point::area()
    {
    	return 0;
    }
    
    double Point::distance(Point& myPoint, Point& myPoint1)
    {
        double distance = sqrt(pow((myPoint1.x-myPoint.x), 2)+pow((myPoint1.y-myPoint.y),2));
    	return distance;
    }
    
    class Circle : public Point
    {
    protected:
       double radius;
    public:
    	Circle(double rad, double X, double Y) : radius(rad), Point(x), Point(y){}
    	virtual double area(Circle&);
    };
    
    /*Circle::Circle(double rad)
    {
    	radius = rad;
    }*/
    
    double Circle::area(Circle& myCircle)
    {
    	return PI*pow((myCircle.radius), 2);
    }
    
    int main()
    {
    	
        
    	return 0;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I believe what you want is:
    Code:
    Circle(double rad, double X, double Y) : radius(rad), Point(X), Point(Y){} //note the capital x and y
    You might want to consider using a consistent naming convention for your class member variables; for example, you could prefix them with m_ (m_x,m_y) or you could add a trailing underscore (x_,y_). As written, it is very easy to confuse function arguments with class members.

    Also, is there any reason you aren't using an initializer list for your Point class?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by JaWiB View Post
    I believe what you want is:
    Code:
    Circle(double rad, double X, double Y) : radius(rad), Point(X), Point(Y){} //note the capital x and y
    Also, is there any reason you aren't using an initializer list for your Point class?
    Hmmmm, is that necessary, since Point is the base class in this case?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Whoa, I just noticed my code was completely wrong, I meant:
    Code:
    Circle(double rad, double X, double Y) : radius(rad), Point(X,Y){}
    I apologize, something must've gone wrong when I copied and pasted
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by JaWiB View Post
    Whoa, I just noticed my code was completely wrong, I meant:
    Code:
    Circle(double rad, double X, double Y) : radius(rad), Point(X,Y){}
    I apologize, something must've gone wrong when I copied and pasted
    Oh, so, when two or more of the members of the derived class are used, you must use className(var, var)? Is that right?

    Thanks!

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Not necessarily. You're calling the constructor of the base class, so the arguments you give it have to match the arguments it expects. In this case, Point's constructor expects two doubles, so you pass X and Y to it.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Thanks, that answers it!

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Hmmm, so why am I getting 10 strange errors that all point to xutility? Below code is what I'm using. Here is a sample of one of the errors:

    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(98): error C2039: 'iterator_category' : is not a member of 'Point'


    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    const double PI = 2.0*asin(1.0); 
    
    class Point
    {
    protected:
    	double x, y;
    public:
    	Point(double, double);
    	double area(){return 0;}
    	double distance(Point&, Point&);
    };
    
    Point::Point(double XX, double YY)
    {
    	x = XX;
    	y = YY;
    }
    
    double Point::distance(Point& pt1, Point& pt2)
    {
        return sqrt(pow((pt2.x-pt1.x), 2)+pow((pt2.y-pt1.y),2));
    }
    
    class Circle : public Point
    {
    protected:
    	double radius;
    public:
    	Circle(double rad, double x, double y) : radius(rad), Point(x, y) {}
    	double area(Circle&);
    };
    
    double Circle::area(Circle& myCircle)
    {
        return PI*pow(myCircle.radius, 2);
    }
    
    int main()
    {
    	Point myPoint(1.3, 4.5);
    	Point myPoint1(3.2, 3.4);
    
    	cout << "The distance between the two points created is " << distance(myPoint, myPoint1) << endl;
    
    	Circle myCircle(10.0, 4.5, 6.5);
    	
    	return 0;
    }

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by patricio2626 View Post
    Hmmm, so why am I getting 10 strange errors that all point to xutility? Below code is what I'm using. Here is a sample of one of the errors:

    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xutility(98): error C2039: 'iterator_category' : is not a member of 'Point'
    I believe you've been bitten by using namespace std; and its consequences.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by Dave_Sinkula View Post
    I believe you've been bitten by using namespace std; and its consequences.
    Dave,

    Are you joking? So, what is the alternative to 'using namespace std'?

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by patricio2626 View Post
    Are you joking? So, what is the alternative to 'using namespace std'?
    No.

    This, among others.
    Code:
    using std::asin;
    using std::cout;
    using std::endl;
    using std::sqrt;
    using std::pow;
    [edit]distance is in the standard, I believe.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Quote Originally Posted by Dave_Sinkula View Post
    No.

    This, among others.
    Code:
    using std::asin;
    using std::cout;
    using std::endl;
    using std::sqrt;
    using std::pow;
    [edit]distance is in the standard, I believe.
    Man, that's awful.

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Sorry, I just caught the edit you made:

    distance is in the standard, I believe

    Do you mean that I was using a function (distance) whose name is reserved?

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by patricio2626 View Post
    Sorry, I just caught the edit you made:

    distance is in the standard, I believe

    Do you mean that I was using a function (distance) whose name is reserved?
    I believe it is that you were using a name that is in the std namespace because your distance function is ill-defined. I believe as you intend -- a function that takes two references to Point -- that you really want a friend function and not a member function. But I'm really not that good at C++.

    Also, do you really want inheritance? Or is aggregation preferred in this case?
    Code:
    #include <iostream>
    #include <cmath>
    
    const double PI = 2.0 * std::asin(1.0); 
    
    class Point
    {
       double x, y;
    public:
       Point(double x_ = 0, double y_ = 0) : x(x_), y(y_)
       {
       }
       friend double distance(const Point& pt1, const Point& pt2)
       {
          double a = pt2.x - pt1.x, b = pt2.y - pt1.y;
          return std::sqrt(a * a + b * b);
       }
    };
    
    class Circle
    {
       double radius;
       Point  center;
    public:
       Circle(double radius_, double x_ = 0, double y_ = 0)
       : radius(radius_), center(x_, y_)
       {
       }
       double area()
       {
          return PI * radius * radius;
       }
    };
    
    int main()
    {
       Point myPoint(1.3, 4.5);
       Point myPoint1(3.2, 3.4);
    
       std::cout << "The distance between the two points created is " 
                 << distance(myPoint, myPoint1) << std::endl;
    
       Circle myCircle(10.0, 4.5, 6.5);
    
       return 0;
    }
    
    /* my output
    The distance between the two points created is 2.19545
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Jun 2006
    Posts
    121
    Yeah, it's specified as inheritance in the book, so that's the example I'm trying to implement. So, you're saying that I should instantiate an object of the base class in the inherited class, and use that object's variables for instantiation of a derived class object. This is good stuff, thanks!

    -Patrick

Page 1 of 2 12 LastLast
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