“this” pointer:(special pointer)
*“this” is a special type pointer variable which contains the address of the current enable instance.
*That is it represents an object that invoke a member function.
*For example,the functin calls “S.read();” will set he pointer “this” to the address of the object S.
*This unique pointer is automatically passed to a function when it is called.
*The pointer “this” act as an implicit argument to the entire member functions.
*A friend function does not have a “this” pointer.
Program1:
#include<iostream.h>
class TestThis
{
public:
void showThis()
{
cout<<”Address of this is:”<<this<<endl;
}
};
void main()
{
TestThis T1,T2;
cout<<”Address of Object T1 is:”<<&T1<<endl;
T1.showThis();
cout<<”Address of Object T2 is:”<<&T2<<endl;
T2.showThis();
}
Program 2:
#include<iostream.h>
class Money
{
int Rs,Ps;
public:
Money() { }
Money( int Rs,int Ps)
{
this ->Rs=Rs;
this->Ps=Ps;
}
void show()
{
cout<<”Rs:”<<Rs;
cout<<”Ps:”<<Ps<<endl;
}
~Money()
{
cout<<”Memory releasing”<<endl;
}
};
void main()
{
Money *M1; //Pointer Object.
M1=new Money (12,40);
M1->show();
delete M1;
}
|
|
|
