C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : 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; 
}
The program will print the output 5.
The program will print the output Garbage-value.
The program will report compile time error.
The program will report runtime error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
4 comments Page 1 of 1.

Manjit Singh said:   1 decade ago
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:   9 years ago
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:   7 years ago
I thought it will not compile because "cout" will give an error.

We are not using std, right?

Fouad Ayman said:   6 years ago
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:

Your comments will be displayed after verification.