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.pptxgehlotkrish03
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;
}
};
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: