DISHA NIRDESHAN

Enlightens your career

Tuesday, June 21, 2011

Questions on Inheritance

INHERITANCE

What is inheritance, explain with an example?

Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them

#include <iostream>
using namespace std;

class BaseClass {
  int i;
public:
  void setInt(int n);
  int getInt();
};

class DerivedClass : public BaseClass {
  int j;
public:
  void setJ(int n);
  int mul();
};

void BaseClass::setInt(int n)
{
  i = n;
}

int BaseClass::getInt()
{
  return i;
}

void DerivedClass::setJ(int n)
{
  j = n;
}

int DerivedClass::mul()
{
  return j * getInt();
}

int main()
{
  DerivedClass ob;

  ob.setInt(10);        // load i in BaseClass
  ob.setJ(4);          // load j in DerivedClass

  cout << ob.mul();     // displays 40

  return 0;
}

 

 

What is a base class? Explain with an example using C++.

 

 

#include <iostream>
using namespace std;

class BaseClass {
  int i, j;
public:
  void set(int a, int b) { 
     i = a; 
     j = b; 
  }
  void show() { 
     cout << i << " " << j << endl; 
  }
};

class DerivedClass : public BaseClass {
  int k;
public:
  DerivedClass(int x) { 
     k = x; 
  }
  void showk() { 
     cout << k << endl; 
  }
};

int main()
{
  DerivedClass ob(3);

  ob.set(1, 2); // access member of BaseClass
  ob.show();    // access member of BaseClass

  ob.showk();   // uses member of DerivedClass class

  return 0;
}

 

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 Base Class?

The base class is the highest class and does not inherit from any other class. Other classes can inherit from a base class. They are called derived classes.

 

class A {
  public:
    int x;
};
class B : public A {
  public:
    int y;
};
class C : public B { };

 

Explaination:

Class A is base class

 

What are Direct Base class and indirect base class?

 

A direct base class is a base class that appears directly as a base specifier in the declaration of its derived class.

An indirect base class is a base class that does not appear directly in the declaration of the derived class but is available to the derived class through one of its base classes. For a given class, all base classes that are not direct base classes are indirect base classes. The above example demonstrates direct and indirect base classes:

Class B is a direct base class of C. Class A is a direct base class of B. Class A is an indirect base class of C. (Class C has x and y as its data members.)

 

 

What is private, public and protected Inheritance?  

Private Inheritance
The Public and protected members of Base class become private members of the derived class.

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.

 

Public Inheritance
All the public members and protected members are inherited as public and protected respectively.

Protected Inheritance
Public and Protected members are derived as protected members.

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.

0 comments :