'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 ;
--------
--------
};
{
int a ;
--------
--------
};
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
{
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
Tags
C plus plus