Expt 5 Inheritance
Expt 5 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.
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
};
Here:
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
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.
class Base {
public:
void display() {
cout << "Base class display 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.