Home > DeveloperSection > Blogs > Inheritance in C++

Inheritance in C


.NET C#  .NET  OOPS  Inheritance 
Ratings:
0 Comment(s)
 637  View(s)
Rate this:

Inheritance in C++

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>

using namespace System;

using namespace std;

 

 class  Rectangle

{

public:

            double length,breadth;

            void  get(double l,double b)

            {

                        length=l;

                        breadth=b;

            }

            double   area()

            {

                        return(length*breadth);

            }

            Rectangle();

            ~ Rectangle();

 

private:

 

};

 public class Cube:public Rectangle     //Single inheritance

{public:

Rectangle obj;

public: double height;

                        double  area()

                        {double res;

                                    res=obj.area();

                                    return(res*height);

                        }

                        void get(double l,double b,double h)

                        {

                                    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;

}


Don't want to miss updates? Please click the below button!

Follow MindStick