×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

Accessing Member Function by pointer in C++

C++ class | Accessing member function by pointer: Here, we are going to learn how to access a member function by using the pointer in C++?
Submitted by IncludeHelp, on September 28, 2018 [Last updated : March 01, 2023]

Create a class along with data member and member functions and then access the member functions by using a pointer in C++.

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.

C++ syntax to access member function by pointer

//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.

C++ program to access member function by pointer

#include <iostream>
using namespace std;

class Number {
private:
    int num;

public:
    //constructor
    Number() { num = 0; };

    //member function to get input
    void inputNumber(void)
    {
        cout << "Enter an integer number: ";
        cin >> num;
    }
    //member function to display number
    void displayNumber()
    {
        cout << "Num: " << num << endl;
    }
};

//Main function
int main()
{
    //declaring object to the class number
    Number N;
    //input and display number using norn object
    N.inputNumber();
    N.displayNumber();

    //declaring pointer to the object
    Number* ptrN;
    ptrN = new Number; //creating & assigning memory

    //printing default value
    cout << "Default value... " << endl;
    //calling member function with pointer
    ptrN->displayNumber();

    //input values and print
    ptrN->inputNumber();
    ptrN->displayNumber();

    return 0;
}

Output

Enter an integer number: 10
Num: 10
Default value...
Num: 0
Enter an integer number: 20
Num: 20

Explanation

The main three steps needs to be understood those are:

  1. Pointer to object creation: Number *ptrN;
  2. Dynamic memory initialization to the pointer object: ptrN = new Number;
  3. Accessing member function by using "Arrow Operator": ptrN->displayNumber();

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.