-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis pointer 2.cpp
More file actions
44 lines (32 loc) · 898 Bytes
/
this pointer 2.cpp
File metadata and controls
44 lines (32 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;
class this_pointer{
public:
this_pointer add(int a) // method to add a value to the member variable pass by value
{
this->a=a+a; // using this pointer to access the member variable
cout<<"a: "<<this->a<<endl;
return *this;
}
this_pointer sub(int a) {
this->a=a-a;
cout<<"a: "<<this->a<<endl;
return *this;
}
/*
advantages of using this pointer:
1. It allows you to refer to the current object within a member function.
2. It helps to resolve naming conflicts between member variables and parameters.
3. It can be used to return the current object from a method, enabling method chaining.
4. It provides a way to access member variables and methods of the current object.
5. It can be used to pass the current
*/
private:
int a;
};
int main()
{
this_pointer A;
A.add(5).sub(10);
return 0;
}