Inheritance in C++





Inheritance is one of the key features of Object-oriented programming in C++. It allows user to create a new class (derived class) from an existing class(base class).

  • The mechanism of creating a new class from an old one is called inheritance (derivation).

  • The old class is referred as PARENT orBASE or SUPER class and the new class is called CHILD orDERIVED or SUB class.

  • The derive class inherits some or all of the members( properties ) from the base class.

  • A class can also inherit properties from more than one class.

  • The most important feature of inheritance is code reusability .

  • Inheritance implements the is a relationship.

Syntax:

class derived-class : [virtual] [access-specifier] base-class
Example:
#include<iostream>
using namespace std;
//base class
class A {
public:
int x;
void show ( ){
cout<<"HELLO : SHOW"<< endl;
}
};
//derived class
class B : public A {
public:
void print ( ){
cout<<"HELLO : PRINT"<< endl;
}
};
int main (){
B ob;
/*Here, B’s object can 
  access all the member of A */
ob.x = 50;
ob.show( );
ob.print( );
}             
OUTPUT:
HELLO : SHOW
HELLO : PRINT

Inheritance Access Specifiers:
  • The access specifiers ( visibility modes ) of inheritance control the access of the inheritable members of base class in derived class.

  • Constructor and private members of base class are not inherited in derived class.

  • Basically there are three types of visibility modes:

    1. Public
    2. Protected
    3. Private

1. Public Inheritance:
  • Syntax:

    class derived-class : public  base-class
  • The access of inherited member in derived class is same as in base class.

  • Member access details:
    Public in BasePublic in Derived
    Protected in BaseProtected in Derived
    Private in BaseNot Inherited
2. Protected Inheritance:
  • Syntax:

    class derived-class : protected  base-class
  • The access of inherited member change in derived class.

  • Member access details:
    Public in BaseProtected in Derived
    Protected in BaseProtected in Derived
    Private in BaseNot Inherited
3. Private Inheritance:
  • Syntax:

    class derived-class : private  base-class
                   OR
    class derived-class : base-class
  • This is default visibility mode for inheritance.

  • The access of inherited member change in derived class.

  • Member access details:
    Public in BasePrivate in Derived
    Protected in BasePrivate in Derived
    Private in BaseNot Inherited


Types of Inheritance:
  • There are five different types of inheritance ...

    1. Single Inheritance
    2. Multiple Inheritance
    3. Hierarchical Inheritance
    4. Multilevel Inheritance
    5. Hybrid Inheritance (also known as Virtual Inheritance)

  • The above inheritance types are just organize the classes and establishes the relationship among classes to form the class hierarchy.

1. Single Inheritance:

Single inheritance represents a form of inheritance when there is only one base class and one derived class.

prowessapps.in
2. Multiple Inheritance:

One class inherits more than one class.

prowessapps.in
3. Hierarchical Inheritance:

A class inherited by more than one class.

prowessapps.in
4. Multilevel Inheritance:

Derived class further inherited by another class.

In that case Derived class is become a base class for another class.

prowessapps.in
5. Hybrid Inheritance (Virtual Inheritance):

"Hybrid Inheritance" is a method where one or more types of inheritance are combined together and used.

prowessapps.in

Constructor & Destructor in Inheritance
  • When an object of a derived class is created either using default or parameterized constructor, program automatically calls the default constructor of base class, then call the the constructor of derived class.

  • Similarly, when an object expires, program automatically calls the derived destructor, if any, and then invoke the base class destructor.
    Example:

    #include<iostream>
    using namespace std;
    //Base Class
    class A {
    public:
    A( ){
    cout<<"DEFAULT CONSTRUCTOR : A";
    cout<< endl;
    }
    A(int a){
    cout<<"PARAMETERIZED CONSTRUCTOR : A";
    cout<< endl;
    }
    ~A( ){
    cout<<" DESTRUCTOR : A"<< endl;
    }
    };
    //Derived Class
    class B : public A {
    public:
    B( ){
    cout<<"DEFAULT CONSTRUCTOR : B";
    cout<< endl;
    }
    B(int a){
    cout<<"PARAMETERIZED CONSTRUCTOR : B";
    cout<< endl;
    }
    ~B( ){
    cout<<" DESTRUCTOR : B"<< endl;
    }
    };
    int main (){
    B ob;
    B ob1(20);
    return 0;
    }                    
    OUTPUT:
    DEFAULT CONSTRUCTOR : A
    DEFAULT CONSTRUCTOR : B
    DEFAULT CONSTRUCTOR : A
    PARAMETERIZED CONSTRUCTOR : B
     DESTRUCTOR : B
     DESTRUCTOR : A
     DESTRUCTOR : B
     DESTRUCTOR : A
    

  • Calling base class constructor from derived class constructor:
    Implicitly every time default constructor gets executed whenever call the derived class constructor. But, you can explicitly call the other constructor of base class.

    Syntax:

    DerivedClassConstructor : BaseClassConstructor
    Example:
    #include<iostream>
    using namespace std;
    //Base Class
    class A {
    public:
    A( ){
    cout<<"DEFAULT CONSTRUCTOR : A";
    cout<< endl;
    }
    A(int a){
    cout<<"PARAMETERIZED CONSTRUCTOR : A";
    cout<< endl;
    }
    ~A( ){
    cout<<" DESTRUCTOR : A"<< endl;
    }
    };
    //Derived Class
    class B : public A {
    public:
    //Explicitly baseClass Constructor call
    B( ) : A(20){
    cout<<"DEFAULT CONSTRUCTOR : B";
    cout<< endl;
    }
    B(int a){
    cout<<"PARAMETERIZED CONSTRUCTOR : B";
    cout<< endl;
    }
    ~B( ){
    cout<<" DESTRUCTOR : B"<< endl;
    }
    };
    int main (){
    //This will call parameterized 
    //constructor of BaseClass	
    B ob;
    B ob1(20);
    return 0;
    }      
    OUTPUT:
    PARAMETERIZED CONSTRUCTOR : A
    DEFAULT CONSTRUCTOR : B
    DEFAULT CONSTRUCTOR : A
    PARAMETERIZED CONSTRUCTOR : B
     DESTRUCTOR : B
     DESTRUCTOR : A
     DESTRUCTOR : B
     DESTRUCTOR : A
    

  • When multiple inheritance is used, default constructors of base classes are called in the order as they are in inheritance list.

    
    class derived :public baseClass1,public baseClass2                                
                                
    In this case first baseClass1 constructor will call and after that baseClass2 constructor then derived class constructor.

Next topic is Polymorphism





 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved