C++ Programming & Tutorials for Beginners

Inheritance

Home  »  C++ Programming  »  Inheritance


     The main idea behind inherintance is to reused, enhance and increase the reliability of the already, existing code. Because of this the inheritance save not only the precious time and money but also lessens the burden and frustration of the programmer.


Contents



Inheritance-Definition

     Inheritance is the most powerful feature of OOPS. In C++ it is implemented by creating the new classes from the existing class. Here, we can take the form of the existing class and then add code to it, without modifying the existing class.

Inheritance means using the Pre-defined Code This is very Main Feature of OOP With the advantage of Inheritance we can use any code that is previously created With the help of inheritance we uses the code that is previously Defined but always Remember We are only using that code but not Changing that code With the Advent of inheritance we able to use pre-defined code and also able to add new code . All the pre-defined code is reside into the form of classes if we wants to use that code then we have to inherit or extend that class The Class that is Pre-defined is called as Base or super Class and the class which uses that code is called as derived or sub class. Below is an example of Inheritance :






Types of Derivation

     Inheritance refers to creation of new class from an existing base class. The members of derived class can inherit the properties class. In other words, the members of derived class access the member functions of base class. There are basically three different types of accessing standards/specifiers/derivations used by which the derived class object access the member functions of base class. These derivations are public, private and protected.

Public Derivation
Public derivation is the most typical important type for accessing base class members. A public derivation informs the compiler that the object of the derived class can access the public as well as protected member functions of the base class. It cannot access the private members of the base class. The private members in base class remains private in the base class. The general syntax of public derivation is given below:

class base_class_name
{
  _____________________
  _____________________
};
class derived_class_name : public base_class_name
{
  _____________________
  _____________________
};


Private Derivation
A private derivation informs the compiler that the object of the derived class can't access even the public base class member functions. In other words, we can say that under private derivation, each public member in the base class is private in the derived class, each protected member in the base class is private in the derived class and each private member in the base class remains the same in base class. The general syntax of the private derivation is given below:

class base_class_name
{
  _____________________
  _____________________
};
class derived_class_name : private base_class_name
{
  _____________________
  _____________________
};


Protected Derivation
The data members of class can be said to be protected if they can be accessed by the member functions can friends of that class and also by the member functions and friends derived from that class. Therefore we can say that in protected derivation, each public member in the base class is protected in the derived class, each protected member in the base class is protected. In the derived class and each private member in the base class remains private in the base class. The general syntax of the protected derivation is given below:

class base_class_name
{
  _____________________
  _____________________
};
class derived_class_name : protected base_class_name
{
  _____________________
  _____________________
};





Types of Base Classes

     A base class is the one through which various new classes can be derived. Any class can be used as a base class. A derived class may act as a base class for the further derived classes. Base classes can be clearly classified into two secitons. These are: Direct base classes and Indirect base classes.

Direct Base Classes
A direct base class of a derived class is explicity listed in that derived class header with the colon (:) notaion when that derived class is declared. The general format of a simple declarationof a base class in the derived class notation is given below:

class base
{
  _____________________
  _____________________
};
class derived : public base
{
  _____________________
  _____________________
};

Indirect Base Classes
An indirect base class is not explicitly listed in derived class header rather the indirect base class in inherited from two or more levels up to the class hierarchy. In other words, we can say that a derived class can itself serve as a base class subject to access control. The general format for declaring indirect base class in hierarchical manner in given below:

class base1
{
  _____________________
  _____________________
};
class derived1 : public base1
{
  _____________________
  _____________________
};
class derived2 : public base2
{
  _____________________
  _____________________
};





Nested Classes

     When a class is declared and defined as a member of another clas, it is known as a nested class. This is also called as containership. The general form of a nested class can be given below:
first class
{
  _____________________
  _____________________
  second obj;    //obj is the object of class second
  _____________________
};
class second
{
  _____________________
  _____________________
};



The following program shows nested classes/containership.



Output



Previous
Next Post »