Member Functions in Classes

Member functions are the functions, that has declaration inside the class definition and works on the data members of the class. The definition of member functions can be inside or outside the definition of class.

If the member function is defined inside the class definition it can be defined directly.
If the member function is defined outside the class, then to use the scope resolution (::) operator along with class name and function name.

Example :

class Cube
{
 public:
 int side;
 int getVolume;    // Declaring function getVolume with no argument and return type int.
};

 

Types of Member Functions

We already know what member functions are and what can be done with them. Now lets study some of the special member functions present in the class. The different types of Member functions are given below ,

  1. Simple member functions
  2. Static member functions
  3. Constant member functions
  4. Inline member functions
  5. Friend functions

Simple Member functions

This is the basic member function, which don’t have any special keyword like static etc as prefix. All the general member functions are termed as simple and basic member functions.

return_type functionName(parameter_list)
{
function body;
}

Static Member functions

Static is something that holds its own 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. Let us discuss its usage with member functions.

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 (.) pointer. 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.

It doesn’t have any “this” keyword which is the reason it cannot access ordinary members. We will study about “this” keyword later.

Const Member functions (constant)

We will study Const keyword in detail later, but as an introduction, 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 {}

Inline functions

All the member functions defined inside the class definition are by default declared as Inline. We will study Inline Functions in details in the next topic.

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. Here , ” MyFriend ” is a class name

Example :

class MyFriend
{
 int i;
 public:
 friend void fun(); // Global function as friend
};

void fun()
{
 MyFriend mf;
 mf.i=10;  // Access to private data member
 cout << mf.i;
}

int main()
{
fun(); //Can be called directly
}

 

Hence, friend functions can access private data members by creating object of that class. Similarly we can also make function of other class as frienda and also we can  make an entire class as friend class.

 

class Other
{
 void fun();
};

class MyFriend
{
 private:
 int i;
 public:
 void getdata();  // Member function of class MyFriend
 friend void Other::fun();   // making function of class Other as friend here
 friend class Other;  // making the complete class as friend
};

 

When we make a class as a friend, all its member functions are by default become friend functions.

By Friend Functions we can understand why C++ is not a pure object oriented language.Since it violates the concept of Encapsulation process.

Inline Functions in C++

All the member functions defined inside the class definition are in default declared as Inline function. Let us have some background knowledge about these functions.

You must remember Pre-processors from C language.. Preprocessors were not used in C++ because they had some drawbacks to perform.