CPP

CPP

Friends has 9 Questions

Which rule will not affect the friend function?

private and protected members of a class cannot be accessed from outside
private and protected member can be accessed anywhere
both a & b
None of the mentioned
Which keyword is used to declare the friend function?

firend
friend
classfriend
myfriend
What is the syntax of friend function?

friend class1 Class2;
friend class;
friend class
None of the mentioned
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class Box
  4.     {
  5.         double width;
  6.         public:
  7.         friend void printWidth( Box box );
  8.         void setWidth( double wid );
  9.     };
  10.     void Box::setWidth( double wid )
  11.     {
  12.         width = wid;
  13.     }
  14.     void printWidth( Box box )
  15.     {
  16.         box.width = box.width * 2;
  17.         cout << "Width of box : " << box.width << endl;
  18.     }
  19.     int main( )
  20.     {
  21.         Box box;
  22.         box.setWidth(10.0);
  23.         printWidth( box );
  24.         return 0;
  25.    }

40
5
10
20
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class sample;
  4.     class sample1 
  5.     {
  6.         int width, height;
  7.         public:
  8.         int area ()
  9.         {
  10.             return (width * height);}
  11.             void convert (sample a);
  12.         };
  13.     class sample 
  14.     {
  15.         private:
  16.         int side;
  17.         public:
  18.         void set_side (int a)
  19.         { 
  20.             side = a;
  21.         }
  22.         friend class sample1;
  23.     };
  24.     void sample1::convert (sample a) 
  25.     {
  26.         width = a.side;
  27.         height = a.side;
  28.     }
  29.     int main () 
  30.     {
  31.         sample sqr;
  32.         sample1 rect;
  33.         sqr.set_side(6);
  34.         rect.convert(sqr);
  35.         cout << rect.area();
  36.         return 0;
  37.     }

24
35
16
36
What is the output of this program?
  1.     #include 
  2.     using namespace std;
  3.     class sample
  4.     {
  5.         private:
  6.         int a, b;
  7.         public:
  8.         void test()
  9.         {
  10.             a = 100;
  11.             b = 200;
  12.         }
  13.         friend int compute(sample e1);
  14.     };
  15.     int compute(sample e1)
  16.     {
  17.         return int(e1.a + e1.b) - 5;
  18.     }
  19.     int main()
  20.     {
  21.         sample e;
  22.         e.test();
  23.         cout  << compute(e);
  24.         return 0;
  25.     }

100
200
300
295
Pick out the correct statement.

A friend function may be a member of another class.
A friend function may not be a member of another class.
A friend function may or may not be a member of another class.
None of the mentioned
Where does keyword 'friend' should be placed?

function declaration
function definition
main function
None of the mentioned
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.     }

200
150
100
300