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

Unit 5 Inheritance

Inheritance in C++ allows a new class to be derived from an existing class, inheriting its member variables and functions. There are three main types of derivations: private, public, and protected, which dictate the accessibility of base class members in the derived class. Additionally, C++ supports five forms of inheritance: single, multiple, hierarchical, multilevel, and hybrid inheritance.
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)
4 views10 pages

Unit 5 Inheritance

Inheritance in C++ allows a new class to be derived from an existing class, inheriting its member variables and functions. There are three main types of derivations: private, public, and protected, which dictate the accessibility of base class members in the derived class. Additionally, C++ supports five forms of inheritance: single, multiple, hierarchical, multilevel, and hybrid inheritance.
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

Unit-5

Inheritance
1. Write about Inheritance? Explain about Different Types Derivations?
Or
1. State the inheritance? Explain the different Categories of Derivations?
Or
1. Discuss about Private, Public and Protected Derivations?
Inheritance:

 Inheritance means a new class is extracted from an existing class.


 The new class inherits all the member variables and functions from the
Existing class.
 Inheritance involves a base class and a derived class.
 The base class is also called as general class.
 The derived class is the specialized class.
 The base class is called as parent class and the derived class is as the child
class.
 The parent class is called as super class and the child class is as sub class.
Representation of Inheritance:

Example:

Types of Derivations: Or Access Specifier/Access Modifier/Access Controller


 C++ provides a no of ways to access to class members.
 A derived class can be declared with one of the access control specifier.
 There are three types of derivations.
i) Private
ii) Public
iii) Protected
 The access specifier determines how the derived class gets the elements of the
base class.
i) Private Derivation/Private Inheritance:
 Private is the access control specifier.
 When a new is deriving with a private from base class, then public and protected
members of the base class will become private members of the derived class.
i) Public Derivation/ Public inheritance:
 Public is the access control specifier.
 When a new class is derived with public from base class, then private and protects
members of the base class will become public members of the derived class.
iii) Protected Derivation:
 Protected is the access control specifier.
 When new class is derived with protected from base class, then both public
members and protected members will become protected members of the derived
class.

2. What is meant by Inheritance? Explain about different form of


Inheritance?
Or
2. Describe about different types of Inheritance with example?
Inheritance:
 Inheritance means a new class is extracted from an existing class.
 The new class inherits all the member variables and functions from the Existing
class.
 Inheritance involves a base class and a derived class.
 The base class is called as general class.
 The derived class is the specialized class.
 The base class is called as the parent class and the derived class is as the child
class.
 The parent class is called as super class and the child class is as sub class.
Representation of Inheritance:

Example:

Types of Inheritance or Different Forms of Inheritance:

In C++, we have 5 different types of Inheritance. Namely,


1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
1. Single Inheritance
 The new class is extracted from only one base class.
 This is known as Single Inheritance.
 It is the simplest of inheritance.
 It is the simplest form of Inheritance.
 Colon (:) is used to derive a new class from base class.
Syntax: class subclass: base class name
Representation of Single Inheritance:

Syntax 2:
Syntax 1:
Class A //base Class
Class A // Base class
{
{
Public: Public:

void function A( ) Void function()


{ {
}
}
}
};
Class B:A //Derived class
Class B : Access Specifier Base Class name
{
{
Public
void function B( ) Public:

{ Void function B()

} {
}
}
2. Multiple Inheritances };

 The new class is derived from two or more than two base classes.
 This is known as Multiple Inheritance.
Representation of Multiple Inheritance:

Syntax:

Class A //Base class


{
Public
void function A( )
{
}
}
Class B //Base class
{
Public
void function B( )
{
}
}
Class C: A, B //Derived class
{
Public
void function C( )
{
}
}
Hierarchical Inheritance
 Multiple derived classes are derived from a single base class.
 This is treated as Hierarchical inheritance.
 In this Inheritance, number of derived classes existed from only on base class.
Representation of Hierarchical Inheritance:

Syntax 1:
Syntax 2:
Class A // base class
Class A // Base class
{
{
Public :
Public:
void function A( )
Void Function A()
{
{
} }
} };
Class B: A //Derived class Class B : Access Specifier Base Class name
{ {
Public : Public:
void function B( ) Void Function B()
{ {
} }
} };
Class C:A //Derived class Class C : A
{ {
Public Public:
void function C( ) Void function C()
{ {
}
}
};
}
Multilevel Inheritance

 The procedure of deriving a class from a derived class is called multilevel


inheritance.

Representation of Multilevel Inheritance:

Syntax:

Class A //base class


{

Public

void function A( )

}
}

Class B: A //Derived class

Public

void function B( )
{

}
}

Class C:B //Derived class


{
Public

void function C( )

{
}

}
Hybrid (Virtual) Inheritance in C++

 Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.


 Hybrid Inheritance = hierarchical + Multi-level Inheritance.
 This is called Hybrid Inheritance and also called Virtual Inheritance.

Representation of Hybrid Inheritance:

Syntax:
Class A //Base class

Public

void fooA( )

{
}

}
Class B:A //Derived class

{
Public

void fooB( )

{
}

}
Class C:A //Derived class
{

Public
void fooC( )

{
}
}

Class D: B,C //Derived class


{

Public
void fooD( )

{
}

};

Write a c++ program to solve the single inheritance?


#include <iostream.h>
#include<constream.h>
class integer
{
protected: //Access specifier
int x; //member data/variable
public:
void get(); //method/function declaration
void display(); //method/function declaration
void increment(); //method/function declaration
};
void integer :: get() //member function definition
{
cin >> x;
}
void integer :: display() //member function definition
{
cout << x << "\n";
}
void integer :: increment() //member function definition
{
x++;
}
class d_integer : public integer // new class derived from base class
{
public:
void decrement(); //member function declaration
};
void main()
{
clrscr();
d_integer d; //derived class object
cout << "Enter a value for d \n";
d.get();
cout << "The value of d =";
d.display();
d.increment();
cout << "the value of d after incrementing \n";
d.display();
}

You might also like