Pointers


  1. 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;
    }











  1. 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.


  1. 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);
    }











  1. 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.



  1. Which operator is used in pointer to member function?











  1. 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.


  1. What should be used to point to a static class member?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Normal pointer



  1. Which is referred by pointers to member?











  1. 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.