C++ Inheritance
C++ Inheritance
C++ Inheritance
C++ Inheritance
In C++, inheritance is a process in which one object
acquires all the properties and behaviors of its
parent object automatically.
In such way, we can reuse, extend or modify the
attributes and behaviors which are defined in other
class.
In C++, the class which inherits the members of
another class is called derived class and the class
whose members are inherited is called base class.
The derived class is the specialized class for the
base class.
Advantages of Inheritance in C++
1. Code Reusability – Inheritance in C++ allows you to
create new classes that are based on existing classes,
so you can reuse existing code and avoid rewriting the
same functionality. This can save a lot of time and
effort when you’re creating new programs.
/////////
};
class B: visibility_mode A
{
///////////
}
C++ Multilevel Inheritance
• Multilevel inheritance is a
process of deriving a class from
another derived class.
• one class inherits another class
which is further inherited by
another class, it is known as multi
level inheritance in C++.
• Inheritance is transitive so the last
derived class acquires all the
members of all its base classes.
Syntax
class A
{
};
class B: visibility_mode A
{
};
class C: visibility_mode B
{
};
C++ Multiple Inheritance
• Multiple inheritance is the process of deriving a
new class that inherits the attributes from two
or more classes.
Syntax
class A
{
};
class B
{
};
class C: visibility_mode_1 A, visibility_mode_2 B
{
};
C++ Hierarchical Inheritance
• Hierarchical inheritance is defined as the
process of deriving more than one class from a
base class.
Syntax
class A
{
};
class B: visibility_mode A
{
};
class C: visibility_mode A
{
};
class D: visibility_mode A
{
};
C++ Hybrid Inheritance
• Hybrid inheritance is a combination of more
than one type of inheritance.
Syntax
class A
{
};
class B: visibility_mode A
{
};
class C: visibility_mode A
{
};
class D: visibility_mode B, visibility_mode C
{
};