Member Functions in C++

Member Functions in C++: Driving programs into a function are one of the major principles of programming. These are known as “Member functions” in  C++. They have their deceleration’s inside the class definitions. And these member functions work on the data member of the class. While defining if the member function is inside the class definition, then it can be defined directly.

Member Functions in C++

In case if it is defined outside of the class, then we should use the special operator known as “scope resolution operator (::)” will be used along with class name and the function name.

Example: Declaring function outside the class



class Sq {
public:
 int a;
 int square();//Declaring function square with no argument and having return type 'int'.
};
int Sq::square()
{
    return a*a;
}

Example: If the function defined inside the class

class Sq {
public:
 int a;
 int square()
 {
   return a*a;//returns square of a number
 }
};

Types of Member Functions in C++

As we already learned about the exact meaning of member functions in C++. And now let us see the different types present in C++. They are as follows:

  • Simple member function
  • Static member function
  • Const function
  • Inline Function
  • Friend Function

Simple Member Functions in C++

The Simple member functions in C++ are known as basic member functions which do not have a special keyword like static known as a prefix.

Syntax:

return_type functionName(parameter_list)
{
function body;
}

Static Member Functions in C++

The static member functions in C++ is nothing but it holds it’s position without changing. So we should use the “static” keyword for this static member function in C++. These functions work for the whole class rather than for a particular object of a class. It can be called using the object and direct member access. operator. But, its more typical to call a static member function by itself, using the class name and scope resolution:: operator.

Syntax: static ReturnType FunctionName (ParameterList);

Example:

#include <iostream> 
#include <string> 
using namespace std;   
void demo() 
{  
//static variable 
    static int count = 0; 
    cout << count << " ";       
//value is updated and will be carried to next function calls 
    count++; 
}  
int main() 
{ 
    for (int i=0; i<5; i++)     
        demo(); 
    return 0; 
} 

Output:

0  1  2  3  4

Const Member Functions in C++

The Const member functions in C++ is used to specify the “read-only” function. I will not modify any non-static data members (or) calls any other non-const member functions. So, if you want to use this member function we should use the “const” keyword. And this keyword must be declared both in the declaration and initialization of the member functions.

Syntax: ReturnType FunctionName (ParameterList) const;

Example:

#include<iostream> 
using namespace std; 
class Test { 
    int value; 
public: 
    Test(int v = 0) {
     value = v;} 
//We get compiler error if we add a line like "value = 100;" in this function. 
   int getValue() 
    const {
    return value;}   
};  
int main() { 
    Test t(20); 
    cout<<t.getValue(); 
    return 0; 
}

Output: 20

Inline Member Functions in C++

The inline member function c++ is defined inside the class definition are default declared as inline. when the this is called the whole code of the inline function gets inserted (or) submitted at the point of the inline function call. It also saves the overhead of a return call from the function.  Mostly the inline function is useful for embedded system because inline can perform less code than the function call preamble and return.

Syntax: inline return-type function-name(parameters)

Example:

#include <iostream> 
using namespace std; 
inline int cube(int s) 
{ 
    return s*s*s; 
} 
int main() 
{ 
    cout<<"The cube of 3 is:"<<cube(3)<<"\n"; 
    return 0; 
}

Output: The cube of 3 is: 27

Friend Function in C++

A friend function in C++ gives access to the private and protected members of the class. It is not the member function of a class. The friend function is declared and implemented outside of the class as a sample function. So, if you want to use this function it should be called by using “friend” keyword.

Note:

  • Friends should be used only for a limited purpose.
  • If too many many functions (or) external classes are declared as friends of a class with protected (or) private data then it decreases the value of encapsulation of separate classes in object-oriented programming.
  • Friendship is not inherited and is not mutual.
  • Finally, the concept of friend is not there in Java.

Example:

#include<iostream> 
class A { 
private: 
    int a; 
public: 
    A() { a=0; } 
    friend class B;//Friend Class 
}; 
  
class B { 
private: 
    int b; 
public: 
    void showA(A& x) { 
//Since B is friend of A, it can access private members of A 
     std::cout << "A::a=" << x.a; 
    } 
}; 
  
int main() { 
   A a; 
   B b; 
   b.showA(a); 
   return 0; 
}

Output:

A::a=0