Hello everybody,
I wrote a test source code about 2 classes point and circle. These classes illustrate the center of a circle (point class) and its radius (circle class). Circle class inherits point class. Here's the code:
#include <iostream>
using namespace std;
class point {
private:
int x, y;
public:
point() {}
point(int x, int y);
point(const point& d);
int getX() { return x; }
int getY() { return y; }
};
class circle : public point {
private:
int radius;
public:
circle(point p, int r);
int getRadius() { return radius; }
bool operator>(circle c);
};
int main() {
circle c1(point(0, 1), 2), c2(point(2, 2), 1);
if (c1 > c2)
cout << "center: " << c1.getX() << ", " << c1.getY() << " radius: " << c1.getRadius() << endl;
else
cout << "center: " << c2.getX() << ", " << c2.getY() << " radius: " << c2.getRadius() << endl;
return 0;
}
point::point(int x, int y) {
this -> x = x;
this -> y = y;
}
point::point(const point& d) {
x = d.x;
y = d.y;
}
circle::circle(point p, int r) {
point(p);
radius = r;
}
bool circle::operator>(circle c) {
return (radius > c.radius);
}
My question refers to that specific piece of code:
circle::circle(point p, int r) {
point(p);
radius = r;
}
When i call copy constructor point() inside the body of circle constructor (as above) then compiler complains showing the message: "declaration of 'point p' shadows a parameter"
When i change that code into the following then everything is ok:
circle::circle(point p, int r) : point(p) {
radius = r;
}
Why is that happening? I thought that i could call a constructor inside other's body..Thank you for your time!