Member functions in C++

05 min read

Member Function: A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. If the member function is defined inside the class definition it can be defined directly, but if it is defined outside the class, then we have to use the scope resolution :: operator along with class name along with function name.

1) Member functions defined within the class

Syntax

class className
{
returnType MemberFunction(arguments)
{                                
//function body
}
. . . .
};

2) Member functions defined outside the class

syntax

class className
{
returnType MemberFunction(arguments);
. . . .
};

returnType className :: function_Name(arg1,arg2,.....argN)
{
function body
}

 

Types of Member Functions

  • Simple functions
  • Static functions
  • Const functions
  • Inline functions
  • Friend functions

 

1) Simple functions: These are the basic member function, which doesn’t have any special keyword like static etc as a prefix.

return_type functionName(parameter_list)
{
function body;
}

 

2) Static functions:  Static is a keyword which can be used with data members as well as the member functions. A function is made static by using the static keyword with the 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 operator. 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
}

 

3) Const functions:  A function becomes const when const keyword is used in function’s declaration. The idea of const functions is not to allow them to modify the object on which they are called.

 

 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;
}

 

4) Inline functions: When the program executes the function call instruction the CPU stores the memory address of the instruction following the function call, copies the arguments of the function on the stack and finally transfers control to the specified function. The CPU then executes the function code, stores the function return value in a predefined memory location/register and returns control to the calling function.

C++ provides inline functions to reduce the function call overhead. An inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it is small.

inline return-type function-name(parameters)
{
    // function code

 

5) Friend function: If a function is defined as a friend function then, the private and protected data of a class can be accessed using the function.The compiler knows a given function is a friend function by the use of the keyword friend. For accessing the data, the declaration of a friend function should be made inside the body of the class starting with keyword friend.

 

Characteristics of member functions

1) A program can have several classes and they can have member functions with the same name, Ambiguity is resolved using the scope resolution operator (::)

2) Private members of a class can be accessed by all the members of the class, whereas non-member functions are not allowed to access.

3) Member functions of the same class can access all other members of their own class without the use of dot operator.

4) Member functions defined as public act as an interface between the service provider and the service seeker.

5) Member functions can take default arguments as well.

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error