Last Updated on April 17, 2023 by Prepbytes
A member function in C++ is a function that is part of a class. It is used to manipulate the data members of the class. Member functions are also known as methods. Member functions are declared inside the class definition and can be defined either inside or outside the class definition. A function declared as a member of the class is called a member function. Generally, member functions are declared as public because they have to be called outside of the class. Member functions are very useful to achieve OOPs concept encapsulation which is used to access private data members of the class using public member functions. In this article, we will learn about member function in-depth, ways to define member function, and types of member function
Member Function
A function is a Member Function if it can be declared as a member of the class (Function declaration or definition within the class). Member Functions can be declared within the class as public, private, or protected functions. Member Functions may be used to read, manipulate, or display all types of data members (private, public, and protected). Member Function is declared as any other variable in C++.
Example of Member Function in C++
Let’s understand what a member function is in C++ with the help of an example?
# includeusing namespace std; class Employee{ int employee_id; string employee_name; string employee_tech; double employee_salary; public: string getDetails(int employee_id); }
In the above example, we can say the getDetails function is a Member Function in C++. Here only a declaration of the Member function is given. We can provide function definitions also.
How to define Member Functions in C++?
There are mainly two ways to define Member Function in C++.
- Definition within the Class
- Definition outside the Class
Defining Member Function within the Class
We can give a definition of the function within the class to define Member Function. This method of defining Member Function is generally used when a definition of function is short. Member functions in C++ has all the access to data members and other member functions of the same class.
Let’s understand how to define Member Function in C++ within the class with the help of an example.
# include <iostream> using namespace std class Employee{ int employee_id; string employee_name; string employee_tech; double employee_salary; public: void setId(int id){ // definition of the function employee_id = id; } void setName(string name){ employee_name = name; } void setTech(string tech){ employee_tech = tech; } void setSalary(double salary){ employee_salary = salary; } int getId(){ return employee_id; } string getName(){ return empoyee_name; } string getTech(){ return employee_tech; } double getSalary(){ return employee_salary; } }
In the above example we have defined the function setId (definition = employee_id=id ). We have defined other functions like setName, setTech, setSalary, getName, getId, etc. We can create an object of class Employee and we can use above defined functions to set values of the private variable (ex:- id, name, tech, salary). We can also use the defined function to get the values of the private variables. The problem with this method is when we write long definitions it’s become more complex to read the code.
Defining Member Function outside the Class
In this method, we can declare a function within the class and we define the function outside the class to achieve the Member Function in C++. While using this method we need to ensure that the function is declared within the class. The problem with this method is when we have more than one class having the same function name so it will be confusing which function’s definition it is.
Below is a small example given to understand the problem?
class Circle{
int size;
public:
void printSize(int s);
}
class Square{
int size;
public:
void printSize(int s);
}
We can see that there is the same function [ printSize( int s ) ] in both classes ( Circle, Square ). To avoid this problem we can use the scope resolution operator (::). The syntax of using the scope resolution operator to achieve Member Function in C++ is given below.
return_type class_name :: member_function_name (arguments){
// definition of the function
}
Let’s see how we can define the member function of both classes ( Circle, Square ) using the scope resolution operator and the above syntax.
void Circle :: printSize(int s){
cout<< s <<endl;
}
void Square :: printSize(int s){
count<< s <<endl;
}
Now lets understand this method of defining Member Function in C++ using an example. We will use the same example as method 1 to understand the difference between the two methods.
# include <iostream> using namespace std; class Employee{ int employee_id; string employee_name; string employee_tech; double employee_salary; public: // only declaration of the functions void setId(int id); void setName(string name); void setTech(string tech); void setSalary(double salary); int getId(); string getName(); string getTech(); double getSalary(); } // definition of the functions outside the class void Employee :: setId(int id){ employee_id=id; } void Employee :: setName(int name){ employee_name=name; } void Employee :: setTech(int tech){ employee_tech=tech; } void Employee :: setSalary(int salary){ employee_salary=salary; } int Employee :: getId(){ return employee_id; } string Employee :: getName(){ return employee_name; } string Employee :: getTech(){ return employee_tech; } double Employee :: getSalary(){ return employee_salary; } }
In the above example, we can see that we have only declared the member functions [ex:- getId(), setId(), etc. ] in the class itself and we define the function outside the class Employee using the scope resolution operator. This method of defining the Member Function in C++ is generally used when the definition of the function is large. This method is very efficient and readable.
Types of Member Function in C++
There are five types of Member Function in C++.
- Simple Function
- Const Function
- Static Function
- Inline Function
- Friend Function
Simple Function
Simple Functions are declared without using any specific keyword ( ex:- static, const ). Simple Functions do not have any specific behavior.
#includeusing namespace std; class Shape{ public: // Simple Member Function int square(int length){ return length*length; } }
Const Function
Constant Functions are functions that are not able to modify data members of the class. We can declare Const Function using the const keyword at the end of the arguments parenthesis. The syntax of the Const Function is given below.
Syntax :-
return_type function_name ( arguments ) **const ;**
#include <iostream> using namespace std; class Shape{ int length; public: Shape(int len=0){ length=len; // data member can be assigned only once } // Const Member Function int square(int len) const{ // length=10; if we uncomment this line code will throw an error because we can't modify the data member return length*length; } };
Static Function
Static Functions are created using static keyword before the return type of function declaration. Static Functions can only call static members and static functions inside the static function definition because Static members functions do not have implicit this argument. We can direct call Static Functions without creating an object of the class. Using the scope resolution operator (::) we can call the static function directly.
#include <iostream> using namespace std; class Shape{ Public: // Static Function static int square(int length){ return length*length; } }; int main(){ // call Static Function without creating an object of class std::cout << Shape::square(10) << std::endl; return 0; }
Inline Function
Inline Functions are created using the inline keyword before the return type of the function declaration. Inline Functions are used to reduce overhead function calling. Inline Function may increase efficiency of the code if it has a small definition.
#include <iostream> using namespace std; inline int square(int length){ return length*length; } int main(){ std::cout << square(10) << std::endl; return 0; }
Friend Function
Friends Functions are functions that have their definition outside (non-member function) of the class but they can still access private and protected members of the class. To access private and protected members outside the class definition of the Friend Function should be inside the class. Friend Functions are declared inside the class using a friend keyword before the return type of the function.
#include <iostream> using namespace std; class Shape{ int length; // private member of the class public: // friend function declaration friend int square(int len); }; // definition of friend function int square(int len){ Shape s; s.length=len; // access private variable return s.length*s.length; } int main(){ std::cout << square(10) << std::endl; return 0; }
FAQs of Member Function in C++
1. What is a non member function in C++?
Non-member functions are the opposite of member functions, non-member functions are declared outside the class.
Example:-
#include <iostream>
using namespace std;
class Shape{
public:
// Member Function
int square(int len);
};
// Non-member Function
int rectangle(int length,int breadth){
return length*breadth;
}
2. What is the member variable C++?
Member variables are declared within the class only and member variables are accessible by the function of that same class (Member Function).
Example:-
class Shape{
int length; // member variable
public:
// Member Function
int square(int len);
};
3. What is the difference between function and member function?
A normal function is always declared outside the class and a member function is always declared within the class (it can be defined outside the class).
Example:-
#include <iostream>
using namespace std;
class Shape{
int length;
public:
// Member Function
int square(int len);
};
// Normal Function
int rectangle(int length,int breadth){
return length*breadth;
}
4. What are the characteristics of member functions?
Member Functions with the same name can be used in multiple classes.
The private member or private function of the class can be accessed by the Member Function in C++.
We can pass the default argument in Member Function as well [ex: int square(int length=10) here we pass default length as 10 ].
A Member Function can access any other member of their class without using the dot (.) operator.
5. Can we overload member functions in C++?
Yes, we can overload member function in C++. Below is the given example of member function overload.
Example:-
#include <iostream>
using namespace std;
class Shape{
int length;
public:
// Member Functions with different return type or arguments (overload)
int square(int length){
return length*length;
}
float square(float length){
return length*length;
}
};
int main(){
Shape s;
float length1=10.1;
int length2=10;
std::cout << s.square(length1) << std::endl;
std::cout << s.square(length2) << std::endl;
return 0;
}