this pointer in C++ programming
C++ uses a unique keyword called this to represent an object that invokes member function. this is a pointer that points to the object for which this function was called.
// C++ program on this pointer.
#include <iostream>
using namespace std;
class Employe
{
public:
int id;
string name;
float salary;
Employe(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main()
{
Employe a1 =Employe(11, "Rakesh", 120000); //creating an object of Employee
Employe a2=Employe(12, "Bunty", 65000); //creating an object of Employee
a1.display();
a2.display();
return 0;
}
OUTPUT:
11 Rakesh 120000
12 Bunty 65000
C++ uses a unique keyword called this to represent an object that invokes member function. this is a pointer that points to the object for which this function was called.
// C++ program on this pointer.
#include <iostream>
using namespace std;
class Employe
{
public:
int id;
string name;
float salary;
Employe(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main()
{
Employe a1 =Employe(11, "Rakesh", 120000); //creating an object of Employee
Employe a2=Employe(12, "Bunty", 65000); //creating an object of Employee
a1.display();
a2.display();
return 0;
}
OUTPUT:
11 Rakesh 120000
12 Bunty 65000