Originally Posted by
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.
Not exactly, I was just thinking more along the line of "a Circle has-a (center) Point", rather than "a Circle is-a Point".
If it's an exercise in inheritance, that wouldn't be much different.
Code:
class Circle : public Point
{
double radius;
public:
Circle(double radius_, double x_ = 0, double y_ = 0)
: Point(x_, y_), radius(radius_)
{
}
double area()
{
return PI * radius * radius;
}
};
What I think was a key issue with your earlier attempts was the distance function.
Code:
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);
}
};