🟣 OOP · Lesson 41
this Pointer
this Pointer
What is this Pointer?
this Pointer
The this pointer stores the address of the current object. It is used to distinguish object members from parameters with the same name.
The this pointer stores the address of the current object. It is used to distinguish object members from parameters with the same name.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
this-pointer.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| this | This used in this Pointer programming. |
| current object | Current object used in this Pointer programming. |
| member access | Member access used in this Pointer programming. |
| pointer | Pointer used in this Pointer programming. |
Syntax / Pattern
this->member = parameter;
Example Program
#include <iostream>
using namespace std;
class Student{
int marks;
public:
void setMarks(int marks){ this->marks = marks; }
void show(){ cout << marks; }
};
int main(){ Student s; s.setMarks(89); s.show(); }
Expected Output
89
Program Explanation
- this->marks means the data member.
- marks without this is the parameter.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- setter methods
- method chaining
- operator overloading
Common Mistakes
- Using this in static function.
- Confusing this pointer with object name.
Practice Tasks
- Create setter using this pointer.
- Explain why this is useful when names are same.
Summary
this Pointer is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is this Pointer?
this Pointer
The this pointer stores the address of the current object. It is used to distinguish object members from parameters with the same name.
The this pointer stores the address of the current object. It is used to distinguish object members from parameters with the same name.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
this-pointer.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| this | This used in this Pointer programming. |
| current object | Current object used in this Pointer programming. |
| member access | Member access used in this Pointer programming. |
| pointer | Pointer used in this Pointer programming. |
Syntax / Pattern
this->member = parameter;
Example Program
#include <iostream>
using namespace std;
class Student{
int marks;
public:
void setMarks(int marks){ this->marks = marks; }
void show(){ cout << marks; }
};
int main(){ Student s; s.setMarks(89); s.show(); }
Expected Output
89
Program Explanation
- this->marks means the data member.
- marks without this is the parameter.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- setter methods
- method chaining
- operator overloading
Common Mistakes
- Using this in static function.
- Confusing this pointer with object name.
Practice Tasks
- Create setter using this pointer.
- Explain why this is useful when names are same.
Summary
this Pointer is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.