C++ Programming - Constructors and Destructors - Discussion

Discussion :: Constructors and Destructors - Programs (Q.No.4)

4. 

Which of the following statement is correct about the program given below?

#include<iostream.h> 
class Bix
{
      int x; 
    public:
      Bix();
      void Show() const;
      ~Bix(){}
};
Bix::Bix()
{
    x = 5;
}
void Bix::Show() const
{
    cout<< x;
}
int main()
{
    Bix objB;
    objB.Show();
    return 0; 
}

[A]. The program will print the output 5.
[B]. The program will print the output Garbage-value.
[C]. The program will report compile time error.
[D]. The program will report runtime error.

Answer: Option A

Explanation:

No answer description available for this question.

Manjit Singh said: (Jun 21, 2015)  
Why will the output be 5?

Isn't there a rule that a constant function can access only constant data? The value for the variable is not declared to be constant. Then why is there no compile time error.

Also the similar question before with x = 25 had the same code overall, except the point at the deconstructor definition. Here there is just the curly braces.

Niketa Agarwal said: (Jul 22, 2016)  
No, it is because previously ~Bix(); which gives compilation error undefined reference to ~Bix()
but here we are writing ~Bix(){}; so it is printing 5.

Saiyan said: (Feb 7, 2019)  
I thought it will not compile because "cout" will give an error.

We are not using std, right?

Fouad Ayman said: (Nov 30, 2019)  
It will not compile and the compiler would raise an error "cout undeclared identifier"
You should either use namespace at the first of the program " using namespace std;" or using scope resolution operator before using cout like that " std::cout<<"pla pla pla"; "

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.