0% found this document useful (0 votes)
25 views6 pages

Expt 5 Inheritance

Uploaded by

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

Expt 5 Inheritance

Uploaded by

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

Experiment No 5

Title: - Implementation of inheritance

Aim: - Study of Inheritance

Theory:-

 What is Inheritance:-
Inheritance is the process by which new classes called derived classes are created
from existing classes called base classes. The derived classes have all the features of the
base class and the programmer can choose to add new features specific to the newly created
derived class.
For example: a programmer can create a base class named fruit and define derived
classes as mango, orange, banana, etc. Each of these derived classes, (mango, orange,
banana, etc.) has all the features of the base class (fruit) with additional attributes or features
specific to these newly created derived classes. Mango would have its own defined features,
orange would have its own defined features, banana would have its own defined features,
etc. This concept of Inheritance leads to the concept of polymorphism.

 Base & Derived Classes:

A class can be derived from more than one class, which means it can inherit
data and functions from multiple base classes. To define a derived class, we use a
class derivation list to specify the base class (es). A class derivation list names one or
more base classes and has the form:

class BaseClass {
// Base class members
};

class DerivedClass : public BaseClass {


// Derived class members
};

Here:

 BaseClass is the parent or superclass.


 DerivedClass is the child or subclass.
 public inheritance allows the derived class to inherit the public members of the base
class as public members.
 Access Control and Inheritance:

A derived class can access all the non-private members of its base class.
Thus base- class members that should not be accessible to the member functions of
derived classes should be declared private in the base class.

 Public Inheritance: Public members of the base class remain public in the derived class.
 Protected Inheritance: Public members of the base class become protected in the derived class.
 Private Inheritance: Public and protected members of the base class become private in the
derived class.

We can summarize the different access types according to who can access
them in the following way:
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no

 Types of inheritance in C++ -

1. Single Inheritance: A class inherits from one base class.


2. Multiple Inheritance: A class inherits from more than one base class.
3. Multilevel Inheritance: A class inherits from a derived class, creating a chain of inheritance.
4. Hierarchical Inheritance: Multiple classes inherit from a single base class.
5. Hybrid Inheritance: A combination of two or more types of inheritance.

1. Single Inheritance:
In this type of inheritance one derived class inherits from only one base
class. It is the simplest form of Inheritance.
2. Multiple Inheritance:
In this type of inheritance a single derived class may inherit from two or
more than two base classes.

3. Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base
class.

4. Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which
in turn inherits from some other class. The Super class for one, is sub class
for the other.

5. Hybrid Inheritance (also known as Virtual Inheritance)


Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.
Sample Program:
Single Inheritance:
#include <iostream>
using namespace std;

class Base {
public:
void display() {
cout << "Base class display function" << endl;
}
};

class Derived : public Base {


public:
void show() {
cout << "Derived class show function" << endl;
}
};

int main() {
Derived obj;
obj.display(); // Inherited from Base class
obj.show(); // Defined in Derived class
return 0;
}

Conclusion: In this experiment, we have studied the concept inheritance and types.

Practical:
1. Write a c++ program that uses single inheritance to print student information. Create a
Person class as the base class and a Student class that inherits from Person.
2. Write a C++ program to demonstrate multilevel inheritance.
3. Write a C++ program to read and print employee information using multiple inheritance.

You might also like