C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 43)
43.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
static double gDouble; 
static float  gFloat; 
static double gChar; 
static double gSum = 0; 
class BaseOne
{
    public:
    void Display(double x = 0.0, float y = 0.0, char z = 'A')
    {
        gDouble = x;
        gFloat  = y;
        gChar   = int(z);
        gSum    = gDouble + gFloat + gChar;
        cout << gSum; 
    }
};
class BaseTwo
{
    public: 
    void Display(int x = 1, float y = 0.0, char z = 'A')
    {
        gDouble = x;
        gFloat  = y;
        gChar   = int(z); 
        gSum    = gDouble + gFloat + gChar;
        cout << gSum;
    }
};
class Derived : public BaseOne, BaseTwo
{
    void Show()
    {
        cout << gSum;
    } 
}; 
int main()
{
    Derived objDev;
    objDev.BaseTwo::Display(10, 20, 'Z');
    return 0; 
}
The program will print the output 0.
The program will print the output 120.
The program will report run-time error.
The program will report compile-time error.
The program will print the output garbage value.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
9 comments Page 1 of 1.

Uma said:   1 decade ago
Because BaseTwo uses private inheritance

Shivesh said:   1 decade ago
static double gSum = 0;
static varable cannot be changed.

Akhilesh kumar said:   1 decade ago
By default inheritance is PRIVATE, so Derived class using private inheritance for BaseTwo, hence Display() method is private method for Derived class is not accessible outside Derived class.

Compile-time ERROR !

ARJUN said:   8 years ago
How will it be private inside the class all variables are declared in public? How are you saying private? Please explain.
(1)

Nana Danso said:   8 years ago
Please, I think you should correct the sample programs in the questions cause without the "using namespace std;" keyword the program won't be able to exexute.

Neha said:   7 years ago
Using namespace std; used in vb2012 not in vb6, So program is right.

Samirhou said:   7 years ago
Yes, the using namespace std is forgotten here.

Here: class Derived: public BaseOne, BaseTwo we can see that the inherence BaseOne is public (I think it doesn't need any explanation) but the BaseTwo inherence is not. Actually, the default inherence is private, so the Display function of the BaseTwo in the derived class becomes a private method. In the main function when we use the Display method, we are trying to access to a private method which is not allowed.

If we add "private" just before BaseTwo in the declaration of the Derived class (ass Derived: public BaseOne, private BaseTwo) the program will work normally

Priyanka said:   5 years ago
I think static members are only used in static member function that's why there is a compile time error.

R Y said:   3 years ago
Please see carefully that BaseTwo is private.

Post your comments here:

Your comments will be displayed after verification.