This Pointer2
This Pointer2
2: #include <iostream>
3: using namespace std;
4:
5: class Student
6: {
7: private://data members
8: int Age;
9:
10: public://class methods
11: Student(int A);
12: void PrintInfo() const;
13: };
14:
15: Student::Student(int A)
16: {
17: Age=A;//Modification of data member
18: }
19:
20: void Student::PrintInfo() const
21: {
22: //using member or get function to accesss class data member
23: cout<<"Student's Age is: "<<Age<<"Yrs"<<endl;
24:
25: //using "this" pointer and "arrow operator" read the value of
26: //the variable of the current object
27: cout<<"Student's Age is: "<<this->Age<<"Yrs"<<endl;
28:
29: //using dereferencing a pointer i.e reading on the address of
30: //memory location of the current object
31: cout<<"Student's Age is: "<<(*this).Age<<"Yrs"<<endl;
32: }
33:
34: int main()
35: {
36: Student st(22); //object creation and initialization
37: st.PrintInfo(); // calling a member function
38: cout<<"Address of the corrent object is: "<<&st<<endl;
39: return 0;
40: }