C++ Inheritance introduction

C++ Inheritance introduction

Introduction:

The Reusability is an another important feature of the Object Oriented programming. it is always good if we could re-use something that already exists rather than trying to create the same all over again and again. It would not only save money and time and but also reduce frustration and also increase the reliability. for instance the re-use of a class that has already been tested, debugged and used many times can save us the effort of developing and testing the same again.

Fortunately , C++ strongly support the concept of reusability(inheritance). The c++ language classes can be reused in several ways. once a class has been written and tested, it can be use by other programmers to suit their requirements . This is basically done by creating new classes, reusing the properties of the existing class. The mechanism of deriving a new class by using an old one is called inheritance. The old class is referred to as the base class or parent class and the new one is called the derived class or subclass

The derived class inherits some or all of the features of the base class. A class can also inherit properties from more than one class and of from more than one level.

A derived class also known as(child class) with only one base class, is called single level inheritance and one with several base classes is called multiple inheritance. on the other hand , the features of one class may be inherited by more than one class. This process is know as hierarchical inheritance.

in the below figure we will show the types of inheritance

single inheritance pic multiple inheritance pic

hierarchical inheritance pic in c++ multilevel inheritance pic in c++

hybrid inheritance pic in c++

Defining derived Class:

A derived class can be defined by specifying its relationship with the base in addition to its own details. The general form of defining a derived class is

class derived-class-name : visibility-mode base-class-name
{
………..
………..
……….. // members of derived class
………..
};

The colon indicates that the derived class(subclass) name is derived from the base class(super class) name The visibility-mode is optional and if present, may be either public or the private . but The default visibility mode is private. visibility mode specifies the features of the base class are privately derived or publicly derived to the derived class.

Let us show example

class ABC: private XYZ // private derivation
{
members of ABC
};
class ABC: public XYZ // its public derivation
{
members of ABC
};
class ABC: XYZ // its private derivation by default
{
members of ABC
};

When a base class is privately inherited by a derived class, public members of the base class becomes private members of the derived class and therefore the public members of the base class can only be accessed by the member function of the derived class. They are inaccessible to the objects of the derived class. But Remember that , a public member of a class can be accessed by its own objects using the dot(.) operator. The result is that no member of the base class is accessible by using the objects of the derived class.

on the other hand, when the base class is publicly inherited, public members of the base class will be become public members of the derived class and therefore they are accessible by the object of the derived class. in both the class, but the private members are not inherited and therefore, the private members of a base class will never become the members of its derived class

In the inheritance some data elements of the base class and members functions are inherited into the derived class. you can add your own data and member functions and thus extend the functionality of the base class. inheritance, when used to modify and extend the capabilities of the existing classes, becomes a very powerful tool for incremental program development

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise