How to access a member function by pointer?
To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this).
The second step, use arrow operator -> to access the member function using the pointer to the object.
Syntax:
//pointer to object declaration
class_name *pointe_name;
//memory initialization at runtime
pointer_name = new class_name;
//accessing member function by using arrow operator
pointer_name->member_function();
Example:
In the below example - there is a class named Number with private data member num and public member functions inputNumber(), displayNumber().
In the example, we are creating simple object N and a pointer to the object ptrN and accessing the member functions by using simple object N and the pointer to the object ptrN.
Program:
Output
Explanation:
The main three steps needs to be understood those are: