Types of Member Functions in C++

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 :: before the identifier of the function.

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 ::

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

Friend function

Friend function is a function that is not member function of a class, but it has access to private and protected members of a class. Friend function is declared and implemented outside of class as a simple function. But the class has to grant “friend” privileges by declaring this function with friend keyword inside of the class declaration. Here is an example of creating a friend function for Person class:

bool checkSalary(Person p)
{
	if (p.salary > 40000) //salary is protected data member
		return true;
	else
		return false;
}

This is a function with a global scope. Inside the class we have to add its declaration with friend keyword:

friend bool checkSalary(Person p);//friend function

The access specifiers do not influence friend functions. That’s why they can be declared anywhere in the class. Now you can call checkSalary from main as a simple function:

bool result = checkSalary(person1);
Translate »