#include
using namespace std;
class Point
{
public:
explicit Point(float x = 0.0f, float y = 0.0f):m_x(x), m_y(y) {}
~Point() {}
void setX(float x) { m_x = x; }
void setY(float y) { m_y = y; }
float getX() const { return m_x; }
float getY() const { return m_y; }
Point operator+(Point p)
{
return Point(m_x + p.getX(), m_y + p.getY());
}
private:
float m_x;
float m_y;
};
class Circle
{
public:
Circle(float x, float y, float radius)
{
m_pCenter = new Point(x, y);
m_radius = radius;
}
~Circle() {}
Circle operator+(Point p)
{
return Circle(m_pCenter->getX() + p.getX(), m_pCenter->getY() + p.getY(), m_radius);
}
Point *getCenter() { return m_pCenter; }
void setCenter(float x, float y) { m_pCenter->setX(x); m_pCenter->setY(y); }
float getRadius() { return m_radius; }
void setRadius(float radius) { m_radius = radius; }
private:
float m_radius;
Point *m_pCenter;
};
int main()
{
Point pA(5, 5);
Point pB(5, 5);
Point pC = pA + pB;
cout << "Point pA(" << pA.getX() << ", " << pA.getY() << "), pB(" << pB.getX() << ", " << pB.getY() << "), pA + pB = pC(" << pC.getX() << ", " << pC.getY() << ")\n";
Circle c1(0, 0, 10);
cout << "c1's center : " << c1.getCenter()->getX() << ", " << c1.getCenter()->getY() << endl;
c1 = c1 + pC;
cout << "After c1 + pC: " << c1.getCenter()->getX() << ", " << c1.getCenter()->getY() << endl;
system("pause");
}
嘛 半夜睡不着无聊了,Point应该用结构更好的。
上面是一个完整程序。 main 函数执行结果: