Example 1:
We include the “iostream” header file to make it easy to utilize the defined functions. Then, we have the “namespace std”. Underneath this, we generate the class named “MyNewClass” and utilize the “public” constructor.
In “public”, we create the member function named “myFunc” and declare “int num” as the parameter of “myFunc()”. Below this, we use the “cout” and invoke the main() method below this in which we create the member function pointer. We declare the “MyFuncPtr” pointer to the member function type, specifying the “MyNewClass” class and the parameter type (int).
After this, we create the class object named “Class_obj” and then call the member function with the aid of the “*MyFuncPtr” pointer by placing the class object with this. We assign “10” as the parameter to render this when we call the member function pointer.
Code 1:
using namespace std;
class MyNewClass {
public:
void myFunc(int num) {
cout << "The value is " << num << endl;
}
};
int main() {
void (MyNewClass::*MyFuncPtr)(int) = &MyNewClass::myFunc;
MyNewClass Class_obj;
(Class_obj.*MyFuncPtr)(10);
return 0;
}
Output:
This illustrates the working of member function pointers. The member function pointer can be used to launch the member functions dynamically according to current conditions.
Example 2:
To use the functions that are defined in the “iostream” header file simple, we include “iostream” here. The “namespace std” is placed next. Underneath it, we create the “Test” class and then use the “public” constructor. We define the ” myTestingFunc ” member function in “public” and set “int t_value” as the parameter for ” myTestingFunc()” in this instance. The “cout” function is used below and the main() method is called. Then, we create the member function pointer.
We specify the “Test” class and the “*MyTestFuncPtr” member function pointer here. We assign “&Test::myTestingFunc” to the member function pointer which is declared as “void (Test::*MyTestFuncPtr)(int)”.
Next, we generate the “t_obj” class object and use this to call the member function by putting the class object and using the “*MyTestFuncPtr” pointer. For this to be presented when we call the member function pointer, we assign “932” as the parameter.
Code 2:
using namespace std;
class Test {
public:
void myTestingFunc(int t_value) {
cout << "The testing value is " << t_value << endl;
}
};
int main() {
void (Test::*MyTestFuncPtr)(int) = &Test::myTestingFunc;
Test t_obj;
(t_obj.*MyTestFuncPtr)(932);
return 0;
}
Output:
The outcome of the given code is rendered. We can see that we called the “member function pointer” with the class object as shown here.
Example 3:
The class that we generate in this code is “MyNewCar” where we utilize the “public” constructor and create the member function in it which is “startCarEngine()”. In this function, we add “cout” which renders when we call this function in our code. Then, we create another member function which is “stopCarEngine()” and utilize “cout” again in this member function.
After this, we invoke the main() function and then declare the member function pointer which is “MyNewCar::*carEngineFunc()”. Below this, we create the “MyNewCar” class object with the name, “myCar_obj”. Then, we assign the “startCarEngine” function to the “carEngineFunc” pointer. Underneath this, we call this function with the aid of the “carEngineFunc” pointer by placing the object name with it.
Next, we reassign the “stopCarEngine” function to the “carEngineFunc” pointer. Below that, we call this function by passing the object name together with the “carEngineFunc” reference.
Code 3:
using namespace std;
class MyNewCar {
public:
void startCarEngine() {
cout << "The Engine of the car starts" << endl;
}
void stopCarEngine() {
cout << "The Engine of the car stops" << endl;
}
};
int main() {
void (MyNewCar::*carEngineFunc)();
MyNewCar myCar_obj;
carEngineFunc = &MyNewCar::startCarEngine;
(myCar_obj.*carEngineFunc)();
carEngineFunc = &MyNewCar::stopCarEngine;
(myCar_obj.*carEngineFunc)();
return 0;
}
Output:
Here, the working of the “member function pointer” is displayed. We can see that we created the member function pointer and displayed the result here.
Example 4:
After including the header file and the “std namespace”, we declare the “MyNewStudent” class here. The ” studentPass()” member function is one of the member functions that we build for the “MyStudentClass” class that we generate here. We also add “cout” to this function which will render when we call it in our code.
Next, we write the “studentFail()” member function where we use “cout” once more. The main() function is then called and the “(MyNewStudent::*studentResultFunc)()” member function pointer is declared. Below that, we generate the ” myStd_obj ” object which belongs to the “MyNewStudent” class.
Next, we assign the “studentPass” function to the “studentResultFunc” pointer. Below that, we call this function by passing the object name together with the “studentResultFunc ” reference. The “studentFail” function is reassigned to the ” studentResultFunc ” pointer. Below that, we invoke this method by giving the “carEngineFunc” reference and the object name.
Now, both functions are called here and the statements that we included in these functions are rendered.
Code 4:
using namespace std;
class MyNewStudent {
public:
void studentPass() {
cout << "The student pass" << endl;
}
void studentFail() {
cout << "The student fail" << endl;
}
};
int main() {
void (MyNewStudent::*studentResultFunc)();
MyNewStudent myStd_obj;
studentResultFunc = &MyNewStudent::studentPass;
(myStd_obj.*studentResultFunc)();
studentResultFunc = &MyNewStudent::studentFail;
(myStd_obj.*studentResultFunc)();
return 0;
}
Output:
We created the member functions in our code and then the member function pointer. After this, we called the member function and displayed the outcome here.
Example 5:
The “SampleClass” is created in this instance. Then, the member function pointer is placed here which is “(SampleClass::*MyFunc)()”. Underneath this, we create the function pointer which is “(*MyFuncPtr)()”. Below it, we declare the “name” of the “string” variable as well as the “MyFunc f” member function pointer.
After this, we have the “public” constructor where we define this member function variable. Underneath this, we create the member functions named “myFunc_1()” and “myFunc_1()” and also add “cout” in each member function which will display when we call this member function.
Then, we call this member function pointer with the aid of “(this->*f)()”. Then, we place the functions again. Here, we change the “cout” statements which we previously added. Then, “main()” is now invoked and the member function pointer is defined as “MyFunc f = &SampleClass::myFunc_2”.
Then, the function pointer is also defined as “MyFuncPtr fp = myFunc_1”. After this, we type “(a.*f)()” to utilize the member function pointer. The “b.func” is placed to utilize the member function. Then, we place “fp()” to call the function pointer.
Code 5:
using namespace std;
class SampleClass;
typedef void (SampleClass::*MyFunc)();
typedef void (*MyFuncPtr)();
class SampleClass{
string name;
MyFunc f;
public:
SampleClass(const char* name)
:name(name),
f(&SampleClass::myFunc_1)
{}
void myFunc_1(){ cout<<name <<"We called fuction 1 here" << endl;}
void myFunc_2(){ cout<<name <<"We called function 2 here" << endl;}
void func(){
(this->*f)();
}
};
void myFunc_1(){ cout<<"The first function" << endl;}
void myFunc_2(){ cout<<"The second function" << endl;}
int main()
{
MyFunc f = &SampleClass::myFunc_2;
MyFuncPtr fp = myFunc_1;
SampleClass a("first - "), b("second - ");
(a.*f)();
b.func();
fp();
}
Output:
The outcome of the code is now rendered here which renders the result accordingly as we called the functions in our code.
Conclusion
We explored that “member function pointers” in C++ ease the dynamic binding, encapsulation of behavior, and flexible handling of function invocation within the context of OOP. We learned that the usage of “member function pointers” can significantly improve the modularity and flexibility of C++ codebases, providing a powerful tool for addressing numerous design and runtime challenges.