SlideShare a Scribd company logo
Pointer to Member Function
• In C++, a pointer to a member function allows
you to call a member function on an object
through a pointer.
• This can be particularly useful for callbacks or
when you need to store member functions in
data structures (e.g.. virtual table in C++,
which is a collections of pointer to virtual
functions).
1. Declaration of Pointer to Member Function
class Sample
{
public:
void funct1();
void funct2(int, int);
int funct3(int);
};
void (Sample :: *fp1)( );
void (Sample :: *fp2)(int, int);
int (Sample :: *fp3)(int);
Declaration of Member Functions Declaration of Function Pointers
that can point to the member
functions of the class Sample
The syntax is:
Return_type (ClassName :: *pointer_name) (Argument list);
Return_type and argument list must be same as that of the member function (to
which the function pointer is pointing to). For example, consider the following
class Sample having three member functions. The declaration of pointer to these
member functions are given on the R.H.S.
can point to
can point to
can point to
2. Definition of Pointer to Member Function
fp1 = &Sample :: funct1; // The address of func1 has been assigned to fp1
fp2 = &Sample :: funct2; // The address of func2 has been assigned to fp2
fp3 = &Sample :: funct3; // The address of func3 has been assigned to fp3
The above two steps, i.e. declaration and definition can be
converted into a single statement as below:
void (Sample :: *fp1)( ) = &Sample :: funct1;
void (Sample :: *fp2)(int, int) = &Sample :: funct2;
int (Sample :: *fp3)(int) = &Sample :: funct3;
3. Calling a Pointer to Function
• (object.*function_pointer)(argument list) ;
• (pointer_to_object -> *function_pointer) (argument list);
Eg.
Sample object; // Object of Sample class
(object.*fp1)( );
(object.*fp2)(int, int);
(object.*fp3)(int);
Sample *ptr = new Sample; // Pointer to object of Sample class
(ptr->*fp1)( );
(ptr->*fp2)(int, int);
(ptr->*fp3)(int);
Point to remember
void (Sample :: *fp1)( );
• Note that the above declaration can point to
any member function whose return type is
void and has no parameter.
Example 1:
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imaginary;
public:
Complex()
{
real = imaginary = 0;
}
Complex(int r, int i)
{
real = r;
imaginary = i;
}
void enterComplexNumber()
{
cout << "n Enter the real part: ";
cin >> real;
cout << "n Enter the imaginary part:
";
cin >> imaginary;
Cont.
void displayComplexNumber()
{
cout << "nThe complex number is: ";
if(imaginary >=0)
cout << real << "+" << imaginary << "i";
else
cout << real << imaginary << "i";
}
};
int main()
{
void (Complex :: *fp1) ( ) = &Complex :: enterComplexNumber ;
Complex c1 ;
(c1.*fp1)() ;
void (Complex :: *fp2) ( ) = &Complex :: displayComplexNumber ;
(c1.*fp2)() ;
return 0;
}
Output:
Enter the real part: 3
Enter the imaginary part: 4
The complex number is: 3+4i
A little updation in the previous main method
Now, look at the signatures of the following functions.
void enterComplexNumber();
void displayComplexNumber();
Both the functions have the same return type and are without any parameter.
Therefore, the following pointer to function notation can be used to point to
above two methods.
void (Complex :: *fp1) ( );
int main()
{
void (Complex :: *fp1) ( ) = &Complex :: enterComplexNumber ;
Complex c1 ;
(c1.*fp1)() ;
fp1 = &Complex :: displayComplexNumber ;
(c1.*fp1)() ;
return 0;
}
Calling pointer to member function using pointer to the object
of Complex class
int main()
{
void (Complex :: *fp1) ( ) = &Complex :: enterComplexNumber ;
Complex *c1 = new Complex ;
(c1->*fp1)() ;
fp1 = &Complex :: displayComplexNumber ;
(c1->*fp1)() ;
return 0;
}
Example 2: Passing of objects
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imaginary;
public:
Complex()
{
real = imaginary = 0;
}
void enterComplexNumber()
{
cout << "n Enter the real part: ";
cin >> real;
cout << "n Enter the imaginary part: ";
cin >> imaginary;
}
Cont.
void displayComplexNumber()
{
cout << "nThe complex number is: ";
if(imaginary >=0)
cout << real << "+" << imaginary << "i";
else
cout << real << imaginary << "i";
}
Complex addComplexNumber(Complex c)
{
Complex temp;
temp.real = real + c.real;
temp.imaginary = imaginary + c.imaginary;
return temp;
}
};
Cont.
int main()
{
void (Complex :: *fp1) () = &Complex :: enterComplexNumber ;
void (Complex :: *fp2) () = &Complex :: displayComplexNumber ;
Complex c1 ;
(c1.*fp1)() ;
(c1.*fp2)() ;
Complex c2 ;
(c2.*fp1) ();
(c2.*fp2) ();
Complex (Complex :: *fp3 ) (Complex) = &Complex :: addComplexNumber;
Complex c3;
c3 = (c1.*fp3) (c2);
(c3.*fp2) () ;
return 0;
}
Enter the real part: 2
Enter the imaginary part: 3
The complex number is: 2+3i
Enter the real part: 4
Enter the imaginary part: 5
The complex number is: 4+5i
The complex number is: 6+8i
Output:
Since, the member functions void enterComplexNumber() and void
displayComplexNumber() are having the same return type (void) and both are
without any parameter, therefore the same pointer can be used to point to
both the functions. Consequently, the main() method has been updated as
below.
int main()
{
void (Complex :: *fp1) () ;
// fp1 is pointing to the function enterComplexNumber
fp1 = &Complex :: enterComplexNumber ;
Complex c1 ;
cout<< "n Entering the first complex number ";
(c1.*fp1)() ;
// Now, fp1 is pointing to the function displayComplexNumber
fp1 = &Complex :: displayComplexNumber ;
cout << " The first ";
(c1.*fp1)() ;
// Cont. on the next page
Cont.
Complex c2 ;
cout<< "n Entering the second complex number ";
// fp1 is pointing to the function enterComplexNumber
fp1 = &Complex :: enterComplexNumber ;
(c2.*fp1) ();
// fp1 is pointing to the function displayComplexNumber
fp1 = &Complex :: displayComplexNumber ;
cout << " The second ";
(c2.*fp1) ();
// fp2 is pointing to the function addComplexNumber
Complex (Complex :: *fp2 ) (Complex) = &Complex :: addComplexNumber;
Complex c3;
c3 = (c1.*fp2) (c2);
cout << "n After addition, ";
(c3.*fp1) () ;
return 0;
}
Output
Entering the first complex number
Enter the real part: 2
Enter the imaginary part: 3
The first complex number is: 2+3i
Entering the second complex number
Enter the real part: 4
Enter the imaginary part: 5
The second complex number is: 4+5i
After addition, complex number is: 6+8i
Passing of member functions as parameters in a member function of the same class
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imaginary;
public:
Complex()
{ real = imaginary = 0; }
void enterComplexNumber()
{
cout << "n Enter the real part: "; cin >> real;
cout << " Enter the imaginary part: "; cin >> imaginary;
}
void displayComplexNumber()
{
cout << "n The complex number is: ";
if(imaginary >=0)
cout << real << "+" << imaginary << "i";
else
cout << real << imaginary << "i";
}
Cont. In the following member function, two pointers to member functions
are being passed as parameters
void addComplexNumber( void (Complex :: *fp1) (), void (Complex :: *fp2) () )
{
Complex c1 ;
(c1.*fp1)() ;
(c1.*fp2)() ;
Complex c2 ;
(c2.*fp1)() ;
(c2.*fp2)() ;
Complex temp;
temp.real = c1.real + c2.real;
temp.imaginary = c1.imaginary + c2.imaginary;
cout << "n After Addition: ";
(temp.*fp2)() ;
}
};
int main()
{
Complex c3;
c3.addComplexNumber(&Complex :: enterComplexNumber, &Complex :: displayComplexNumber);
return 0;
}
Enter the real part: 3
Enter the imaginary part: 4
The complex number is: 3+4i
Enter the real part: 6
Enter the imaginary part: 7
The complex number is: 6+7i
After addition
The complex number is: 9+11i
Output:

