C++ Program to Show Use of This Keyword in Class
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++:
- Resolve Shadowing Issue Using this Keyword.
- Access Currently Executing Object Using this Keyword.
- Access Data Members Using this Keyword.
- 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;
}
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;
}
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;
}
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;
}
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;
}
OutputGeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
Similar Reads
C++ Program to Perform Calculations in Pure Strings Given a string of operations containing three operands for each operation "type of command", "first operand", and "second operand". Calculate all the commands given in this string format. In other words, you will be given a pure string that will ask you to perform an operation and you have to perfor
5 min read
How to Use typename Keyword in C++? In C++, the "typename" is a keyword that was introduced to declare a type. It is used for specifying that the identifier that follows is a type rather than a static member variable. In this article, we will learn how to use the typename keyword in C++. C++ typename KeywordThe typename keyword is mai
2 min read
C++ Program to Create an Interface Interfaces are a feature of Java that allows us to define an abstract type that defines the behaviour of a class. In C++, there is no concept of interfaces, but we can create an interface-like structure using pure abstract classes. In this article, we will learn how to create interface-like structur
2 min read
Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo
5 min read
How to Overload the (+) Plus Operator in C++? In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo
2 min read
Comparator Class in C++ with Examples Comparator Classes are used to compare the objects of user-defined classes. In order to develop a generic function use template, and in order to make the function more generic use containers, so that comparisons between data can be made. Syntax cpp class comparator_class { public: // Comparator func
5 min read
How to Find the Type of an Object in C++? In C++, every variable and object has a type. The type of an object determines the set of values it can have and what operations can be performed on it. Itâs often useful to be able to determine the type of an object at runtime, especially when dealing with complex codebases. In this article, we wil
2 min read
How to Declare a Global Variable in C++? In C++, global variables are like normal variables but are declared outside of all functions and are accessible by all parts of the function. In this article, we will learn how to declare a global variable in C++. Global Variable in C++ We can declare a global variable, by defining it outside of all
2 min read
How to Declare a Static Variable in a Class in C++? In C++, a static variable is initialized only once and exists independently of any class objects so they can be accessed without creating an instance of the class. In this article, we will learn how to declare a static variable in a class in C++. Static Variable in a Class in C++To declare a static
2 min read
How to Add Message to Assert in C++ In C++, assert is a debugging tool that allows the users to test assumptions in their code. The standard assert macro provided by <cassert> checks the condition and if the condition is evaluated as false, the assert statement terminates the program and prints an error message on the console al
2 min read