0% found this document useful (0 votes)
10 views10 pages

Inheritance

Inheritance in C++ allows one object to acquire properties and behaviors from another, enabling code reusability and modification. The class that inherits is called the derived class, while the class being inherited from is the base class, with visibility modes including public, private, and protected. Various types of inheritance include single, multilevel, multiple, hierarchical, and hybrid inheritance.

Uploaded by

jazz202426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Inheritance

Inheritance in C++ allows one object to acquire properties and behaviors from another, enabling code reusability and modification. The class that inherits is called the derived class, while the class being inherited from is the base class, with visibility modes including public, private, and protected. Various types of inheritance include single, multilevel, multiple, hierarchical, and hybrid inheritance.

Uploaded by

jazz202426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Inheritance

Inheritance is a process in which one object acquires all the properties and behaviors of
its parent object automatically. In such way, you can reuse, extend or modify the
attributes and behaviors which are defined in other class.

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.

Sub Class: The class that inherits properties from another class is called Sub class or
Derived Class.
Super Class: The class whose properties are inherited by sub class is called Base
Class or Super class.

Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is
no need to define the member again. So less code is required in the class.
 Visibility modes can be classified into three categories:
o Public: When the member is declared as public, it is accessible to all the
functions of the program.
o Private: When the member is declared as private, it is accessible within the class
only.
o Protected: When the member is declared as protected, it is accessible within its
own class as well as the class immediately derived from it.

Base Derived class visibility


class
visibility

Public Private Protected

Private Not Not Inherited Not Inherited


Inherited

Protected Protected Private Protected

Public Public Private Protected


Visibility of Inherited Members

class A
{
public:
int x;
protected:
int y;
private:
int z;
};

class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};

class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};

class D : private A // 'private' is default for classes


{
// x is private
// y is private
// z is not accessible from D
};

Types of Inheritance
o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance
Single Inheritance

Single inheritance is defined as the inheritance in which a derived class is inherited


from the only one base class.

The Syntax of Derived class:

class derived_class_name : visibility-mode base_class_name


{
// body of the derived class.
}
Program:
Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another derived class.

When 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.
Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes
from two or more classes.

Syntax of the Derived class:

class D : visibility B-1, visibility B-2, ….


{
// Body of the class;
}
Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class
from a base class.

Syntax of Hierarchical inheritance:

class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.

You might also like