1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
class Point
{
private:
int x;
int y;
public:
Point() : x(0), y(0) {}
Point(int x, int y) : x(x), y(y) {}
public:
friend std::ostream& operator<<(std::ostream& stream, Point& p);
Point& operator= (const Point &a);
};
std::ostream &operator<<(std::ostream &stream, Point &p)
{
stream << "(" << p.x << ", " << p.y << ")";
return stream;
}
Point& Point::operator= (const Point& a)
{
x = a.x;
y = a.y;
return *this;
}
class Line : public Point
{
private:
Point p1;
Point p2;
public:
Line() : p1(Point(0, 0)), p2(Point(0, 0)) {}
Line(const Point & p1, const Point & p2) : p1(p1), p2(p2) {}
public:
void Draw();
friend std::ostream& operator<<(std::ostream& stream, Line& line);
Line& operator= (const Line&);
};
void Line::Draw()
{
std::cout << "Line drawing\n";
}
std::ostream& operator<<(std::ostream& stream, Line& line)
{
std::cout << "Line: " << line.p1 << ", " << line.p2 << '\n';
return stream;
}
Line& Line::operator= (const Line& a)
{
p1 = a.p1;
p2 = a.p2;
return *this;
}
int main() //Cannot change
{
Line l1; // no output
std::cout << l1; //Output: Line (0,0) (0,0)
l1.Draw(); //Output: Line drawing
Line l2(Point(),
Point(100, 100)); // no output
std::cout << l2; //Output: Line (0,0) (100,100)
l1 = l2;
std::cout << l1; //Output: Line (0,0) (100,100)
}
|