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 

Bài Viết Liên Quan

Chủ Đề