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);
		Point(double a = 0, double b = 0);
		virtual ~Point(){};
		void setPoint(double a, double b);		
		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);
		Circle(double a = 0, double b = 0, double r = 0);
		virtual ~Circle(){};
		void setCircle(double r);
		double getRadius() const;
		double getArea() const;		
		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;

Recommended Answers

All 2 Replies

Are you sure that's the class heiarchy you want? If it were me I'd declare Circle to have a member variable of type Point, since a Circle is not a Point like every Dog is an Animal and every Car is a Vehicle.

What's a problem? At first sight, it works...

Some remarks:
1. You have Point class. Where is natural constructor Circle(const Point&,double radius) ?
2. It's doubtful style to define overloaded >> op with interactive output. Better define special member function (enterPoint, enterCircle or what else). Good stream input operator must read a result of correspondent output op - that's all.
3. Why getRadius but setCircle for radius setting?
4. Is a circle a point too (public inheritance)? May be so, but it's open to question...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.