C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - Programs (Q.No. 15)
15.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
class BixBase
{
    int x, y; 
    public:
    BixBase(int xx = 10, int yy = 10)
    {
        x = xx;
        y = yy;
    }
    void Show()
    {
        cout<< x * y << endl;
    }
};
class BixDerived : public BixBase
{
    private:
        BixBase objBase; 
    public:
    BixDerived(int xx, int yy) : BixBase(xx, yy)
    {
        objBase.Show();
    }
};
int main()
{
    BixDerived objDev(10, 20);
    return 0; 
}
The program will print the output 100.
The program will print the output 200.
The program will print the output Garbage-value.
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
5 comments Page 1 of 1.

Arjun said:   8 years ago
Good explanation. Thank you all.

Akkshay said:   1 decade ago
This is example of single inheritance.

Class BixDerived is derived from class BixBase.

i.e Class BixDerived acquires all properties of class BixBase.

Hence, whenever object of BixDerived class is created, it allocates space for its variables as well as for Base class variables.

So, the values passed to Base class constructor are assigned to the variables of Base class inherited in derived class.

And then in derived class, the new object of Base class is created as a member.

i.e. BixBase objBase;

While creating this object, no values are being passed so it initializes to default parameters.

And, as Show() ; is called with this new object, it prints 10*10 = 100.

If you call Show() ; as,

BixBase :: Show() ;

Which calls the Show() ; function with current object, it will print 10*20 = 200.

Hope this helps!

Enjoy! :) :).

Kehki said:   1 decade ago
ObjBase.show() calls the base constructor with default values.

Prabuferoz said:   1 decade ago
Dervied class is dervied from public of base class so it takes base class value.

Piya said:   1 decade ago
Can someone tell me why is this constructor accepting the default values, why aren't the values overridden ?

Post your comments here:

Your comments will be displayed after verification.