C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 37)
37.
What will be the output of the following program?
#include<iostream.h> 
class BaseCounter
{
    protected:
    long int count;

    public:
    void CountIt(int x, int y = 10, int z = 20)
    {
        count = 0;
        cout<< x << " " << y << " " << z << endl;
    } 
    BaseCounter()
    {
        count = 0;
    }
    BaseCounter(int x)
    {
        count = x ;
    } 
}; 
class DerivedCounter: public BaseCounter
{
    public:
    DerivedCounter()
    { }
    DerivedCounter(int x): BaseCounter(x) 
    { }
};
int main()
{
    DerivedCounter objDC(30); 
    objDC.CountIt(40, 50); 
    return 0; 
}
30 10 20
Garbage 10 20
40 50 20
20 40 50
40 Garbage Garbage
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
2 comments Page 1 of 1.

Prity Sinha said:   4 years ago
objDC.CountIt(40, 50);
It will intialize value of x and y in void CountIt(int x, int y = 10, int z = 20),
As rule says default value and override and default value order is right to left.
So x=40,y=50, z is consider as default which is 20.

MIDHUNGOPAL said:   8 years ago
Please explain it.

Post your comments here:

Your comments will be displayed after verification.