Loading....
Coupon Accepted Successfully!

 

The this Pointer

Use of this pointer is now outdated. Objects are used to invoke the non-static member function of the class. For example, if p is an object of class P and get() is a member function of P, the statement p.get() is used to call the function. The statement p.get() operates on p. In the same way, if ptr is a pointer to a P object, the function called ptr->get() operates on *ptr.
 
However, the question is, how does the member function get() to understand which p it is functioning on? C++ compiler provides get() with a pointer to p called this. The pointer this is transferred as an unseen parameter in all calls to non-static member functions. The keywordthis is a local variable that is always present in the body of any non-static member function.
 
The keyword this does not need to be declared. The pointer is rarely referred to explicitly in a function definition. However, it is used implicitly within the function for member references. For example, if p.get(k) is called, where k is a member of P, the keyword this is set to &p and k is set to this->k, which is equivalent to p.k.

 

13.13 Write a program to use this pointer and return pointer reference.

#include<iostream.h>

#include<conio.h>

class number

{

int num;

public :

void input()

{

cout<<“\n Enter a Number : ”;

cin>>num;

}

void show()

{ cout<<“\n The Minimum Number : ”<<num; }

number min( number t)

{ if (t.num<num)

return t;

else

return *this; }

};

void main()

{

clrscr();

number n,n1,n2;

n1.input();

n2.input();

n=n1.min(n2);

n.show();

}

OUTPUT

Enter a Number : 152

Enter a Number : 458

The Minimum Number : 152

 

Explanation: In the above program, the class number contains one integer number variable num. The class also contains member functions input()show() and min(). The input() function reads an integer through the keyboard. The show() function displays the contents on the screen. The min() function finds a minimum number out of two. The variables n, n1, and n2 are objects of class number.
 
In function main(), the objects n1 and n2 call the function input() and read integers. Both the objects n1 and n2 are passed to function min() with the statement n = n1.min(n2). The object n receives the returned value by the function min(). The object n1 calls the function min(). The object n2 is passed as an argument in the function min().
 
In function num(), the formal object t receives the contents of argument n2. In the same function, the object n1 is also accessible. As studied earlier, the pointer this is present in the body of every non-static member function, and it points to the object n1. Using the pointer this, we can access the individual member variable of object n1. The if statement compares the two objects, and the return statement returns the smaller object.

 

13.14 Write a program to enter name and age of two persons. Find the elder person. Use this pointer.

#include<iostream.h>

#include<conio.h>

class name

{

char str[15];

int age;

public:

void input()

{

cout<<“\n Enter Name and age : ”;

cin>>str;

cin>>age;

}

void show()

{

cout<<“\n\nElder person ”;

cout<<“\n Name: ”<<str;

cout<<“\n Age: ”<<age;

}

name display( name x)

{

cout<<“\n\n Contents of object n1 (this pointer)”;

cout<<“\n Name:” <<this->str;

cout<<“\n Age:” <<this->age;

cout<<“\n\n Contents of object n2 (x)”;

cout<<“\n Name: ” <<x.str;

cout<<“\n Age: ” <<x.age;

if (this->age>x.age)

return *this;

else

return x;

}

};

void main()

{

clrscr();

name n,n1,n2;

n1.input();

n2.input();

n=n1.display(n2);

n.show();

}

OUTPUT

Enter Name and age : Mahesh 25

Enter Name and age : Suresh 30

Contents of object n1(this pointer)

Name : Mahesh

Age : 25

Contents of object n2 (x)

Name : Suresh

Age : 30

Elder person

Name : Suresh

Age : 30

 

Explanation: The above program is similar to the previous one. Here, contents of explicitly and implicitly passed objects are displayed. The this pointer points to the hidden argument (implicit argument). The if statement compares the member variable age of both the objects and returns the greater one. The object n collects the return value of function display() and calls the function show(). The function show() displays the contents of object n. Working of this pointer is illustrated in Figure.

 

29188.png
Fig: Working of this pointer




Test Your Skills Now!
Take a Quiz now
Reviewer Name