class vector int x y z public vector
Перегрузка операций class vector { int x, y, z; public: vector operator+(vector t); vector operator=(vector t); vector operator++(int); void show(){cout<<"x = "<<x<<" | y= "<<y<<"| z = "<<z<<"n"; }; void assign (int mx, int my, int mz ) { x=mx; y=my; z=mz; } }; vector: : operator+(vector t) { vector temp; temp. x = x+t. x; temp. y = y+t. y; temp. z = z+t. z; return temp; }; vector: : operator=(vector t) { x=t. x; y=t. y; z=t. z; return *this; }; vector: : operator++(int) { x++; y++; z++; return *this; }; vector: : operator++() { ++x; ++y; ++z; return *this; };
Перегрузка операций int main() { vector a, b, c; a. assign(1, 2, 3); b. assign(10, 10); a. show(); b. show(); c=a+b; c. show(); c=a+b+c; c. show(); c=b=a; c. show(); a=c. operator++(1); b. show(); c++; a. show(); c. show(); ++c; b=++c; c. show(); b. show(); c. show(); Результат работы программы: x=1 y=2 z=3 x=10 y=10 z=10 x=11 y=12 z=13 x=22 y=24 z=26 x=1 y=2 z=3 x=2 y=3 z=4
- Slides: 7