Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string>
- class C {
- private:
- int x = 10;
- public:
- C() {
- std::cout << "Build C without x" << std::endl;
- }
- C(int _x) : x {_x } {
- std::cout << "Build C with x" << std::endl;
- }
- C sub(const C& c) const {
- int x_value = x - c.x;
- return C(x_value);
- }
- int getX() const {
- return x;
- }
- C operator-() const {
- int new_x = -getX();
- return C(new_x);
- }
- C operator+() const {
- int new_x = getX();
- return C(new_x);
- }
- C& operator-=(const C& c) {
- x -= c.x;
- return *this;
- }
- friend C operator+(const C& left, const C& right);
- friend C operator-(const C& left, const C& right);
- };
- C operator+(const C& left, const C& right) {
- return C(left.getX() + right.getX());
- }
- C operator-(const C& left, const C& right) {
- return C(left.getX() - right.getX());
- }
- int main()
- {
- C a;
- C b;
- auto var = a + b;
- std::cout << var.getX() << std::endl;
- var -= a;
- std::cout << var.getX() << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement