What is the output of this program?
#include <iostream>
using namespace std;
class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
void Box::setWidth( double wid )
{
width = wid;
}
void printWidth( Box box )
{
box.width = box.width * 2;
cout << "Width of box : " << box.width << endl;
}
int main( )
{
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
a) 40
b) 5
c) 10
d) 20
Submitted by: Murtazad) 20
Explanation:
We are using the friend function for printwidth and multiplied the width value by 2, So we got the output as 20
Output:
$ g++ friend.cpp
$ a.out
20
Submitted by: Murtaza
Explanation:
We are using the friend function for printwidth and multiplied the width value by 2, So we got the output as 20
Output:
$ g++ friend.cpp
$ a.out
20
Submitted by: Murtaza
Read Online C++ Friend Job Interview Questions And Answers
Top C++ Friend Questions
☺ | List the characteristics of friend functions? |
☺ | Explain the characteristics of friend functions? |
☺ | What is a friend function? |
☺ | What are friend classes? |
☺ | Explain advantages of using friend classes? |
Top C Plus Plus Language Categories
☺ | C++ Pointers & Functions Interview Questions. |
☺ | C++ Operator Overloading Interview Questions. |
☺ | C++ Exception Handling Interview Questions. |
☺ | C++ Virtual Functions Interview Questions. |
☺ | C++ Template Interview Questions. |