There are various types of member function in C++.
1. Simple functions
2. Static functions
3. Const functions
4. Inline functions
5. Friend functions
1. Simple Member functions
These are the basic member function, which don’t have any special keyword like static etc as prefix. All the general member functions, which are of below given form, are termed as simple and basic member functions.
Syntax:
return_type functionName(parameter_list)
{
function body;
}
2. Static Member functions
Static is something that holds its position. Static is a keyword which can be used with data members as well as the member functions. We will discuss this in details later. As of now we will discuss its usage with member functions only.
A function is made static by using static keyword with function name. These functions work for the class as whole rather than for a particular object of a class.
It can be called using the object and the direct member access dot (.) operator. But, its more typical to call a static member function by itself, using class name and scope resolution operator (::)
Example :
class X
{
public:
static void f(){};
};
int main()
{
X::f(); // calling member function directly with class name
}
These functions cannot access ordinary data members and member functions, but only static data members and static member functions.
3. Const Member functions
Const keyword makes variables constant, that means once defined, there values can’t be changed. When used with member function, such member functions can never modify the object or its related data members.
//Basic Syntax of const Member Function
void fun() const {}
4. Inline functions
In C++,we can create short functions that are not actually called rather their code is expanded in line at the point of invocation.
5. Friend functions
Friend functions are actually not class member function. Friend functions are made to give private access to non-class functions. You can declare a global function as friend, or a member function of other class as friend.