#include <iostream>
#include <cstring>
using std::ostream;
using std::cout; using std::endl;
const int size = 25;
class Vec
{private:
int x, y;
char name[size];
public:
Vec(){ x = 0, y = 0;
strncpy_s(name, size, "Default", size);
cout << " [In Dconstructor], object created: " << name <<
"(" << x << "," << y << ") address: " << this << "\n\n";
}
Vec(const char*desc) { x = 0, y = 0;
strncpy_s(name, size, desc, size);
cout << " [In Constructor], object created: " << name <<
"(" << x << "," << y << ") address: " << this << "\n\n";
}
Vec(int x0, int y0, const char *desc){ x = x0, y = y0;
strncpy_s(name, size, desc, size);
cout << " [In Constructor], object created: " << name <<
"(" << x << "," << y << ") address: " << this << "\n\n";
}
Vec(const Vec& original)
{ x = original.x; y = original.y;
strncpy_s(name, size, "Temp", size);
cout << " [In Copy Constructor], object created: " << name <<
"(" << x << "," << y << ") address: " << this << "\n\n";
}
~Vec(){cout<<" Destroying "<<(*this).name<<", Address: "<<this<<"\n\n";}
Vec operator+(const Vec& right)
{ Vec sum;
sum.x = x + right.x; sum.y = y + right.y;
strncpy_s(sum.name, size, "sum", size);
cout <<" [In OP+]: " << sum.name << "(" << sum.x << "," << sum.y <<
") = " << (*this).name <<"(" << (*this).x << "," << (*this).y <<
") + " << right.name <<"(" << right.x << "," << right.y << ")\n\n";
return sum;
}
Vec& operator=(const Vec& rhs)
{x = rhs.x; y = rhs.y;
strncpy_s(name, size, rhs.name, size);
cout << " [In OP=]: " << rhs.name << "(" << rhs.x << "," << rhs.y <<
") is copied into " << name << "(" << x << "," << y << ")\n\n ";
return *this;
}
friend ostream& operator<<(ostream& out, const Vec& v)
{out << " [OP<<] c contains: " << v.name << "(" << v.x << "," << v.y << ")\n\n"; return out; }
};
int main()
{ Vec a(1, 2, "vecA");
Vec b(3, 4, "vecB");
Vec c("vecC");
c = a + b;
cout << c << "\n\n";
return 0;
}