Member Functions of a Class in C++

What is member function of a class?

Member function of a class is a function that must be declared inside the class.

The member functions can be defined as

1. Inside Member Function
2. Private Member Function
3. Outside Member Function

1. Inside Member Function

Inside member function class can be declared in public or private section.

Example : Program to demonstrate member function

Accessing private member of a class using member function

#include <iostream>
using namespace std;
class employee
{
     private:               //Private section starts
          int empid;
          float esalary;
     public:             //Public section starts
          void display()     //Member function
          {
               empid = 001;
               esalary = 10000;
               cout<<"Employee Id is : "<<empid<<endl;
               cout<<"Employee Salary is : "<<esalary<<endl;
          }
};
int main()
{
      employee e;      //Object Declaration
      e.display();        //Calling to member function
      return 0;
}


Output:
Employee Id is : 1
Employee Salary is : 10000

  • In the above program, the member function display() is defined inside the class in public section.
  • In int main() function, object 'e' is declared. An object has permission to access the public members of the class.
  • The object 'e' invokes the public member function display().
  • The public member function can access the private members of the same class.
  • The display() function initializes the private member variables and displays the contents on the console.

2. Private Member Function

  • It is possible to access private data member of a class using public member function.
  • Private member function is invoked by public member function of the same class.

Example : Program demonstrating public member function

Accessing private member function using public member function

#include <iostream>
using namespace std;
class employee
{
     private:               //Private section starts
         int empid;
         float esalary;
         void addvalues()      //Private member function
         {
               empid = 001;
               esalary = 10000;
         }
     public:             //Public section starts
         void display()     //Public member function
         {
               addvalues();       //Calling to private member function
               cout<<"Employee Id is : "<<empid<<endl;
               cout<<"Employee Salary is : "<<esalary<<endl;
         }
};
int main()
{
     employee e;         //Object Declaration
     e.display();           //Calling to public member function
     //e.addvalues(); cannot be accessible
     return 0;
}


Output:
Employee Id is : 1
Employee Salary is : 10000

  • In the above example, the public member function display() invokes the private member function addvalues().
  • The private section of a class employee contains one member function addvalues().
  • The display() function is defined in public section. In int main() function, 'e' is an object of class employee.
  • The object 'e' cannot access the private member function. To execute the private member function, the private function must be invoked using public member function.

3. Outside Member Function

  • If a function is small and inside the class, it is considered as an Inline function.
  • If a function is large, it should be defined outside the class.
  • When the member function of a class is defined outside the class they are called as Externally defined member function.
  • Scope Resolution (::) operator is used to define the member function of the class outside the scope and prototype declaration of function must be declared inside the class.
Syntax:
Return_type Class_name :: Function_name(argument_list)
{
     //Statements;
}

Example : Define member function class outside the scope/class

#include <iostream>
using namespace std;
class employee
{
     private:                  //Private section starts
          int empid;
          float esalary;
     public:                 //Public section starts
          void display(void);     //Prototype Declaration
};  //End of class
void employee :: display()     //Function Definition Outside the Class
{
     empid = 001;
     esalary = 10000;
     cout<<"Employee Id is : "<<empid<<endl;
     cout<<"Employee Salary is : "<<esalary<<endl;
}
int main()
{
     employee e;      //Object Declaration
     e.display();       //Calling to public member function
     return 0;
}


Output:
Employee Id is : 1
Employee Salary is : 10000

  • In the above program, the prototype declaration of display() function is declared inside the class terminated by class definition.
  • The function body of display() function is defined inside the class.
  • The function declaration of display() function is, void employee :: display() where, void is a return type and employee is a class name. The scope resolution (::) operator separates the class name and function name, followed by the body of function which is defined.