Whenever we call a member function inside another member function of one class it is known as Nesting of the member function. Generally, the member function which is called by another member function is kept private so that it cannot be called directly using the dot operator.
Let’s look at the example code below for better understanding:
#include <iostream> using namespace std; class phone_number { private: string num; void check_num(); public: void input_num(); void output_num(); }; void phone_number::input_num() { cout << "Please Enter the Phone number: " ; cin >> num; check_num(); // Nested member function } // The Scope resolution operator (::) is used to see the class whose member function is there. void phone_number::check_num() { if( num.size() !=10) { cout<<"Entered Phone number is wrong"<<"\n"; }else{ int check=0; for(int i=0;i<10;i++){ int tp=num[i]-'0'; if(tp>=0 && tp<=9){ }else{ check++; break; } } if(check!=0){ cout<<"Entered Phone number is wrong"<<"\n"; } } } void phone_number::output_num() { cout << "Your entered number is: " << num; } int main() { phone_number obj; obj.input_num(); obj.output_num(); return 0; }
Output:
Please Enter the Phone number: 9876543210
Your entered number is: 9876543210
In the above code, the nested member function is check_num(). It checks whether the given number is correct or not. Since we don’t want the user to use it directly by writing obj.check_num() so we have kept it private.