Skip to content
Related Articles

Related Articles

C++ Program to Show Use of This Keyword in Class

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Jul, 2022

Here, we will see how to use this keyword in a class using a C++ program. this keyword in C++ is an implicit pointer that points to the object of the class of which the member function is called. Every object has its own this pointer. Every object can reference itself by this pointer. 

There are 4 ways this keyword can be used in a class in C++:

  1. Resolve Shadowing Issue Using this Keyword.
  2. Access Currently Executing Object Using this Keyword.
  3. Access Data Members Using this Keyword.
  4. Calling Member Functions Using this Keyword.

Let’s start discussing these different ways in detail.

1. Resolve Shadowing Issue Using this Keyword

Shadowing occurs when there is a local variable that has the same name as an instance variable. Below is the C++ program to show how this keyword can be used to resolve the shadowing issues:

C++




// C++ program to use this keyword
// to resolve shadowing issue
#include <iostream>
using namespace std;
  
class GFG 
{
  string name;
    
  public:
  GFG(string name)
  {
    // Use this keyword to initialize value 
    // of class member name as the parameter 
    // name passed in the constructor.
    this->name = name;
  }
  void display() 
  
    cout << name << endl; 
  }
};
  
// Driver code
int main()
{
  GFG gfg("GeeksforGeeks");
  gfg.display();
  return 0;
}

Output

GeeksforGeeks

2. Access Currently Executing Object Using this Keyword

This keyword can be used to chain functions and delete objects via its member functions.

Example 1: Below is the C++ program to use this keyword to delete the object using its member functions.

C++




// C++ program to use this keyword 
// to delete object of the class
#include <iostream>
using namespace std;
  
class GFG 
{
  string name;
    
  public:
  GFG(string name)
  {
    // Use this keyword to assign value 
    // of class member name as the
    // parameter name passed in the 
    // constructor.
    this->name = name;
  }
    
  void display() 
  
    cout << name << endl; 
  }
    
  void del()
  {
    // Use this keyword to delete 
    // the object
    delete this;
  }
};
  
// Driver code
int main()
{
  GFG *gfg = new GFG("GeeksforGeeks");
  gfg->display();
  gfg->del();
  return 0;
}

Output

GeeksforGeeks

Example 2: Below is the C++ program to use this keyword to access currently executing object to chain function calls:

C++




// C++ program to use this keyword to 
// access currently executing object 
// to chain function calls:
#include <iostream>
using namespace std;
  
class GFG 
{
  string name;
  int data;
    
  public:
  GFG setName(string name)
  {
    this->name = name;
    return *this;
  }
    
  GFG setData(int data)
  {
    this->data = data;
    return *this;
  }
    
  void display()
  {
    cout << name << endl;
    cout << data << endl;
  }
};
  
// Driver code
int main()
{
  // Creating object
  GFG gfg;
    
  // chaining function calls
  gfg = gfg.setName("GeeksforGeeks").setData(20);
    
  gfg.display();
  return 0;
}

Output

GeeksforGeeks
20

3. Access Data Members Using this Keyword

Below is the C++ program to use this keyword to access the data member of the currently executing object:

C++




// Below is the C++ program to use
// this keyword to access the data 
// members of currently executing 
// object
#include <iostream>
using namespace std;
  
class GFG 
{
  string name;
    
  public:
  GFG(string name)
  {
    // Initialize value of class member 
    // name as the parameter name passed 
    // in the constructor.
    this->name = name;
  }
    
  void display()
  {
    // Accesses string data member name
    cout << this->name << endl;
  }
};
  
// Driver code
int main()
{
  GFG gfg("GeeksforGeeks");
  gfg.display();
  return 0;
}

Output

GeeksforGeeks

4. Calling Member Functions Using this Keyword

Below is the C++ program to use this keyword to call member functions associated with the currently executing objects:

C++




// C++ program to use this keyword
// to call member functions of currently 
// executing objects
#include <iostream>
using namespace std;
  
class GFG 
{
  string name;
    
  public:
  GFG(string name)
  {
    // Initialize value of class member 
    // name as the parameter name passed 
    // in the constructor.
    this->name = name;
  }
    
  void displayX(int);
  void display();
};
    
void GFG :: displayX(int x)
{
  for (int i = 0; i < x; i++) 
  {
    // Access member functions of currently
    // executing object
    this->display();
  }
}
    
void GFG :: display()
{
  // Accesses string data member name
  cout << this->name << endl;
}
  
// Driver code
int main()
{
  GFG gfg("GeeksforGeeks");
  gfg.displayX(4);
  return 0;
}

Output

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!