Sunday, January 29, 2012

Puzzle code constructor and destructor

Constructor and destructor
Q1)output of the program
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                            int b;
                 public :test()
                            {
                                           a=b=100;
                            };
                             void putdata()
                              {
                                          cout<<a<<endl;
                                           cout<<b;
                              };


};

main()
{
 test t;
 t.putdata();
 getch();
}
Ans:-100
          100
Discussion:-Here Default constructor is called when the object of test class is created.


Q2)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                            int b;
                 public :test()
                            {
                                    a=b=100;
                             };
                             test(int n)
                             {
                                      a=b=n;
                              }
                             void putdata()
                              {
                                      cout<<a<<endl;
                                       cout<<b;
                               };


};

main()
{
 test t(10);    //Parameterized constructor call
 t.putdata();
 getch();
}
Ans:-10 10
Discussion:-Here parameterized constructor is call when the object of test class is created.


Q3)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 public :test(int n)
                                {
                                   a=b=n;
                                }
                                void putdata()
                                {
                                  cout<<a<<endl;
                                  cout<<b;
                                };


};

main()
{
 test t;
 t.putdata();
 getch();
}
Ans:-Compile error.
Discussion:-If we write parameterized or copy constructor then it is mandatory to write default constructor.


Q4)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 private:test()
                                {
                                                 a=b=0;
                                }
                 public:  void putdata()
                                {
                                 cout<<a<<endl;
                                 cout<<b;
                                };


};

main()
{
 test t;
 t.putdata();
 getch();
}
Ans:-Compile error.
Discussion:-Here default constructor is declared as private member function.


 Q5)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                                                int b;
                 public  :test()    //Default constructor.
                                                                {
                                                                 a=b=0;
                                                                }
                                                                test(int n)   //Paramiterised constructor.
                                                                {
                                                                 a=b=n;
                                                                }
                                                                test(test &p)   //Copy constructor.
                                                                {
                                                                  a=p.a;
                                                                  b=p.b;
                                                                }

                                                                void putdata()
                                                                {
                                                                 cout<<a<<endl;
                                                                 cout<<b;
                                                                };


};

main()
{
 test t(100),t1(t);
 t1.putdata();
 getch();
}
Ans:-100
          100
Discussion:-Here copy constructor is called for the object   t1  with the value of object  t;

 
Q6) Output of the program

#include "iostream.h"
#include "conio.h"
class test
{
                 private: int a;
                                int b;
                 public :test()          //Default constructor.
                                {
                                  a=b=0;
                                }
                                test(int n=5)   //Paramiterised constructor.
                                {
                                 a=b=n;
                                }

                                void putdata()
                                {
                                  cout<<a<<endl;
                                  cout<<b;
                                };


};

main()
{
 test t;
 t.putdata();
 getch();
}



Q7)output of the program
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 public :int test()          //Default constructor.
                                {
                                 a=b=0;

                                }
                                void putdata()
                                {
                                 cout<<a<<endl;
                                 cout<<b;
                                };


};

main()
{
 test t;
 t.putdata();
 getch();
}
Ans:-Compile time error.
Discussion:-Constructor cannot return any value.


Q8)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 public :test()
                                {
                                 a=b=0;
                                }
                                ~test()
                                {
                                 cout<<"Destructor run";
                                }
                                void putdata()
                                {
                                 cout<<a<<endl;
                                 cout<<b;
                                };
};

main()
{
 test t1,t2,t3;
 {

 }
getch();
}
Ans:-Destructor run Destructor run Destructor run
Discussion:-When the scope of object will goes out then Destructor function will call.

  Q9)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 public :test()
                                {
                                 a=b=0;
                                }
                                void putdata()
                                {
                                 cout<<a<<endl;
                                 cout<<b;
                                };
};

main()
{
 test t1;
 test();
 t1.putdata();

 getch();
}
Ans:-0 0
Discussion:-Here no need to call the constructor function. But there is no error if constructor is call.

 
Q10)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 public :inline test()
                                {
                                 a=b=0;
                                }
                                void putdata()
                                {
                                 cout<<a<<endl;
                                 cout<<b;
                                };
};

main()
{
 test t1;
 test();

 t1.putdata();
 getch();
}
Ans:-0 0
Discussion:-Here test constructor function is defined as inline function.If we declare a function inline then function call overhead will be avoid.

 Q11)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 public : test(int m,int n=5)
                                {
                                 a=m;
                                 b=n;
                                }
                                void putdata()
                                {
                                 cout<<a<<endl;
                                 cout<<b;
                                };
};

main()
{
 test t(100);
 t.putdata();
 getch();
}
Ans:-100   5
Discussion:-Here one argument of test constructor function is default argument. And other argument is passing during the declaration of constructor.

 Q12) Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
                 private:int a;
                                int b;
                 public :test()
                                {
                                 a=b=100;
                                }
                                void modify() const
                                {
                                 ++a;
                                 ++b;
                                };
};

main()
{
 const test t;
 t.modify();
 getch();
}
Ans:-Compile time error.
Discussion:-Here the object t is declared as constant and the modify() function is also declared as constant function ,so it value of t object  should  not change.



No comments:

Post a Comment