ABCA-2
PPT PRESENTATIOM
TOPIC :- introduction of inheritance
Subject Code:- CD 305
Submitted to : Submitted BY :
SORABH JATAV
[Link] 0905CE231018
sharma
INTRODUCTION OF INHERITANCE
The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most
important features of Object Oriented Programming in C++. In this
article, we will learn about inheritance in C++, its modes and types along
with the information about how it affects different properties of the
class.
2
SYNTAX OF INHERITANCE IN C++
class derived_class_name : access-specifier base_class_name
{
// body ....
};
• class: keyword to create a new class
• derived_class_name: name of the new class, which will inherit the base class
• access-specifier: Specifies the access mode which can be either of private, public or
protected. If neither is specified, private is taken as default.
• base-class-name: name of the base class.
3
TYPES OF INHERITANCE
The inheritance can be classified on the basis of the relationship
between the derived class and the base class. In C++, we have 5
types of inheritances:
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
4
SINGLE INHERITANCE
In single inheritance, a class is allowed to inherit from only
one class. i.e. one base class is inherited by one derived
class only.
5
SYNTAX OF SINGLE HERITANCE
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
6
EXAMPLE
1. #include <iostream>
2. using namespace std;
3.
4. class Vehicle {
5. public:
Output
6. Vehicle() { cout << "This is a Vehicle\n"; }
7. };
8. This is a Vehicle
9. class Car : public Vehicle { This Vehicle is Car
10. public:
11. Car() { cout << "This Vehicle is Car\n"; }
12. };
13.
14. int main()
15. {
16. Car obj;
17. return 0;
}
7
Multilevel Inheritance
In this type of inheritance, a derived class is created from another
derived class and that derived class can be derived from a base
class or any other derived class. There can be any number of levels
8
Syntax of multilevel inheritance
class C
{
... .. ...
};
class B : public C
{
... .. ...
};
class A: public B
{
... ... ...
};
9
Example
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class fourWheeler : public Vehicle {
public:
fourWheeler() { cout << "4 Wheeler Vehicles\n"; }
};
fourWheeler
class Car : public fourWheeler {
public:
Car() { cout << "This 4 Wheeler Vehical is a Car\n"; }
};
int main()
{
Car obj;
return 0;
10
}