In C++, How To Use Constructor, Destructor, Copy Constructor And Assignment Operator Overloading.

#include<iostream>
using namespace std;
class Point{
	private:
		int x, y;
	public:
		//default
		Point(){
			cout << "Default constructor" << endl;
			set(0, 0);
		}
		//paramter constructor
		Point(int x, int y){
			cout << "Parameter constructor" << endl;
			set(x, y);
		}
		//copy constuctor 
		Point(Point &p){
			cout << "Copy constructor" << endl;
			set(p.x, p.y);
		}
		// copy assignment
		Point& operator = (Point &p){
			cout << "copy assignment operator" << endl;
			set(p.x, p.y);
			return *this;
		}
		void print(){
			cout << " x: " << x << ", y: " << y << endl;
		}
		void set(int x, int y){
			this->x = x;
			this->y = y;
			print();
		}
		~Point(){
			cout << "Destructor of point" << endl;
			print();
		}
};

int main(){
	Point p1(5, 6);
	Point p2(p1);
	p1.set(7,8);
	Point p3;
	p3 = p1;
	p1.set(1,3);
	p3.print();
}
Download

Comments

Popular posts from this blog