-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer-to-object-members.cpp
55 lines (45 loc) · 1.6 KB
/
pointer-to-object-members.cpp
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
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
class ABC {
public:
int data;
void getData() {
std::cout << std::endl << "Enter a data: ";
std::cin >> data;
std::cout << "The value of data after you entered: " << data;
}
};
int main() {
//
// using pointer of int data type.
//
//declaring an object of class ABC
ABC obj;
// declaring a pointer of int data type
int *p;
//modifing the value of a public variable of class
obj.data = 4;
//saving the address of variable
p = &obj.data;
//printing out the value of variable using int data type pointer.
std::cout << "\nThe value of data after modified by pointer of int data type: " << *p;
//
// using pointer of a class data type.
//
//declaring pointer to an object of a class ABC
ABC *q;
//copying the address of an object;
q = &obj;
//calling the method using pointer to an object
q->getData();
//modifying the variable using pointer to an object
q->data = 5;
//printing out the value of variable using pointer to an object.
std::cout << "\nThe value of data after modified by pointer to an object: " << q->data;
return 0;
}
/*
In the above program we have two pointers one is integer pointer and other is class type pointer both are used to point to object members. Thus both data members and member functions can be accessed via pointer also, just by giving them, reference of a class they belong to.
For invoking Member function through pointer we use pointer to pointer Member function operator (->)
You can assign the address of a public member of an object to a pointer.
When accessing members of a class given a pointer to an object, use Arrow(->) operator.
*/