Friends

Q.

What is the output of this program?

1.
    #include 
2.
    using namespace std;
3.
    class base
4.
    {
5.
        int val1, val2;
6.
        public:
7.
        int get()
8.
	{
9.
            val1 = 100;
10.
            val2 = 300;
11.
	}
12.
        friend float mean(base ob);
13.
    };
14.
    float mean(base ob)
15.
    {
16.
        return float(ob.val1 + ob.val2) / 2;
17.
    }
18.
    int main()
19.
    {
20.
        base obj;
21.
        obj.get();
22.
        cout << mean(obj);
23.
        return 0;
24.
    }
A. 200
B. 150
C. 100
D. 300

Answer : Option A

Explanation :

In this program, We are finding the mean value by declaring the function mean as a friend of class base.
Output:
$ g++ friend3.cpp
$ a.out
200