As we all know, functions play an important role in programming. Driving programs in function is one of the essential principles of programming. In C, they are called functions, but in C++, they are called member functions. This tutorial will teach you about the member function and its concept used in C++.



What are Member Functions in C++?

Member functions are C++ functions that have declarations inside the class definition, and these member functions work on the data member of the class. A member function definition can be written inside or outside the class definition. Suppose the definition of the member function is inside the class definition. In that case, it can define directly, but if it is defined outside the class, then a scope resolution operator (::) is used along with the name of the class and the function name.

Example:

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

int Sq::square()
{
    return a * a;
}

In this case, if you define the member function outside the class definition, you must declare the function inside the class definition and then define it outside using the scope resolution operator.

or

Example:

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

In the above program, the function is defined inside the class, so you do not need to declare it first and can directly define it.

main() Function of C++

The main() function is called when the program starts after initializing the non-local objects with static storage duration. It is the primary entry point of any C++ program executed in a hosted environment. In C++, the main() function is written after the class definition and has several unique properties. These are:

  • The main() function cannot be used anywhere within the program.
    • In particular, it cannot be called recursively.
    • Its address cannot be taken for reuse.
  • The main() function cannot be predefined and cannot be overloaded.
  • The main() function cannot be declared static, inline, or constexpr.
  • The return type of the main() function cannot be deduced, i.e., "auto main() {" it is not allowed in C++.

The main() function will be the same for both function definitions (discussed above). You have to create class objects inside the main() and use that object along with the dot (.) operator member functions will be called.

Syntax:

return_type main()
{
    class_name object - declaration;
    object_name.memberfunction1_name();
    object_name.memberfunction2_name();
    ........
}

Example:

int main()
{
    Sq S1;
    S1.a = 6;
    S1.square();
    cout << " Square of the number is :"<< S1.square();
}

Types of Member Functions in C++

As we are now familiar with what member functions are and how they are declared and defined, how they are used in a C++ program to handle data member and member functions, and how they are called from the main(); it is time to know what are the different types of member functions provided by C++.

Listed below are the types of member functions used within the class.
  • Simple member function
  • Static Member function
  • Const function
  • Inline function
  • Friend function

Simple Member Functions

As discussed earlier, these are simple functions of C++ with or without return type and with or without parameters. The basic structure of a simple member function is:

Syntax:

return_type functionName(parameter_list)
{
    // function body;
}

Static Member Functions

The keyword 'static' is used with such member functions. Static is mainly used to hold its positions. These functions work for the whole class rather than a particular class object.

Example:

class X {
public:
    static void k(){};
};
int main()
{
    G::k(); // calling member function directly with class name
}

The static member functions cannot access ordinary data members and member functions but can only access the static data members and static member functions of a class.

Const Member Function

The keyword const makes a variable constant, meaning its value cannot be changed once defined. The basic syntax of the const member function is:

Example:

void fun() const{}

Inline Function

When a function is declared inline, the compiler places a copy of that specific function's code at each point where the function is called at compile time.

Friend Function

Functions are declared as a friend using the keyword 'friend' to give private access to non-class functions. Programmers can declare a global function as a friend or a member function of another class as a friend.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram