Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C++ Compiler.
- Code, Compile, Run and Debug C++ program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- class MyReal {
- public:
- float x;
- float y;
- public:
- MyReal(): x(0), y(0) { }
- MyReal(const float z) {
- this->x = z;
- this->y = z;
- }
- MyReal(const float x, const float y) {
- this->x = x;
- this->y = y;
- }
- // Способ 1
- public:
- MyReal operator+(const MyReal &rhs) const {
- return MyReal(rhs.x + y, rhs.x - y);
- }
- MyReal operator+() const {
- return MyReal(x + y);
- }
- // Способ 2
- public:
- friend MyReal operator+(const MyReal &r) {
- return MyReal(r.x + r.y);
- }
- friend MyReal operator+(const MyReal &lhs, const MyReal &rhs) const {
- return MyReal(lhs.x + rhs.y, lhs.x - rhs.y);
- }
- };
- #include <iostream>
- using namespace std;
- int main()
- {
- MyReal x(2, 5);
- MyReal y(2, 5);
- x+y;
- cout<<x.x;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement