What type of function is member function?

Member functions are part of C++ classes. Member functions represent behavior of a class. All member functions can be divided into the following categories:

  1. Simple functions
  2. Const functions
  3. Static functions
  4. Inline functions
  5. Virtual functions

Although, friend functions are not member function, we will discuss the use of friend functions too. Friend functions can access even private members of a class. That’s why they are used to manipulate class objects.

Before starting to describe different types of member functions there is an important note you must know. A good style is to separate the interface of the class from its implementation. The header file (extension .h) has to specify what does a class is. The source file has to define how a class works. For example, the class Person from the previous topics should be separated into 2 files. The first one is the header file which describes data members and contains functions’ declaration:

#include 
#include 
#include 
using namespace std;

class Person
{
	public://access control
		string firstName;//these data members
		string lastName;//can be accessed
		tm dateOfBirth;//from anywhere
	protected:
		string phoneNumber;//these members can be accessed inside this class,
		int salary;// by friend functions/classes and derived classes
	private:
		string address;//these members can be accessed inside the class
		unsigned long int insuranceNumber;//and by friend classes/functions
	public:
		bool setInsuranceNumber(unsigned long int insurance);
		string getAddress();
		long int getInsuranceNumber();
};

And the source file has to implement declared functions:
#include "Person.h"
bool Person::setInsuranceNumber(unsigned long int insurance)
{
	if (insurance > 100000000 && insurance < 999999999)
	{
		insuranceNumber = insurance;//a correct value is set
		return true;//everything is ok
	}
	else
		//do not set incorrect value and return false
		return false;
}
string Person::getAddress()
{
	if (!address.empty())
		return address;
	else
		return "Attention!!! Address is empty";
}
long int Person::getInsuranceNumber()
{
	return insuranceNumber;
}

When you implement a function outside of the class declaration, you have to specify the name of the class with
#include "Person.h"
bool Person::setInsuranceNumber(unsigned long int insurance)
{
	if (insurance > 100000000 && insurance < 999999999)
	{
		insuranceNumber = insurance;//a correct value is set
		return true;//everything is ok
	}
	else
		//do not set incorrect value and return false
		return false;
}
string Person::getAddress()
{
	if (!address.empty())
		return address;
	else
		return "Attention!!! Address is empty";
}
long int Person::getInsuranceNumber()
{
	return insuranceNumber;
}
1 before the identifier of the function.

Table of Contents

Simple function

Simple functions are functions that do not have any specific keyword used in declaration. They do not have any special behavior and manipulate with data members of a class. The syntax used for declaration of a simple member functions is:

ReturnType FunctionName (ParameterList);

We declared some simple functions in class person:

void printPerson()
{
	cout << "First name " << firstName << endl;
	cout << "Last name " << lastName << endl;
	cout << "Year of Birth " << dateOfBirth.tm_year << endl;
}

string getAddress()
{
	return address;
}

long int getInsuranceNumber()
{
	return insuranceNumber;
}

Const function

Const function is used to specify a “read-only” function. This type of function can’t modify any non-static data members or call any other non-const member functions. If you want to declare a constant function you have to add const keyword after parenthesis of parameter list:

ReturnType FunctionName (ParameterList) const;

The const keyword must be used both in declaration and implementation of the member function. For example, we can modify getAddress member function to a const one:

string getAddress() const;

This function is constant and it can’t modify any data member:
string Person::getAddress() const
{
	if (!address.empty())
		return address;
	else
		return "Attention!!! Address is empty";
}

Static function

Static functions have class scope. They can’t modify any non-static data members or call non-static member functions. Static members functions do not have implicit this argument. That’s why they can work only with static members of class. Static member functions can be declared using the following format:

static ReturnType FunctionName (ParameterList);

Static member functions can’t be declared virtual. Here is an example of declaring a static member function:

static void getInfo();

The implementation of this function doesn’t require const keyword:
void Person::getInfo()
{
	cout << "This is Person class" << endl;
}

You can call static function without creating any object of this class. You just have to specify class name followed by
#include "Person.h"
bool Person::setInsuranceNumber(unsigned long int insurance)
{
	if (insurance > 100000000 && insurance < 999999999)
	{
		insuranceNumber = insurance;//a correct value is set
		return true;//everything is ok
	}
	else
		//do not set incorrect value and return false
		return false;
}
string Person::getAddress()
{
	if (!address.empty())
		return address;
	else
		return "Attention!!! Address is empty";
}
long int Person::getInsuranceNumber()
{
	return insuranceNumber;
}
1
Person::getInfo();

Inline function

Inline functions are declared by using inline keyword. The purpose of inline functions is discussed in details in “Inline functions”. All the functions that are implemented inside the class declaration are inline member functions.

Virtual member function

Virtual member function is a member functions that is expected to be overloaded in the derived class. Virtual functions are used in inheritance – they provide correct behavior when you call a function from derived class that is declared virtual in base class by using a pointer to the base class. The examples of use of virtual functions are described in “C++ Inheritance”.

What are the type of member functions?

Member Functions can be declared within the class as public, private, or protected functions. Member Functions may be used to read, manipulate, or display all types of data members (private, public, and protected). Member Function is declared as any other variable in C++.

Why member functions are used?

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.

Are member functions inline?

Member functions of a local class must be defined within their class definition. As a result, member functions of a local class are implicitly inline functions.

Are member functions static?

By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.