Explain this pointer in c++

'this' pointer :-

C++ uses a unique keyword called "this" to represent an object that invokes a member function. 'this' is a pointer that points to the object for which this function was called. This unique pointer is called and it passes to the member function automatically. The pointer this acts as an implicit argument to all the member function, for e.g.

class ABC
{
int a ;
--------
--------
};


The private variable ‘a’ can be used directly inside a member function, like

a=123;

We can also use the following statement to do the same job.

this → a = 123

program:-

class stud
{
int a; public:
void set (int a)
{
this → a =  a; //here this point is used to assign a class level
} ‘a’ with the argument ‘a’ 
void show ( )
{
cout << a;
}
};
main ( )
{
stud S1, S2;
S1.bet (5) ;
S2.show ( );
}
Output:
5
 

Md Ashraf

'KNOWLEDGE WITH ASHRAF' is the platform where you find all the type of knowledge especially on programming based. Our goal is to give you a deeper grasp of technology in specifics that will help you increase your knowledge.

Previous Post Next Post

Contact Form