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;
}
Discussion:
5 comments Page 1 of 1.
Piya said:
1 decade ago
Can someone tell me why is this constructor accepting the default values, why aren't the values overridden ?
Prabuferoz said:
1 decade ago
Dervied class is dervied from public of base class so it takes base class value.
Kehki said:
1 decade ago
ObjBase.show() calls the base constructor with default values.
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! :) :).
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! :) :).
Arjun said:
8 years ago
Good explanation. Thank you all.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers