Sunday, January 29, 2012

Puzzle code of class and object of C++


Q1)Output  of   the program
#include "iostream.h"
#include "conio.h"
class test
{
  Private : int a;
            int b;
};
main()
{
 test t;
 t.a=100;
 t.b=200;
 getch();
}
Ans:-Compile error.
Discussion:-Here a and b data member are declared as private ,so  t.a=100,Such type of initialization is not possible.

Q2)Output of the program
#include "iostream.h"
#include "conio.h"
class test
{
  private:int a;
                 int b;

  public:void putdata()
                                  {
                                                 cout<<a;
                                                 cout<<b;
                                  }
                                  void getdata()
                                  {
                                                a=100;
                                                b=100;
                                  }
};
main()
{
 test t;
 t.getdata();
 t.putdata();
 getch();
}
Ans:-100 100
Discussion:-Here a and b data member are private and getdata and putdata ate public member function ,so the public member function handeling private data member.

Q3)Is this code is erroneous?

#include "iostream.h"
#include "conio.h"
class test
{
  private:int a;
                 int b;
  private:void putdata()
                                                 {
                                                 cout<<a;
                                                 cout<<b;
                                                 }
  public:void getdata()
                                  {
                                                a=100;
                                                b=100;
                                  }
};
Ans:-No
Discussion:-Here the visibility scope private is declared twice.And  compilar support it.

Q4)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
  private:int a;
                                                 int b;

  private:void putdata()
                                                 {
                                                 cout<<a;
                                                 cout<<b;
                                                 }
  public:void getdata()
                                  {
                                                a=100;
                                                b=100;
                                  }
};
main()
{
 test t;
 t.getdata();
 t.putdata()

 getch();
}
Ans:-Compile time error.
Discussion:-Here the function member putdata is declared as private member.So it is not possible invoke the putdata function using class object.


Q5)Output of the program

#include "iostream.h"
#include "conio.h"
class test
{
  private:int a;
                                                 int b;
  private:void putdata()
                                                 {
                                                  cout<<a;
                                                  cout<<b;
                                                 }
  public:void getdata()
                                  {
                                                a=100;
                                                b=100;
                                  }
                                  void call_putdata()
                                  {
                                                putdata();
                                  }
};
main()
{
 test t;
 t.getdata();
 t.call_putdata();

 getch();
}
Ans:-100 100
Discussion:-Here putdata() function is declared as private function so,to call putdata function call_putdata function is used.Which is public function within same class.

 Q6)Output of the program.
#include "iostream.h"
#include "conio.h"

int n=100;
main()
{
 int n=50;
  {
                int n=20;
                cout<<n;
                cout<<::n;
  }

 getch();
}
Ans:-20 100
Discussion:-Here n is declared three times. So within inner scope the value of n is 20.And the scope resulation operator always takes global value of n.

Q7)Output of the program.

#include "iostream.h"
#include "conio.h"
main()
{
 int a=100;
 int &b=a;

 cout<<a<<endl;
 cout<<b<<endl;
 a=200;
 cout<<b<<endl;
 getch();
}
Ans:-100
          100
          200              
Discussion:-Here the variable is declared as a reference of variable a.So,When the value of a is changed the value of b also automatically changed.


Q8)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
{
 private:const int a;
 public :void change()
                                                {
                                                 a++;
                                                }
}
main()
{
 test t;
 t.change();

 getch();
}
Ans:-Code is erroneous.
Discussion:-Here a is declared as constant data member, and the change() function is trying to change the constant value. So it will produce error.

Q9)Detect the error.

#include "iostream.h"
#include "conio.h"
class test
{
 private:int a;
 public: int b;
 protected:int c;
};

main()
{
 test t;
 t.a=100;
 t.b=200;
 t.c=300;

 getch();
}
Ans:-Compile time error.
Discussion:-Here a and b data member are declared as privately and protected simultaneously. So The initialization t.a=100,and t.b=200 is wrong.



Q10)Output of the program
#include "iostream.h"
#include "conio.h"
void fun()
{
 class test
 {
  int a;

  public:void getdata()
                                 {
                                                                a=100;
                                 };
                                 void putdata()
                                 {
                                                                cout<<a;
                                 };

 };
test t;
t.getdata();
t.putdata();
};
main()
{
 fun();
 getch();
}
Ans:-100
Discussion:-Here test class is declared within fun() function. And it is syntactically correct.


 Q11)Find the error.
#include "iostream.h"
#include "conio.h"
void fun()
{
 class test
 {
  int a;

  public:void getdata()
                                 {
                                                                a=100;
                                 };
                                 void putdata()
                                 {
                                                                cout<<a;
                                 };

 };
test t;
t.getdata();
t.putdata();
};
main()
{
 test t1;
 getch();
}
Ans:-Compile error.
Discussion:-Here test class is declared inside the fun() function. So the scope of test class is within the fun() function. We cannot make object of test class within main function.
 

Q12)Output of the program.

#include "iostream.h"
#include "conio.h"
class test
 {

                 class test1
                 {
                                private:int a;
                                public :void fun()
                                                                {
                                                                 a=100;
                                                                 cout<<a;
                                                                }

                };

};

main()
{
 test1 t;
 t.fun();
 getch();
}
Ans:-100
Discussion:-Here test1 class is declared within test class. As both the class is global, it will work fine.

 Q13)Output of the program.
#include "iostream.h"
#include "conio.h"
class
 {
                public:int a;
                public:void fun()
                                                 {
                                                  a=100;
                                                  cout<<a;
                                                 }


 }t;

main()
{
 t.fun();
 getch();
}
Ans:-100
Discussion:-Though here class name is missing, here t  is a class object. And it works fine.

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



 };

main()
{
 test t;
 t.getdata(100);
 t.putdata();
 getch();
}
Ans:-Some garbage value.
Discussion:-In TC++ compiler  it gives garbage value. Here getdata function is calling by one argument.


Q15)Output of the program.
#include "iostream.h"
#include "conio.h"
class test
 {
  private:int a;
  public :void fun();
 };

main()
{
 test t;
 cout<<&t;

 getch();
}
Ans:-Adress of object t;
Discussion: Here within cout<< function the address of object has printed.


No comments:

Post a Comment