C++
Important Links
What is this pointer?
this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++.
Example :-
#include <iostream>
using namespace std;
class Employee {
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
Employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee
e1.display();
e2.display();
return 0;
}
In C++, this pointer is used to represent the address of an object inside a member function. For example, consider an object obj calling one of its member function say method() as obj.method(). Then, this pointer will hold the address of object obj inside the member function method(). The this pointer acts as an implicit argument to all the member functions.
Example :-
class ClassName {
private:
int dataMember;
public:
method(int a) {
// this pointer stores the address of object obj and access dataMember
this->dataMember = a;
... .. ...
}
}
int main() {
ClassName obj;
obj.method(5);
... .. ...
}
Applications of this pointer :-
1. Return Object :- One of the important applications of using this pointer is to return the object it points. For example, the statement.
return *this;
inside a member function will return the object that calls the function.
2. Method Chaining After returning the object from a function, a very useful application would be to chain the methods for ease and a cleaner code.
For example,
positionObj->setX(15)->setY(15)->setZ(15);
Here, the methods setX, setY, setZ are chained to the object, positionObj. This is possible because each method return *this pointer.
This is equivalent to
positionObj->setX(15);
positionObj->setY(15);
positionObj->setZ(15);
3. Distinguish Data Members :- Another application of this pointer is distinguishing data members from local variables of member functions if they have same name. For example,
Example :-
Example 1: C++ program using this pointer to distinguish local members from parameters.
#include <iostream>
#include <conio.h>
using namespace std;
class sample
{
int a,b;
public:
void input(int a,int b)
{
this->a=a+b;
this->b=a-b;
}
void output()
{
cout<<"a = "<
}
};
int main()
{
sample x;
x.input(5,8);
x.output();
getch();
return 0;
}
Example :-
#include <iostream>
#include <conio.h>
using namespace std;
class student
{
char name[100];
int age,roll;
float percent;
public:
void getdata()
{
cout<<"Enter data"<
cout<<"Name:";
cin>>name;
cout<<"Age:";
cin>>age;
cout<<"Roll:";
cin>>roll;
cout<<"Percent:";
cin>>percent;
cout<
}
student & max(student &s1,student &s2)
{
if(percent>s1.percent && percent>s2.percent)
return *this;
else if(s1.percent>percent && s1.percent>s2.percent)
return s1;
else if(s2.percent>percent && s2.percent>s1.percent)
return s2;
}
void display()
{
cout<<"Name:"<
cout<<"Age:"<
cout<<"Roll:"<
cout<<"Percent:"<
}
};
int main()
{
student s,s1,s2,s3;
s1.getdata();
s2.getdata();
s3.getdata();
s=s3.max(s1,s2);
cout<<"Student with highest percentage"<
s.display();
getch();
return 0;
}
Enter data
Name:Paul
Age:24
Roll:11
Percent:79
Enter data
Name:Reem
Age:21
Roll:9
Percent:87
Enter data
Name:Philip
Age:23
Roll:5
Percent:81
Student with highest percentage
Name:Reem
Age:21
Roll:9
Percent:87
Example :-
#include <iostream>
#include <conio.h>
using namespace std;
class Demo {
private:
int num;
char ch;
public:
void setMyValues(int num, char ch){
this->num =num;
this->ch=ch;
}
void displayMyValues(){
cout<
cout<
}
};
int main(){
Demo obj;
obj.setMyValues(100, 'A');
obj.displayMyValues();
return 0;
}