×

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

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a...

2 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

Maps in C++

Maps: Maps in C++ are the containers associated with key and mapped values. By keys and mapped values, we mean that the maps are used to store elements formed by the...

4 minutes read.

How to run program in turbo c++

What is Turbo C++? Turbo c++ is an integrated development environment (IDE) and a compiler to run C++ code. Turbo c++ helps to link the header files with the main code....

2 minutes read.

C++ STL (Standard Template Library)

Introduction C++ is a flexible type and general proposed programming language. So we need a standard library that supports C++. C++ STL (Standard Template Library) is a collection of templates that...

6 minutes read.

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.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Similarities between C++ and Java

People who are preparing for software engineering roles must have come across either one of these languages because as all the companies do not accept python for hiring a fresher...

3 minutes read.

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an...

7 minutes read.

Bits stdc++.h in C++

<bits/stdc++.h> in C++ In essence, it is a header file that contains all the standard libraries. It makes sense to use this file in programming competitions to speed up work, especially...

2 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

C++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.