C++ Programming - Constructors and Destructors - Discussion

Discussion :: 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; 
}

[A]. The program will print the output 25.
[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 C

Explanation:

No answer description available for this question.

Vineet said: (Aug 8, 2012)  
Destructor should be define.

Sindhu said: (Aug 9, 2012)  
Here x is declared as private so we cannot access x in main function.

Sai said: (Aug 15, 2012)  
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().

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

Jyoti said: (Oct 17, 2012)  
The destructor is not provided the definition.

Bittoo said: (Mar 5, 2013)  
If destructor cannot be declared, it must be defined within class.

Prabuferoz said: (Sep 27, 2013)  
void show() should be declared before destructor.

Bob Marley said: (Aug 11, 2014)  
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 :).

Rexi said: (Sep 17, 2014)  
Destructor has to destroy the object. Thus we have to define destructor inside the main function.

Swetha said: (Jan 2, 2015)  
The program get compiled and it provides only run time error.

Sid said: (Mar 8, 2015)  
Yes its run time error. So answer is D.

Guriya said: (Apr 29, 2015)  
Yes, runtime error: undefined symbol Bix::~Bix().

Jayanthi Akula said: (Nov 26, 2015)  
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.

Vigneshwar said: (Apr 29, 2016)  
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.

Niketa Agarwal said: (Jul 22, 2016)  
It is because of undefined reference to destructor.

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

Neha said: (May 21, 2018)  
program throw runtime error =Bix::~Bix(void)" not compile time.

Please help me to solve this.

Priya Modak said: (Dec 21, 2018)  
It is because of undefined reference to the destructor.

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

Raghavendra said: (May 3, 2019)  
It's because of cout it should be std::cout and include file should be iostream not iostream.h.

Jenny said: (Nov 6, 2019)  
First of all, it should be <iostream>
And next would be ~Bix(); as ~Bix(){};

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.