Untitled

 avatar
unknown
plain_text
4 years ago
1.8 kB
10
Indexable
//
//  main.cpp
//  C++Tut
//
//  Created by Apurva, Vijay on 23/10/21.
//

#include <iostream>
#include "LogHeader.h"

class Point {
private:
    int x, y;
    
public:
    
    Point() : x(0), y(0) {
        std::cout << "Default Constructor - Called" << std::endl;
    }
    
    Point(int x, int y) : x(x), y(y) {
        std::cout << "Constructor - Called" << std::endl;
    }

    Point(Point&& p) noexcept : x(std::exchange(p.x, 0)), y(std::exchange(p.y, 0)) {
        std::cout << "Move Constructor - Called" << std::endl;
    }

    Point(const Point& p) : x(p.x), y(p.y) {
        std::cout << "Copy Constructor - Called" << std::endl;
    }

    
    ~Point() {
        std::cout << "Default Destructor is - Called. x - " << x << " y - " << y << std::endl;
    }
    
    int getX() {
        return x;
    }
    
    int getY() {
        return y;
    }
    
    void setX(int x) {
        this->x = x;
    }
    
    void setY(int y) {
        this->y = y;
    }
    
    void print() {
        LOG("Values for x and y are : ");
        std::cout << "value of x is : " << x << std::endl;
        std::cout << "value of y is : " << y << std::endl;
    }
    
    Point&& operator+ (const Point& p2) {
        LOG("+ Operator overloading is called.");

        Point newP(0, 0);
        newP.setX(this->x + p2.x);
        newP.setY(this->y + p2.y);


        return std::move(newP);
    }

    Point& operator= (const Point& p2) {
        LOG("Assignment operator is being called.");

        this->x = p2.x;
        this->y = p2.y;

        return *this;
    }
    
};

int main(int argc, const char * argv[]) {
    {
    Point p1(10, 20);
    Point p2(20, 40);
    
    Point p3 = p2 + p1;
    
    p3.print();
    return 0;
    }
    
}
Editor is loading...