C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - Programs (Q.No. 15)
15.
What will be the output of the following program?
#include<iostream.h> 
class BixBase
{
    public:
    int x, y;
    BixBase(int xx = 0, int yy = 5)
    {
        x = ++xx; 
        y = --yy;
    }
    void Display()
    {
        cout<< --y;
    } 
    ~BixBase(){} 
};
class BixDerived : public BixBase
{
    public:
    void Increment()
    {
        y++;
    }
    void Display()
    {
        cout<< --y;
    } 
}; 
int main()
{
    BixDerived objBix;
    objBix.Increment();
    objBix.Display();
    return 0; 
}
3
4
5
Garbage-value
The program will report compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
4 comments Page 1 of 1.

Karthik mass said:   8 years ago
Nice explanation.

Ivneet said:   1 decade ago
x= 1, y=4, initially.
Incrementing y and then store back in y , thus y=5.

And if we simply print y in this case , it will be 5,
But while we print as -- is there it first print the decremented value, and then later on store it in y, that is, it will print 4.

Vikas Khandel said:   1 decade ago
Its a perfect example of implicit call. The derived class object so created calls the base class constructor initializing the values of x and why and then the increment () function of the derived class increment the value of why and finally the display () function displays it.

NEHA GUPTA said:   1 decade ago
Please explain the answer?

Post your comments here:

Your comments will be displayed after verification.