# Inheritance problem

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-02-2007
patricio2626
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; }```
• 04-02-2007
JaWiB
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?
• 04-02-2007
patricio2626
Quote:

Originally Posted by JaWiB
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?
• 04-02-2007
JaWiB
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 :eek:
• 04-02-2007
patricio2626
Quote:

Originally Posted by JaWiB
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 :eek:

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!
• 04-02-2007
JaWiB
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.
• 04-03-2007
patricio2626
• 04-03-2007
patricio2626
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; }```
• 04-03-2007
Dave_Sinkula
Quote:

Originally Posted by patricio2626
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.
• 04-03-2007
patricio2626
Quote:

Originally Posted by Dave_Sinkula
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'?
• 04-03-2007
Dave_Sinkula
Quote:

Originally Posted by patricio2626
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;```
distance is in the standard, I believe.
• 04-03-2007
patricio2626
Quote:

Originally Posted by Dave_Sinkula
No.

This, among others.
Code:

```using std::asin; using std::cout; using std::endl; using std::sqrt; using std::pow;```
distance is in the standard, I believe.

Man, that's awful.
• 04-03-2007
patricio2626
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?
• 04-03-2007
Dave_Sinkula
Quote:

Originally Posted by patricio2626
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 */```
• 04-04-2007
patricio2626
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last