Monday, September 5, 2011

this pointer in C++ Programs


Normally pointers can hold the address of it’s own type of variable. this is a keyword used to denote a pointer which can hold the address of any type of location. In C++ programs, this pointers are used to hold the address of objects of any class.

#include< iostream.h >
#include< conio.h >
class Exam
{
private:
int i;
public:
void setData (int a)
{
this->i=a;
cout<< "Address of the object is: -"<< this;
}
void show ()
{
cout<< "\nData member="<< this->i;
}
};
void main ()
{
clrscr ();
Exam s1, s2;
int i;
cout<< "\nEnter value for 1st object: -";
cin>>i;
s1.setData (i);
s1.show ();
cout<< "\nEnter value for 2nd object: -";
cin>>i;
s2.setData (i);
s2.show ();
getch ();
}


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner