I am trying to finish it but there is a problem. The overloaded operator>> doesn’t work in class Circle. Would you please explain to me what wrong thing I did? Thank you so much.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::istream;
using std::ostream;
#include <iomanip>
using std::setw;
////////////////// POINT ////////////////////////////////
class Point
{
friend ostream &operator<<(ostream &out, const Point &right);
friend istream &operator>>(istream &in, Point &right);
public:
Point(double a = 0, double b = 0);
virtual ~Point(){};
void setPoint(double a, double b);
private:
double x;
double y;
};
Point::Point(double a, double b) : x(a), y(b)
{}
void Point::setPoint(double a, double b)
{ x = a; y = b; }
ostream &operator<<(ostream &out, const Point &right)
{
out << '(' << right.x << ',' << right.y << ')';
out << endl;
return out;
}
istream &operator>>(istream &in, Point &right)
{
int a, b;
in >> a;
in >> b;
right.setPoint(a, b);
return in;
}
////////////////// CIRCLE ////////////////////////////////
class Circle : public Point
{
friend ostream &operator<<(ostream &out, const Circle &right);
friend istream &operator>>(istream &in, Circle &right);
public:
Circle(double a = 0, double b = 0, double r = 0);
virtual ~Circle(){};
void setCircle(double r);
double getRadius() const;
double getArea() const;
private:
double radius;
};
Circle::Circle(double a, double b, double r) : Point(a, b)
{ setCircle(r); }
void Circle::setCircle(double r)
{ radius = r; }
double Circle::getRadius() const
{ return radius; }
double Circle::getArea() const
{ return 3.14 * getRadius() * getRadius(); }
ostream &operator<<(ostream &out, const Circle &right)
{
out << "Center: ";
out << Point(right);
out << "Area: " << right.getArea() << endl;
return out;
}
istream &operator>>(istream &in, Circle &right)
{
cout << "Enter the center of circle: " << endl;
in >> static_cast<Point>(right); // HERE. It doesn't work //
cout << "Enter radius: ";
in >> right.radius;
return in;
}
void main()
{
Circle c;
cin >> c;
cout << c;
cout << endl;
}