Sunday, January 29, 2012

Puzzle code of inheritence in C++

Q1)Output of the program.
#include "iostream.h"
#include "conio.h"
class base
{
 private:void print()
                {
                                  cout<<"Base class";
                }

};
class derive:public base
{
 public:void call()
                  {
                                 cout<<"Derive class";
                  }
}

main()
{
derive d;
d.print();
 getch();
}
Ans:-Compile time error.
Discussion:-Here derive class is publicly inherited from base class. So the print() function of base class remain private member in derive class. So  the print() function call is not possible  using direct object.

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


};
class derive:public base
{
 public:void call()
                  {
                                 print();
                  }
};

main()
{
derive d;
d.call();
 getch();
}
Ans-100
Discussion:-Here derive class is publicly inherited from base class. So derive  class function  call() is calling the base class function print().

Q3) Output of the program.
#include "iostream.h"
#include "conio.h"
class base
{
  public: base()
                  {
                                 cout<<"Base class constructor"<<endl;
                  }


};
class derive:public base
{
 public:derive()
                 {
                                 cout<<"Derive class constructor"<<endl;
                                 base();
                 }

};

main()
{
derive d;
getch();
}
Ans:-Base class constructor.
         Derive  class constructor.
         Base  class constructor.

Discussion:-When the constructor of derive class is call, then at first the base class constructor is called then sub class constructor is call.

Q4)Output of the program
#include "iostream.h"
#include "conio.h"
class base
{
  public:void abc()
                                  {
                                                 cout<<"Base class"<<endl;
                                  };


};
class derive:private base
{
 public:void xyz()
                                 {
                                                abc();
                                 };

};

main()
{
derive d;
d.abc();
getch();
}
Ans:-Compile time error.
Discussion:-Here derive class is privately inherited from base class.  So  public member of base class will be the private member of sub class.

Q5)Output of the program
#include "iostream.h"
#include "conio.h"
class base
{
  public:void abc()
              {
                                 cout<<"Base class"<<endl;
                 };


};
class derive:private base
{
 public:void xyz()
                 {
                                abc();
                 };
};
class low:public derive
{
                  public:void hello()
                  {
                                  xyz();
                 }

};
main()
{
low l;
l.hello();
getch();
}
Ans:-Base class.
Discussion:-It is the example of multi level inheritance. Her e hello() function is calling it’s superclass xyz() function and xyz() function is calling it’s super class  function abc().And within abc() function ,the message is printing.

Q6)Output of the program.
#include "iostream.h"
#include "conio.h"
class base
{
  protected:void abc()
                      {
                                 cout<<"Base class"<<endl;
                      };


};
class derive:private base
{
 public:void xyz()
                 {
                                abc();
                 };
};

main()
{
derive d;
d.xyz();
getch();
}
Ans:-Base class.
Discussion:-Here derive class is privately inherited from base class. So protected member of base class will be the private member of derive class.


Q7)Output of the program.
#include "iostream.h"
#include "conio.h"
class base
{
  protected:void abc()
                      {
                                cout<<"Base class"<<endl;
                      };


};
class derive:public base
{
 public:void xyz()
                 {
                                abc();
                 };
};

main()
{
derive d;
d.abc();
getch();
}
Ans:-Compile time error.
Discussion:-Here derive class is inherited publicly. So the protected function member abc() remain protected in derive class. So it is not possible to access the protect  function member using  . operator.


Q8) Output of the program.
#include "iostream.h"
#include "conio.h"
class abc
{
 private  :void abcfun()
                  {
                                cout<<"Super class1"<<endl;
                  };
 public:  void callabc()
                 {
                              abcfun();
                 }
};
class xyz:public abc
{
 public:void xyzfun()
                 {
                                cout<<"Super class2";
                              callabc();

                };
};
class pqr:private abc,public xyz
{
 public :void pqrfun()
                {
                                 cout<<"Derive class";
                }
};

main()
{
 pqr p;
 p.pqrfun();
 p.xyzfun();
 getch();
}

Ans:-Derive class Super class2 Super class1
Discussion:- It is a example of hierarchical inheritance. Here xyz class is publicly inherited from  base class abc and class pqr is inherited both from abc as privately and xyz  as publicly.


Q9)Output of the program.
#include "iostream.h"
#include "conio.h"
class abc
{
 private:int a;
 public:abc()
            {
                                cout<<"Hello";
             };

};

class xyz:public abc
{
 private:int x;
 public:xyz()
             {
                                abc();
             };

};

class pqr:public xyz,public abc
{
 public:pqr()
                 {
                                xyz();
                  }
};

main()
{
 pqr p;

 getch();
}
Ans:-HelloHelloHelloHello


Q10)Output of the program
#include "iostream.h"
#include "conio.h"
class abc
{
                public:void print()
                            {
                                  cout<<"abc";

                             };
};

class xyz
{
 public:void print()
                 {
                                  cout<<"xyz";

                 };


};

class pqr: public xyz,public abc
{


};
main()
{
 pqr p;
 p.abc::print();                   //Call to print function of abc class
 p.xyz::print();                   // Call to print function of xyz class

 getch();
}
Ans:-abc  xyz
Discussion:-Here pqr  class is publicly inherited from both the class abc and xyz.
                      And is print() function is common both two class ,so we can invoke individual print() function using scope resolution operator.



No comments:

Post a Comment