Home » C++ Programming » Functions » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class BoxExample
    {
    double W;
    public:
    friend void printW(BoxExample box );
    void setW( double width );
    };
    void BoxExample::setW( double width )
    {
    W = width;
    }
    void printW(BoxExample box )
    {
    box.W = box.W * 3;
    cout << "Width of box : " << box.W << endl;
    }
    int main( )
    {
    BoxExample box;
    box.setW(15.0);
    printW( box );
    return 0;
    }
    1. 15
    2. 3
    3. 45
    4. Compilation Error
    5. None of these
Correct Option: C

We are using the friend function for printwidth and multiplied the width value by 3, So we got the output as 45.



Your comments will be displayed only after manual approval.