C++ Program using this pointer


In this program, You will learn how to implement this pointer in C++.


this->x = x

this->y = y

Example: How to implement this pointer in C++.

#include<iostream>
using namespace std;

class Test {
   int x, y;
public:

   Test(int x, int y) {
       this->x = x;
       this->y = y;
   }
   void display() {
       cout << "value of x is:" << x;
       cout << "\nvalue of y is:" << y;
   }
};

int main() {

   Test tt(10, 20);
   tt.display();

   return 0;
}

Output:

value of x is:10
value of y is:20