We know that pointer is a variable which holds address of some other variable an we can access the data of another variable using pointer technique.
'this' is special type of pointer which holds the address of objects which invokes the member function of the class.
Whenever any object calls its member function, 'this' pointer is automatically set and contains the address of that object. In other words , whenever a member function gets called , an address of the object get passed to the function and this address gets collected in the 'this' pointer.
Hence 'this' pointer acts as an implicit argument to all the member function of the class and it is automatically passed to the member function when member function of class is called.
In operator overloading , we have been implicitly using the pointer 'this' to the member function.
When a unary operator is overloaded using a member function , we pass no argument to the function. When a binary operator is overloaded using a member function , we pass only one argument to the function and other argument is implicitly passed using pointer 'this'
#include<conio.h>
#include<iostream.h>
class abc
{
int a;
public:
void accept(int a1)
{
this->a=a1;
}
void display( )
{
cout<<"\n\tAddress of calling object is--->"<<this;
cout<<"\n\tValue of a is--->"<<this->a;
}
};
void main( )
{
abc A1;
clrscr( );
cout<<\n\tAddress of object is--->"<<&A1;
A1.accept(10);
A1.display( );
getch( );
}
Output:-
Address of object is--->0⤫8f9afff4
Address of calling object is--->0⤫8f9afff4
Value of a is--->10
One important application of 'this' pointer is to return the object it points to.
The Statement
return *this
inside a member function will return the object that invokes the function. This statement assumes importance when we want to compare two or more objects inside a member function and return the invoking object as a result.
#include<conio.h>
#include<iostream.h>
#include<string.h>
class person
{
char name[20];
int age;
public:
person(char nm[20],float ag)
{
strcpy(name,nm);
age=ag;
}
person *great(person *x)
{
if(x->age>=age)
return x;
else
return this:
}
void display( )
{
person p1("Irawen",101);
person p2("Pirawen",102);
clrscr( );
person *p=p1.great(&p2);
p->display( );
getch( );
}
Output:-
Name is--->Irawen Age is--->101
'this' is special type of pointer which holds the address of objects which invokes the member function of the class.
Whenever any object calls its member function, 'this' pointer is automatically set and contains the address of that object. In other words , whenever a member function gets called , an address of the object get passed to the function and this address gets collected in the 'this' pointer.
Hence 'this' pointer acts as an implicit argument to all the member function of the class and it is automatically passed to the member function when member function of class is called.
In operator overloading , we have been implicitly using the pointer 'this' to the member function.
When a unary operator is overloaded using a member function , we pass no argument to the function. When a binary operator is overloaded using a member function , we pass only one argument to the function and other argument is implicitly passed using pointer 'this'
#include<conio.h>
#include<iostream.h>
class abc
{
int a;
public:
void accept(int a1)
{
this->a=a1;
}
void display( )
{
cout<<"\n\tAddress of calling object is--->"<<this;
cout<<"\n\tValue of a is--->"<<this->a;
}
};
void main( )
{
abc A1;
clrscr( );
cout<<\n\tAddress of object is--->"<<&A1;
A1.accept(10);
A1.display( );
getch( );
}
Output:-
Address of object is--->0⤫8f9afff4
Address of calling object is--->0⤫8f9afff4
Value of a is--->10
One important application of 'this' pointer is to return the object it points to.
The Statement
return *this
inside a member function will return the object that invokes the function. This statement assumes importance when we want to compare two or more objects inside a member function and return the invoking object as a result.
#include<conio.h>
#include<iostream.h>
#include<string.h>
class person
{
char name[20];
int age;
public:
person(char nm[20],float ag)
{
strcpy(name,nm);
age=ag;
}
person *great(person *x)
{
if(x->age>=age)
return x;
else
return this:
}
void display( )
{
person p1("Irawen",101);
person p2("Pirawen",102);
clrscr( );
person *p=p1.great(&p2);
p->display( );
getch( );
}
Output:-
Name is--->Irawen Age is--->101
0 Comments:
Post a Comment