Wednesday, 4 April 2018

'this' Pointer

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

0 Comments:

Post a Comment

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses