Sail E0 Webinar
Question


What is the output of this program?


1.
#include
2.
using namespace std;
3.
class sample
4.
{
5.
int width, height;
6.
public:
7.
void set_values (int, int);
8.
int area () {return (width * height);}
9.
friend sample duplicate (sample);
10.
};
11.
void sample::set_values (int a, int b)
12.
{
13.
width = a;
14.
height = b;
15.
}
16.
sample duplicate (sample rectparam)
17.
{
18.
sample rectres;
19.
rectres.width = rectparam.width * 2;
20.
rectres.height = rectparam.height * 2;
21.
return (rectres);
22.
}
23.
int main ()
24.
{
25.
sample rect, rectb;
26.
rect.set_values (2, 3);
27.
rectb = duplicate (rect);
28.
cout
Options:
A .  20
B .  16
C .  24
D .  None of the mentioned
Answer: Option C

In this program, we are using the friend function for duplicate function and calculating the area 

of the rectangle.
Output:
$ g++ friend1.cpp
$ a.out
24



Was this answer helpful ?
Next Question

Submit Solution

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

Latest Videos

Latest Test Papers