×

This keyword in C++

  • This keyword in C++ is a pointer which points to the object.
  • It is passed as an argument to functions which helps in accessing the object. It is used for a non-static function.
  • There are different ways in which we can use this keyword in C++. They are
    1. To resolve the shadowing issue when this keyword is used.
    2. To access the data members of the current object.
    3. To access the currently executing object using this keyword.
    4. To call the member function of the current object.

To resolve the shadowing issue when this keyword is used:

  • Before getting into the program, we need to know what shadowing is.
  • Shadowing issue occurs when both the instance variable and local variable have the same name.
  • Let us consider a program in C++ which is used in case of shadowing issues:
/ / program in C++ for above case
# include< iostream >
Using namespace std;
Class demo {
string s;
public:
demo ( string s )
{
/ / using this keyword
/ / to initialize the value
this - > s = s;
}
void display ( ) {
cout << s << endl; } ];
/ / main function
int main ( )
{
Demo d(“Hello world “);
d.display ( );
return 0;
}


Output:

Hello world

To access the data members of the current object

  • By using this keyword, we can access the object and the data member associated with the object.
  • Let us consider a program in C++ for the above scenario:
/ / program in C++
/ / to access the object using this keyword
# include <iostream>
Using namespace std;
class demo 
{
String s;
public :
demo ( string s )
{
/ / using this keyword
this - > s = s;
}
Void display ( )
{ 
Cout << this - > s << endl;
}
};
/ /class driver
int main ( )
{
demo d (“Hello world”);
d.display ( );
return 0;
}

Output:

Hello world

To access the currently executing object using this keyword:

  • By using this keyword, we can access the currently executing object.
  • We can chain function, and object can be deleted with the help of the member function.
/ / program in C++ for above scenario
# include <iostream>
Using namespace std:
class demo {
string s;
public :
demo (string s) {
/ / using this keyword to assign value 
this - > s = s;
}
Void display ( ) {
cout << s << endl;
}
void delete ( ) {
delete this;
} };
/ / main function
int main ( ) {
demo *d = new demo (“Hello world “);
d - > display ( );
d - > delete ( );
return 0;
}

Let us consider a program in C++ for the above scenario:

Output:

Hello world

To call the member function of the current object:

  • This keyword is used to access the object of the class.
  • It is also used to access the function associated with the object.
  • Let us consider a program in C++:
/ / program in C++ using this keyword
/ / to call the function of current object
# include <iostream >
using namespace std;
class demo 
{
String s;
public:
demo ( string s )
{
/ / using this keyword 
this - > s = s;
}
void dis ( int );
void display ( );
};
void demo : : dis (int x)  {
/ / accessing the function the current object
for( int i = 0; i < x ; i++)  {
this - > display ( ); }
}
Void demo  : : display ( )  {
/ / access the string data member
Cout << this - > s <<endl;  }
/ / main function
int main ( ) {
demo d (“Hello world “);
d.dis ( 5 );
return 0;
}

Output:

Hello world
Hello world
Hello world
Hello world
Hello world
  • Hence, we can conclude that this keyword can be used to To resolve the shadowing issue when this keyword is used.
  • It is used to access the data member of the current class.
  • To access the object which is currently executing.
  • To call the function associated with the object.

Related Topics

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

Snake Code in C++

Snake is a popular game that can be played on almost any device and runs on any operating system. In this game, snakes can move in any direction, including left,...

4 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

Difference between "int main( ) and int main(void)" in C/C++

int main( ) function In the C/C++ programming language, int main() indicates a function that returns an integer at the end of the program execution. In general, a value of '0' indicates...

3 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

Program that produces different results in C and C++

Introduction: There are many such programs that compile run both in C and C++ but give different outcomes when compiled by the C and C++ compilers. There are a variety of such...

6 minutes read.

How to create a library in C++

Before going on to the creation of a library, let’s understand its meaning. What is a library? In simple words, a library is a collection of numerous functions, methods, classes, header files...

7 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

4 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to...

2 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.