Sail E0 Webinar
Question


What is the output of this program?


1.
#include
2.
using namespace std;
3.
struct X;
4.
struct Y
5.
{
6.
void f(X*);
7.
};
8.
struct X
9.
{
10.
private:
11.
int i;
12.
public:
13.
void initialize();
14.
friend void g(X* , int);
15.
friend void Y :: f(X*);
16.
friend struct Z;
17.
friend void h();
18.
};
19.
void X :: initialize()
20.
{
21.
i = 0;
22.
}
23.
void g(X* x, int i)
24.
{
25.
x -> i = i;
26.
}
27.
void Y :: f(X * x)
28.
{
29.
x -> i = 47;
30.
cout i;
31.
}
32.
struct Z
33.
{
34.
private:
35.
int j;
36.
public:
37.
void initialize();
38.
void g(X* x);
39.
};
40.
void Z::initialize()
41.
{
42.
j = 99;
43.
}
44.
void Z::g(X* x)
45.
{
46.
x -> i += j;
47.
}
48.
void h()
49.
{
50.
X x;
51.
x.i = 100;
52.
cout
Options:
A .  99
B .  47
C .  Data accessed
D .  None of the mentioned
Answer: Option C

In this program, We are using the access specifiers to friend function to manipulate the values.
Output:
$ g++ acc3.cpp
$ a.out
Data accessed



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers