Shallow copy

Write a program to demonstrate the use of Shallow copy.

#include<iostream>
using namespace std;
class A
{
	private:
		int *x;
	public:
		A()
		{
			cout << "default constructor" << endl;
		}
		A(int *p)
		{
			cout << "parameterized constructor" << endl;
			x = new int;
			*x = *p;
		}
		~A()
		{
			cout << "destructor" << endl;
		}
		void modify()
		{
			*x = 50;
		}
		void print()
		{
			cout << "*x = " << *x <<  endl;
		}
};
int main()
{
	int x = 21;
	A obj1(&x);
	A obj2 = obj1;
	obj1.print();
	obj2.print();
	obj1.modify();
	obj1.print();
	obj2.print();
	return 0;
}

Output

parameterized constructor
*x = 21
*x = 21
*x = 50
*x = 50
destructor
destructor