Q:- create a class in which you will declare a pointer to int . This variable will be defined n allocated memory in the constructor. Create a destructor of the class where this memory will be deallocated.
Using this class do the following:
1) In the main function create an instance of this class
2) Create another function outside main function( lets call it foo) and pass it by value the object created in the main function.
3) Once foo is finished, try to access the int* defined within class in the main and see what happens ?
4) Do you get any problem at the end of the program ? If yes, why ?
5) Try to correct this problem using copy constructor
Program:
//#include <iostream>
using namespace std;
class salu
{
public:
int* ptr;
///////////////////////// constructor//////////////////////////
salu()
{
ptr= new int;
}
//////////////////////////destructor///////////////////////////
~salu()
{
delete ptr;
}
///////////////////////copy constructor/////////////////////////
salu(const salu &d)
{
int* ptr;
ptr = new int;
ptr=d.ptr;
}
};
//////////////////////////Function Foo//////////////////////////
void foo(salu x)
{
}
////////////////////// Start of main/////////////////////////////
void main()
{
salu a;
foo(a);
cout<<a.ptr<<endl;
}