Pointers
- What is the output of this program?
#include <iostream>
using namespace std;
class Example
{
public:
void FunctionA()
{
cout << "Function A" << endl;
}
int num;
};
void (Example :: *ptr1)() = &Example :: FunctionA;
int Example :: *ptr2 = &Example :: num;
int main()
{
Example object;
Example *ptrObject = new Example;
(object.*ptr1)();
(ptrObject ->* ptr1)();
object.*ptr2 = 3;
ptrObject ->* ptr2 = 4;
cout << object.*ptr2 << endl
<< ptrObject ->* ptr2 << endl;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
In this program, As we are passing the value twice to the method
in the class, It is printing the Function A twice and then it is printing the given value.
- What is the output of this program?
#include <iostream>
using namespace std;
class N
{
public:
int num1;
void fun(int num2)
{
cout<< num2 << endl;
}
};
int main()
{
int N :: *ptr1 = &N :: num1;
void (N :: * ptr2) (int) = &N :: fun;
N object;
object.*ptr1 = 30;
cout << object.*ptr1 << endl;
(object.*ptr2) (15);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this program, We are assigning 30 and printing it in the
main function and then for value 15, We are passing the value to class and printing it.
- Which operator is used in pointer to member function?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
The pointer to member operators .* and ->* are used to bind a pointer to a member of a specific class object.
- What should be used to point to a static class member?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Normal pointer
- Which is referred by pointers to member?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
We cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.