What is Member Function in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

A Member function is a function that is declared as a member of a class. It is declared inside the class in any of the visibility modes i.e.i.e. public, private, and protected, and it can access all the data members of the class. If the member function is defined inside the class definition, it can be defined directly inside the class; otherwise, we need to use the scope resolution operator (::) to declare the member function in C++ outside the class(we will see about it later in the article).

The main aim of using the member function is to provide modularity to a program, which is generally used to improve code reusability and to make code maintainable.

Syntax of Member function is as follows :

Definition

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable.

Member function in C++ operates on any object of the class of which it is a member and has access to all the members of a class for that object. Therefore, we need to declare an object of a class to access member functions of that particular class.

Member Function inside the Class

In this section, we will see how member functions are defined inside the class. Before beginning, let's define the class.

The above class represents a student class that comprises essential details about the student. In the following code, we are using some member functions to get the details and to set the value of the details.

Output

The Problem with Function Definition inside

The major issue that occurs when member functions are defined within the class is that it makes the function inline and implicit expansion takes place, which ultimately reduces the performance of the code because an inline function results in larger code size so it may cause thrashing in memory. Also, more page fault occurs; hence, defining the function outside the class is recommended. Let's see how

Member Function outside the Class

To define a member function outside the class, we will use the underlying syntax :

The only thing we need to take care of is that a declaration of memberFunctionName should exist in the className. Note that there is no difference in calling the member function. Even if it is defined outside the class, we need to define an object and use it to access the function. Please go through the below example to have a better understanding :

Output

Accessing the Private Member Function of A Class

The class members declared as private can only be accessed by functions inside the class, not by any class object.

So to access private member functions, we will be using the public member function of the same class to call private member functions public member functions have access to the private member functions.

The code below shows how public member functions can be wrapped up over private member functions to make them accessible from the class objects.

Output

Conclusion

  • Member functions in C++ are the functions used inside the class of C++.
  • Member functions can be defined inside or outside the class (though it is recommended to define them outside).
  • Using member functions can help improve the modularity of code and increase code reusability.