What is the output of this program?
#include <iostream>
using namespace std;
class sample;
class sample1
{
int width, height;
public:
int area ()
{
return (width * height);}
void convert (sample a);
};
class sample
{
private:
int side;
public:
void set_side (int a)
{
side = a;
}
friend class sample1;
};
void sample1::convert (sample a)
{
width = a.side;
height = a.side;
}
int main ()
{
sample sqr;
sample1 rect;
sqr.set_side(6);
rect.convert(sqr);
cout << rect.area();
return 0;
}
a) 24
b) 35
c) 16
d) 36
Submitted by: Administratord) 36
Explanation:
In this program, we are using the friend for the class and calculating the area of the square.
Output:
$ g++ friend2.cpp
$ a.out
36
Submitted by:
Explanation:
In this program, we are using the friend for the class and calculating the area of the square.
Output:
$ g++ friend2.cpp
$ a.out
36
Submitted by:
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. |