Calling a non-member function inside a class in C++
Last Updated :
23 Aug, 2022
Member Function: It is a function that can be declared as members of a class. It is usually declared inside the class definition and works on data members of the same class. It can have access to private, public, and protected data members of the same class. This function is declared as shown below:
C++
// Given Class
class memberdemo {
private:
{
public:
{
void check();
}
// Member Function
trial::void check();
}
}
Non-member Function: The function which is declared outside the class is known as the non-member function of that class. Below is the difference between the two:
- The member function can appear outside of the class body (for instance, in the implementation file). But, this approach is followed, the member function must be qualified by the name of its class. This is to identify that function is a member of a particular class. But a non-member function always appears outside of a class.
- Another difference between member functions and non-member functions is how they are called (or invoked) in the main routine.
Program 1:
C++
// C++ to illustrate the above concepts
#include <iostream>
using namespace std;
class A {
int a;
public:
// Member function
void memberfn(int x)
{
a = x;
cout << "Member function inside"
<< " class declared\n";
}
// Member function but definition
// outside the class
void memberfn2();
};
// Member function declared with
// scope resolution operator
void A::memberfn2()
{
cout << "Member function but declared "
<< " outside the class\n";
}
// Non-member function
void nonmemberfn()
{
cout << "Non-member function\n";
}
// Driver Code
int main()
{
// Object of class A
A obj;
// Calling Member Function
obj.memberfn(5);
obj.memberfn2();
// Calling Non-Member Function
nonmemberfn();
return 0;
}
Output:Member function inside class declared
Member function but declared outside the class
Non-member function
Explanation: From the above program, there are three types of cases:
- A member function is declared and defined in the class and called using the object of the class.
- A member function is declared in the class but defined outside the class and is called using the object of the class.
- A non-member function that is declared outside the class but called a normal function inside the main function.
Now, the question here arises whether it is possible to call a non-member function inside a member function in a program or not. The answer is YES. Below is the program to illustrate how to call a non-member function inside a member function:
Program 2:
C++
// C++ program to illustrate the
// above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the factorial
// of the number N
int fact(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}
// Class Examples
class example {
int x;
public:
void set(int x) { this->x = x; }
// Calling the non-member function
// inside the function of class
int find() { return fact(x); }
};
// Driver Code
int main()
{
// Object of the class
example obj;
obj.set(5);
// Calling the member function
cout << obj.find();
return 0;
}
Explanation: A non-member function can be called inside a member function but the condition is that the non-member function must be declared before the member function. In the above example, the same approach is followed and it becomes possible to invoke a non-member function thus the answer is the factorial of 5 i.e. 120.
Similar Reads
How to Declare a Static Member Function in a Class in C++? In C++, static functions are functions that are directly associated with a class so we can access the static function directly without creating an object of the class using the scope resolution operator. In this article, we will learn how we can declare a static function in a class in C++. Declare a
1 min read
Function Pointer to Member Function in C++ In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a functio
3 min read
Passing a Function as a Parameter in C++ Passing a function as an argument is useful in dynamically changing the behaviour of the function. This concept has already been used while passing a custom comparator function as an argument in sort() function to sort a sequence of objects as per the need.Function that is passed as argument is call
4 min read
How to Check if a Template Class has the Given Member Function in C++? In C++ programming, we often need to check if a template class contains a specific member function. In this article, we will discuss how to check if a Template Class has the given member function or not. Check for the Member Function in a Template ClassWe can check whether a member function exists i
3 min read
How to Call a Virtual Function From a Derived Class in C++? In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a vir
2 min read
How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use
2 min read