More Related Content

Similar to Pointer to Member Function.pptx pointer in c++ (20)

PPT
w10 (1).ppt
amal68766
 
PPTX
Operator overload rr
Dhivya Shanmugam
 
PPT
Unit vi(dsc++)
Durga Devi
 
PPT
Mca 2nd sem u-2 classes & objects
Rai University
 
PPT
Bca 2nd sem u-2 classes & objects
Rai University
 
PPTX
Object oriented programming slides for presentation
abdullahkhann3534
 
PPTX
C++ Object Oriented Programming Lecture Slides for Students
MuhammadAli224595
 
PPTX
ExamRevision_FinalSession_C++ notes.pptx
nglory326
 
DOCX
Bc0037
hayerpa
 
PPTX
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
PPTX
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
PDF
C++ prgms 3rd unit
Ananda Kumar HN
 
PDF
Implementation of oop concept in c++
Swarup Kumar Boro
 
PPTX
class and objects
Payel Guria
 
PPTX
Class and object
prabhat kumar
 
PDF
The Function Pointer Tutorials
Nont Banditwong
 
PDF
Ch 4
AMIT JAIN
 
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
 
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
PDF
polymorphism in c++ with Full Explanation.
UdayGumre
 
w10 (1).ppt
amal68766
 
Operator overload rr
Dhivya Shanmugam
 
