marq

Dr. Charles Simonyi is the Father of Modern Microsoft Excel                                           JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.                                           The word "Biology" is firstly used by Lamarck and Treviranus                                           Hippocrates (460-370 bc) is known as father of medicine.                                           Galene, 130-200 is known as father of Experimental Physology                                           Aristotle (384-322 BC) is known as Father of Zoology because he wrote the construction and behavior of different animals in his book "Historia animalium"                                           Theophrastus(370-285 BC) is known as father of Botany because he wrote about 500 different plants in his book "Historia Plantarum".                                           John Resig is known as Father of Jquery -                                          HTML is a markup language which is use to design web pages. It was invented in 1990 by Tim Berners-Lee.                                                                The Google was founded by Larry Page and Sergey Brin.                                                                Rasmus Lerdorf was the original creator of PHP. It was first released in 1995.                                                               Facebook was founded by Mark Zuckerberg                                                               Bjarne Stroustrup, creator of C++.                                                                Dennis Ritchie creator of C                                                                                                                              James Gosling, also known as the "Father of Java"                                          At 11.44%, Bihar is India's fastest growing state                                          Father of HTML -Tim Berners Lee                                          orkut was created by Orkut Büyükkökten, a Turkish software engineer                    Photoshop: It came about after Thomas Knoll, a PhD student at the University of Michigan created a program to display grayscale images on a monochrome monitor which at the time was called 'Display'.

question answer inheritance


What is a base class?

Inheritance is one of the important features of OOP which allows us to make hierarchical classifications of classes. In this, we can create a general class which defines the most common features. Other more specific classes can inherit this class to define those features that are unique to them. In this case, the class from which other classes are inherited is referred as base class.

For example, a general class vehicle can be inherited by more specific classes car and bike. The class vehicle is base class in this case.

class Base
{
     int a;
     public:
         Base()
         {
                   a = 1;
                   cout <<”inside Base class”;
         }
};

class Derived:: public Base //class Derived is inheriting class Base publically
{
     int b;
     public:
            Derived()
            {
                   b = 1;
                   cout <<”inside Derived class”;
            }
};

What is private inheritance?

When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is private:

i. Private members of base class are not accessible to derived class.
ii. Protected members of base class become private members of derived class.
iii. Public members of base class become private members of derived class.
#include <iostream>
using namespace std;

class base
{
      int i, j;
      public:
       void setij(int a, int b)
      {
           i = a;
           j = b;
      }

       void showij()
       {
             cout <<”\nI:”<<i<<”\n J:”<<j;
       }
};

class derived : private base
{
      int k;
      public:
      void setk()
      {
           //setij();
           k = i + j;
      }
      void showall()
      {
               cout <<”\nK:”<<k<<show();
      }
};

int main()
{
            derived ob;
            //ob.setij(); // not allowed. Setij() is private member of derived
            ob.setk(); //ok setk() is public member of derived
            //ob.showij(); // not allowed. Showij() is private member of derived
            ob.showall(); // ok showall() is public member of derived
            return 0;
}


What is protected inheritance?

When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is protected:

Private members of base class are not accessible to derived class.
Protected members of base class remain protected in derived class.
Public members of base class become protected in derived class.
#include <iostream>
using namespace std;

class base
{
      protected:
      int i, j;
      public:
      void setij(int a, int b)
      {
             i = a;
             j = b;
      }
      void showij()
      {
             cout <<”\nI:”<<i<<”\n J:<<j;
      }
};

class derived : protected base
{
       int k;
       public:
       void setk()
       {
              setij();
              k = i + j;
       }
       void showall()
       {
               cout <<”\nK:”<<k<<show();
       }
};

int main()
{
        derived ob;
        //ob.setij(); // not allowed. Setij() is protected member of derived
        ob.setk(); //ok setk() is public member of derived
        //ob.showij(); // not allowed. Showij() is protected member of derived
       ob.showall(); // ok showall() is public member of derived
       return 0;
}




What are the advantages of inheritance?

Advantages of Inheritance:

Allows the code to be reused as many times as needed. The base class once defined and once it is compiled, it need not be reworked.
Saves time and effort as the main code need not be written again.




How do we implement inheritance in C++?

The inheritance feature in C++ is implemented in the following manner:
class Square: public Shape
{
        ...
};

In the above example all public members of Shape can be reused by the class Square. If public key word is left, private inheritance takes place by default. If protected is specified the inheritance is applied for any descendant or friend class.




What is private inheritance?

When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is private:

i. Private members of base class are not accessible to derived class.
ii. Protected members of base class become private members of derived class.
iii. Public members of base class become private members of derived class.
#include <iostream>
using namespace std;

class base
{
      int i, j;
      public:
       void setij(int a, int b)
      {
           i = a;
           j = b;
      }

       void showij()
       {
             cout <<”\nI:”<<i<<”\n J:”<<j;
       }
};

class derived : private base
{
      int k;
      public:
      void setk()
      {
           //setij();
           k = i + j;
      }
      void showall()
      {
               cout <<”\nK:”<<k<<show();
      }
};

int main()
{
            derived ob;
            //ob.setij(); // not allowed. Setij() is private member of derived
            ob.setk(); //ok setk() is public member of derived
            //ob.showij(); // not allowed. Showij() is private member of derived
            ob.showall(); // ok showall() is public member of derived
            return 0;
}


What are the different types of Inheritance?

Single Inheritance
A (parent class) -> B (child class)
Multiple Inheritance
A -> C, B -> C
Hierarchical inheritance
A -> B, A -> C, A -> D
Multilevel inheritance
A -> B, B -> C
Hybrid inheritance
A -> B, A -> C, B -> D, C -> D.


Explain the problem with overriding functions.

Overriding of functions occurs in Inheritance. A derived class may override a base class member function. In overriding, the function names and parameter list are same in both the functions. Depending upon the caller object, proper function is invoked.

Consider following sample code:

class A
{
        int a;
        public:
          A()
          {
                  a = 10;
          }
          void show()
          {
                cout << a;
          }
};

class B: public A
{
        int b;
        public:
        B()
        {
              b = 20;
        }
        void show()
        {
                 cout << b;
        }
};

int main()
{
        A ob1;
        B ob2;
        ob2.show(); // calls derived class show() function. o/p is 20
        return 0;
}

As seen above, the derived class functions override base class functions. The problem with this is, the derived class objects can not access base class member functions which are overridden in derived class.

Base class pointer can point to derived class objects; but it has access only to base members of that derived class.

Therefore, pA = &ob2; is allowed. But pa->show() will call Base class show() function and o/p would be 10.

No comments:

Post a Comment