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
| #include <iostream>
using namespace std;
// 2D
class Point2D
{
public:
double x;
double y;
Point2D operator + (const Point2D &);
};
Point2D Point2D::operator + (const Point2D & p)
{
Point2D out = p;
out.x += x;
out.y += y;
return out;
}
// 3D
class Point3D : public Point2D
{
public:
double z;
Point3D operator + (const Point3D &);
};
Point3D Point3D::operator + (const Point3D & p)
{
Point3D out = p;
out.Point2D::operator + (*this);
out.z += z;
return out;
}
int main (void)
{
Point2D a,b,c;
Point3D d,e,f;
a.x = 4; a.y = 6;
b.x = 8; b.y = 1;
c = a + b;
cout << "(" << a.x << ";" << a.y << ")" << endl;
cout << "(" << b.x << ";" << b.y << ")" << endl;
cout << "(" << c.x << ";" << c.y << ")" << endl;
cout << endl;
d.x = 2; d.y = 9; d.z = 7;
e.x = 3; e.y = 4; e.z = 5;
f = d + e;
cout << "(" << d.x << ";" << d.y << ";" << d.z << ")" << endl;
cout << "(" << e.x << ";" << e.y << ";" << e.z << ")" << endl;
cout << "(" << f.x << ";" << f.y << ";" << f.z << ")" << endl;
cout << endl;
return 0;
} |
Partager