Unit vi(dsc++)
Durga Devi
 
Mca 2nd sem u-2 classes & objects
Rai University
 
Bca 2nd sem u-2 classes & objects
Rai University
 
Object oriented programming slides for presentation
abdullahkhann3534
 
C++ Object Oriented Programming Lecture Slides for Students
MuhammadAli224595
 
ExamRevision_FinalSession_C++ notes.pptx
nglory326
 
Bc0037
hayerpa
 
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
C++ prgms 3rd unit
Ananda Kumar HN
 
Implementation of oop concept in c++
Swarup Kumar Boro
 
class and objects
Payel Guria
 
Class and object
prabhat kumar
 
The Function Pointer Tutorials
Nont Banditwong
 
Ch 4
AMIT JAIN
 
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
 
Object Oriented Programming (OOP) using C++ - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
polymorphism in c++ with Full Explanation.
UdayGumre
 

Recently uploaded (20)

PPTX
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
PPTX
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
PPTX
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
PPTX
THE JEHOVAH’S WITNESSES’ ENCRYPTED SATANIC CULT
Claude LaCombe
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
10CLA Term 3 Week 4 Study Techniques.pptx
mansk2
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PDF
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Translation_ Definition, Scope & Historical Development.pptx
DhatriParmar
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Unlock the Power of Cursor AI: MuleSoft Integrations
Veera Pallapu
 
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
Rules and Regulations of Madhya Pradesh Library Part-I
SantoshKumarKori2
 
THE JEHOVAH’S WITNESSES’ ENCRYPTED SATANIC CULT
Claude LaCombe
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
10CLA Term 3 Week 4 Study Techniques.pptx
mansk2
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Stepwise procedure (Manually Submitted & Un Attended) Medical Devices Cases
MUHAMMAD SOHAIL
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Ad

