Home » C++ Programming » Modifier Types » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    struct A;
    struct B
    {
    void fun1(A*);
    };
    struct A
    {
    private:
    int k;
    public:
    void initialize();
    friend void fun2(A* , int);
    friend void B :: fun1(A*);
    friend struct C;
    friend void fun3();
    };
    void A :: initialize()
    {
    k = 0;
    }
    void fun2(A* a, int k)
    {
    a -> k = k;
    }
    void B :: fun1(A * a)
    {
    a -> k = 35;
    cout << a->k;
    }
    struct C
    {
    private:
    int L;
    public:
    void initialize();
    void fun2(A* a);
    };
    void C::initialize()
    {
    L = 45;
    }
    void C::fun2(A* a)
    {
    a -> k += L;
    }
    void fun3()
    {
    A a;
    a.k = 150;
    cout << a.k;
    }
    int main()
    {
    A a;
    C c;
    c.fun2(&a);
    cout << "Data accessed";
    }
    1. Data accessed
    2. Compilation Error
    3. Garbage value
    4. Runtime Error
    5. None of these
Correct Option: A

In this program, We are using the access specifiers to friend function to manipulate the values.



Your comments will be displayed only after manual approval.