C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - Programs (Q.No. 3)
3.
Which of the following statement is correct about the program given below?
#include<iostream.h> 
class Bix
{
      int x; 
    public:
      Bix();
     ~Bix();
      void Show() const;
};
Bix::Bix()
{
    x = 25;
}
void Bix::Show() const
{
    cout<< x;
}
int main()
{
    Bix objB;
    objB.Show();
    return 0; 
}
The program will print the output 25.
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:
19 comments Page 1 of 2.

Sai said:   1 decade ago
Kindly note that it's not due to inaccessibility of x. it is very well accessible in Bix::<<anything>> . The destructor is only declared, but not defined anywhere. Hence the compiler throws " undefined reference to Bix::~Bix()" when it's called after the return statement of main().

Jayanthi Akula said:   10 years ago
When we are calling any constant function. The object of that function should be constant. Here in program "objB" is the object of "Bix" which is having a normal object.

So a normal object can not call its constant function. Try the program by declaring the object as "const" like const Bix objB.

Bob marley said:   1 decade ago
In this program we should also define the destructor, like as,

~Bix()
{ }
It should be like that...we can't not left destructor without defining it :).

Priyank said:   1 decade ago
The destructor is only declared, but not defined anywhere
i.e, it is given like this "Bix::~Bix()" and left without definition.

Vigneshwar said:   9 years ago
Const function does not allow the object members to be modified. In this program, we are not modifying so it will print the output 25.

Priya modak said:   7 years ago
It is because of undefined reference to the destructor.

write ~Bix(); as ~Bix(){};
Then it will work.

Neha said:   7 years ago
program throw runtime error =Bix::~Bix(void)" not compile time.

Please help me to solve this.

Niketa Agarwal said:   9 years ago
It is because of undefined reference to destructor.

write ~Bix(); as ~Bix(){};
Then it will work.
(1)

Rexi said:   1 decade ago
Destructor has to destroy the object. Thus we have to define destructor inside the main function.

Raghavendra said:   6 years ago
It's because of cout it should be std::cout and include file should be iostream not iostream.h.


Post your comments here:

Your comments will be displayed after verification.