blog

Home / DeveloperSection / Blogs / Inheritance in C++

Inheritance in C++

priyanka kushwaha2241 05-Feb-2015

In this blog, I’m explaining about Inheritance in C++

 

Inheritance is the ability to create a class from another class, the “parent” class, extending the functionality and state of the parent in the derived, or “child” class. It allows derived classes to overload methods from their parent class.

Types of inheritance in C++

1. Single Inheritance

2. Multilevel Inheritance

3. Multiple Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance (also known as Virtual  Inheritance)

Syntax :

Class DeriveName: Access Specifier BaseClass


1.   In Single Inheritance  base class functionality access in derive class.

2.  In Multilevel Inheritance a Derived class can also inherited by another class

3.   Multiple Inheritance is that in which a class inherits the features from two Base Classes when a Derived Class takes features from  two Base class

4.  Hierarchical Inheritance in which a Base class is inherited by many sub class.

5.   Hybrid Inheritance : This  is a Mixture of two or More inheritance and  in this Inheritance a Code may contains two or three types of inheritance in single code.

Example
// Inheritance.cpp : main project file.
 
#include"stdafx.h"
#include<iostream>
usingnamespace System;
usingnamespace std;
 
 class  Rectangle
{
public:
            double length,breadth;
            void  get(doublel,doubleb)
            {
                        length=l;
                        breadth=b;
            }
            double   area()
            {
                        return(length*breadth);
            }
            Rectangle();
            ~ Rectangle();
 
private:
 
};
 publicclassCube:publicRectangle     //Single inheritance
{public:
Rectangle obj;
public: double height;
                        double  area()
                        {double res;
                                    res=obj.area();
                                    return(res*height);
                        }
                        void get(doublel,doubleb,doubleh)
                        {
                                    obj.length=l;
                           obj.breadth=b;
                                    height=h;
                        }
            Cube();
            ~Cube();
 
private:
 
};
 
 
 
Cube::Cube()
{
}
 
Cube::~Cube()
{
}
 
 Rectangle:: Rectangle()
{
}
 
 Rectangle::~ Rectangle()
{
}
int main(array<System::String ^> ^args)
{
            Cube  Ref;
            double result;
            Ref.get(2,3,3);
            result=Ref.area();
            cout<<"Area of cube";
            cout<<result;
                        Console::ReadKey(0);
    return 0;
}

Updated 05-Feb-2015

Leave Comment

Comments

Liked By