how to use this keyword c++ code example

Example: this keyword in cpp

this is a keyword that refers to the current instance of the class. 
 There can be 3 main usage of this keyword in C++. It can be used to 
 pass current object as a parameter to another method. 
 It can be used to refer current class instance variable.
   
#include <iostream>
using namespace std;
class Demo {
private:
  int num;
  char ch;
public:
  void setMyValues(int num, char ch){
    this->num =num;
    this->ch=ch;
  }
  void displayMyValues(){
    cout<<num<<endl;
    cout<<ch;
  }
};
int main(){
  Demo obj;
  obj.setMyValues(100, 'A');
  obj.displayMyValues();
  return 0;
}

Output:- 
  	100
	A

Tags:

Cpp Example