0% found this document useful (0 votes)
2 views1 page

This Pointer2

The document presents a C++ program that defines a 'Student' class with a private data member 'Age' and methods for initializing and printing the student's age. It demonstrates the use of the 'this' pointer and dereferencing to access class data members. The main function creates a 'Student' object and calls the PrintInfo method to display the student's age and the object's address.

Uploaded by

Emmanuel Mrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

This Pointer2

The document presents a C++ program that defines a 'Student' class with a private data member 'Age' and methods for initializing and printing the student's age. It demonstrates the use of the 'this' pointer and dereferencing to access class data members. The main function creates a 'Student' object and calls the PrintInfo method to display the student's age and the object's address.

Uploaded by

Emmanuel Mrigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1: // 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: }

You might also like