In this chapter we shall learn about:
1. Defining member functions inside of the class
2. Defining member functions outside of the class
3. Characteristic of member functions
4. Inline member functions
We know that a class has data members and member functions. In this chapter we shall learn how to declare a member functions and different ways to initialize them.
Member Function Introduction:
Functions defined inside a class are called as “member functions”. These member functions have unrestricted access to private data members inside the class.
The member functions can be defined inside a class, or outside a class.
1. Defining member functions inside of the class
A member function can be defined inside of the class. Then these functions are by default inline member functions.
Point to be noted is, if you want to access member functions after crating an object, then you need to define them as “public” access specifier.
Example of a function defined inside of the class:
class Example
{
public:
void member_function_defined_inside_class()
{
cout<<"Hi from member_function_defined_inside_class"<<endl;
}
};
2. Defining member functions outside of the class
A function can also be defined outside of the class. In such cases, we follow below points:
1. We write the function declaration inside the class.
2. While defining a function outside the class, we use scope resolution operators “::”.
3. The function name is preceded by class name and scope resolution operator.
Syntax:
<function_return_type> <class_name>:: <function_name>
Example:
class Example
{
public:
void member_function_defined_outside_class();
};
void Example::member_function_defined_outside_class()
{
cout<<"Hi from member_function_defined_outside_class"<<endl;
}
How to call member functions?
We can call member functions by creating a class object.
Example:
/*
* File : member_function_example.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
using namespace std;
class Example
{
public:
void member_function_defined_inside_class()
{
cout<<"Hi from member_function_defined_inside_class"<<endl;
}
void member_function_defined_outside_class();
};
void Example::member_function_defined_outside_class()
{
cout<<"Hi from member_function_defined_outside_class"<<endl;
}
int main()
{
Example e1;
e1.member_function_defined_inside_class();
e1.member_function_defined_outside_class();
}
Output:
Hi from member_function_defined_inside_class
Hi from member_function_defined_outside_class
Characteristic of member functions
1. A member function defined as a private, then it cannot be accessed outside a class, with an object.
2. Non static member functions have to be accessed with an object.
3. There can be member function of same name in different classes.
4. One-member function can access another member function without creating an object.
4. Inline member function
An inline member function will replace the function call with actual function definition. Hence preventing the function call and function return overhead.
It is recommended that functions with minimal functionality should be used as inline. As the function definition is replaced in the place of function call at compile time the program size tends to increase. As it is compile time, we cannot do it for library functions.
By default, all the member functions defined inside a class are inline member function.
Syntax:
inline return_type class_name::function_name(arguments)
{
//statements;
}
Example of inline member function defined outside a class.
/*
* File : inline_member_function_example.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
using namespace std;
class Example
{
public:
void inline_member_function_defined_outside_class();
};
inline void Example::inline_member_function_defined_outside_class()
{
cout<<"Hi from inline_member_function_defined_outside_class"<<endl;
}
int main()
{
Example e1;
e1.inline_member_function_defined_outside_class();
}
Output:
Hi from inline_member_function_defined_outside_class