Pointer to Member Function.pptx pointer in c++

  • 1. Pointer to Member Function • In C++, a pointer to a member function allows you to call a member function on an object through a pointer. • This can be particularly useful for callbacks or when you need to store member functions in data structures (e.g.. virtual table in C++, which is a collections of pointer to virtual functions).
  • 2. 1. Declaration of Pointer to Member Function class Sample { public: void funct1(); void funct2(int, int); int funct3(int); }; void (Sample :: *fp1)( ); void (Sample :: *fp2)(int, int); int (Sample :: *fp3)(int); Declaration of Member Functions Declaration of Function Pointers that can point to the member functions of the class Sample The syntax is: Return_type (ClassName :: *pointer_name) (Argument list); Return_type and argument list must be same as that of the member function (to which the function pointer is pointing to). For example, consider the following class Sample having three member functions. The declaration of pointer to these member functions are given on the R.H.S. can point to can point to can point to
  • 3. 2. Definition of Pointer to Member Function fp1 = &Sample :: funct1; // The address of func1 has been assigned to fp1 fp2 = &Sample :: funct2; // The address of func2 has been assigned to fp2 fp3 = &Sample :: funct3; // The address of func3 has been assigned to fp3 The above two steps, i.e. declaration and definition can be converted into a single statement as below: void (Sample :: *fp1)( ) = &Sample :: funct1; void (Sample :: *fp2)(int, int) = &Sample :: funct2; int (Sample :: *fp3)(int) = &Sample :: funct3;
  • 4. 3. Calling a Pointer to Function • (object.*function_pointer)(argument list) ; • (pointer_to_object -> *function_pointer) (argument list); Eg. Sample object; // Object of Sample class (object.*fp1)( ); (object.*fp2)(int, int); (object.*fp3)(int); Sample *ptr = new Sample; // Pointer to object of Sample class (ptr->*fp1)( ); (ptr->*fp2)(int, int); (ptr->*fp3)(int);
  • 5. Point to remember void (Sample :: *fp1)( ); • Note that the above declaration can point to any member function whose return type is void and has no parameter.
  • 6. Example 1: #include <iostream> using namespace std; class Complex { private: int real, imaginary; public: Complex() { real = imaginary = 0; } Complex(int r, int i) { real = r; imaginary = i; } void enterComplexNumber() { cout << "n Enter the real part: "; cin >> real; cout << "n Enter the imaginary part: "; cin >> imaginary;
  • 7. Cont. void displayComplexNumber() { cout << "nThe complex number is: "; if(imaginary >=0) cout << real << "+" << imaginary << "i"; else cout << real << imaginary << "i"; } }; int main() { void (Complex :: *fp1) ( ) = &Complex :: enterComplexNumber ; Complex c1 ; (c1.*fp1)() ; void (Complex :: *fp2) ( ) = &Complex :: displayComplexNumber ; (c1.*fp2)() ; return 0; } Output: Enter the real part: 3 Enter the imaginary part: 4 The complex number is: 3+4i
  • 8. A little updation in the previous main method Now, look at the signatures of the following functions. void enterComplexNumber(); void displayComplexNumber(); Both the functions have the same return type and are without any parameter. Therefore, the following pointer to function notation can be used to point to above two methods. void (Complex :: *fp1) ( ); int main() { void (Complex :: *fp1) ( ) = &Complex :: enterComplexNumber ; Complex c1 ; (c1.*fp1)() ; fp1 = &Complex :: displayComplexNumber ; (c1.*fp1)() ; return 0; }
  • 9. Calling pointer to member function using pointer to the object of Complex class int main() { void (Complex :: *fp1) ( ) = &Complex :: enterComplexNumber ; Complex *c1 = new Complex ; (c1->*fp1)() ; fp1 = &Complex :: displayComplexNumber ; (c1->*fp1)() ; return 0; }
  • 10. Example 2: Passing of objects #include <iostream> using namespace std; class Complex { private: int real, imaginary; public: Complex() { real = imaginary = 0; } void enterComplexNumber() { cout << "n Enter the real part: "; cin >> real; cout << "n Enter the imaginary part: "; cin >> imaginary; }
  • 11. Cont. void displayComplexNumber() { cout << "nThe complex number is: "; if(imaginary >=0) cout << real << "+" << imaginary << "i"; else cout << real << imaginary << "i"; } Complex addComplexNumber(Complex c) { Complex temp; temp.real = real + c.real; temp.imaginary = imaginary + c.imaginary; return temp; } };
  • 12. Cont. int main() { void (Complex :: *fp1) () = &Complex :: enterComplexNumber ; void (Complex :: *fp2) () = &Complex :: displayComplexNumber ; Complex c1 ; (c1.*fp1)() ; (c1.*fp2)() ; Complex c2 ; (c2.*fp1) (); (c2.*fp2) (); Complex (Complex :: *fp3 ) (Complex) = &Complex :: addComplexNumber; Complex c3; c3 = (c1.*fp3) (c2); (c3.*fp2) () ; return 0; }
  • 13. Enter the real part: 2 Enter the imaginary part: 3 The complex number is: 2+3i Enter the real part: 4 Enter the imaginary part: 5 The complex number is: 4+5i The complex number is: 6+8i Output:
  • 14. Since, the member functions void enterComplexNumber() and void displayComplexNumber() are having the same return type (void) and both are without any parameter, therefore the same pointer can be used to point to both the functions. Consequently, the main() method has been updated as below. int main() { void (Complex :: *fp1) () ; // fp1 is pointing to the function enterComplexNumber fp1 = &Complex :: enterComplexNumber ; Complex c1 ; cout<< "n Entering the first complex number "; (c1.*fp1)() ; // Now, fp1 is pointing to the function displayComplexNumber fp1 = &Complex :: displayComplexNumber ; cout << " The first "; (c1.*fp1)() ; // Cont. on the next page
  • 15. Cont. Complex c2 ; cout<< "n Entering the second complex number "; // fp1 is pointing to the function enterComplexNumber fp1 = &Complex :: enterComplexNumber ; (c2.*fp1) (); // fp1 is pointing to the function displayComplexNumber fp1 = &Complex :: displayComplexNumber ; cout << " The second "; (c2.*fp1) (); // fp2 is pointing to the function addComplexNumber Complex (Complex :: *fp2 ) (Complex) = &Complex :: addComplexNumber; Complex c3; c3 = (c1.*fp2) (c2); cout << "n After addition, "; (c3.*fp1) () ; return 0; }
  • 16. Output Entering the first complex number Enter the real part: 2 Enter the imaginary part: 3 The first complex number is: 2+3i Entering the second complex number Enter the real part: 4 Enter the imaginary part: 5 The second complex number is: 4+5i After addition, complex number is: 6+8i
  • 17. Passing of member functions as parameters in a member function of the same class #include <iostream> using namespace std; class Complex { private: int real, imaginary; public: Complex() { real = imaginary = 0; } void enterComplexNumber() { cout << "n Enter the real part: "; cin >> real; cout << " Enter the imaginary part: "; cin >> imaginary; } void displayComplexNumber() { cout << "n The complex number is: "; if(imaginary >=0) cout << real << "+" << imaginary << "i"; else cout << real << imaginary << "i"; }
  • 18. Cont. In the following member function, two pointers to member functions are being passed as parameters void addComplexNumber( void (Complex :: *fp1) (), void (Complex :: *fp2) () ) { Complex c1 ; (c1.*fp1)() ; (c1.*fp2)() ; Complex c2 ; (c2.*fp1)() ; (c2.*fp2)() ; Complex temp; temp.real = c1.real + c2.real; temp.imaginary = c1.imaginary + c2.imaginary; cout << "n After Addition: "; (temp.*fp2)() ; } }; int main() { Complex c3; c3.addComplexNumber(&Complex :: enterComplexNumber, &Complex :: displayComplexNumber); return 0; }
  • 19. Enter the real part: 3 Enter the imaginary part: 4 The complex number is: 3+4i Enter the real part: 6 Enter the imaginary part: 7 The complex number is: 6+7i After addition The complex number is: 9+11i Output: