-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_pointers_to_objects.cpp
More file actions
26 lines (26 loc) · 908 Bytes
/
01_pointers_to_objects.cpp
File metadata and controls
26 lines (26 loc) · 908 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
// Write a class with an integer data member, a function to input and a function to display it. Create an object of the class using pointer and class its member functions.
#include<iostream>
using namespace std;
class Test
{
private:
int n;
public:
void in()
{
cout<<"Enter number: ";
cin>>n;
}
void out()
{
cout<<"The value of n = "<<n;
}
};
int main()
{
Test *ptr; // pointer to an object of class Test
ptr = new Test; // dynamically create object using 'new'. new Test dynamically creates an object of type Test in heap memory (not stack).The address of that object is returned. ptr stores that address.Now ptr points to the new object.
ptr ->in(); // * (Dereference Operator) Used to access the value stored at an address. call input function through pointer
ptr ->out(); // call output function through pointer
return 0;
}