0% found this document useful (0 votes)
5 views

Unit3inheritanceinc 190814092425

Uploaded by

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

Unit3inheritanceinc 190814092425

Uploaded by

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

Object Oriented Programming

Unit III
Operators & Inheritance In C++

Prepared By
Mayur jagdale
Asst. Prof CI Dept.
Topics
Inheritance in C++
Types of Inheritance
Inheritance in C++

The mechanism of deriving a class from another class is


known as Inheritance.
Inheritance is the most importance concept of object
oriented programming.
It allows us to define a class in terms of another class, which
helps to create and maintain an application.
The main advantage of Inheritance is, it provides an
opportunity to reuse the code functionality and fast
implementation time.
The members of the class can be Public, Private or Protected.
Syntax:
class DerivedClass : AccessSpecifier BaseClass
The default access specifier is Private.
Inheritance helps user to create a new class (derived
class) from a existing class (base class).
Derived class inherits all the features from a Base class
including additional feature of its own.
Types of Inheritance.
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
1. Single Inheritance

In Single Inheritance, one class is derived from another


class.
It represents a form of inheritance where there is only one
base and derived class.
// C++ program to explain Single inheritance
#include <iostream.h>
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Car: public Vehicle{
};
int main()
{
Car obj; // creating object of sub class will invoke the constructor of base classes

return 0;
}
Multiple Inheritance: Multiple Inheritance is a feature
of C++ where a class can inherit from more than one
classes. i.e one sub class is inherited from more than
one base classes. Syntax
class subclass_name : access_mode base_class1, access_mode
base_class2, ....
{ //body of subclass
};
C++ program to explain multiple inheritance

#include <iostream.h> int main()


class Vehicle { {
public:
Vehicle()
// creating object of sub
{ class will
cout << "This is a Vehicle" << endl;
} // invoke the constructor
}; of base classes
class FourWheeler {
public: Car obj;
FourWheeler()
{
return 0;
cout << "This is a 4 wheeler Vehicle“; }
}
}; Output:
class Car: public Vehicle, public FourWheeler
{
This is a Vehicle
This is a 4 wheeler Vehicle
};
3.Multilevel Inheritance
In this type of inheritance, a derived class is
created from another derived class.
C++ program to implement Multilevel Inheritance
#include <iostream.h>
int main()
class Vehicle
{
{
public:
Vehicle()
Car obj;
{ return 0;
cout << "This is a Vehicle" << endl;
} }
};
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
output:
cout<<"Objects with 4 wheels are
vehicles"<<endl; This is a Vehicle
}
};
class Car: public fourWheeler{
Objects with 4 wheels
public:
car()
are vehicles
{
cout<<"Car has 4 Wheels"<<endl; Car has 4 Wheels
}
};
4 Hierarchical Inheritance:
In this type of inheritance, more than one sub
class is inherited from a single base class. i.e. more
than one derived class is created from a single base
class.
C++ program to implement Hierarchical Inheritance

#include <iostream>
int main()
class Vehicle {
{
Car obj1;
public:
Vehicle() Bus obj2;
{
cout << "This is a Vehicle" << endl;
return 0;
} }
};
class Car: public Vehicle
Output:
{ This is a Vehicle
};
This is a Vehicle
class Bus: public Vehicle
{

};
5 Hybrid (Virtual) Inheritance:
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance. Below image shows the combination of
hierarchical and multiple inheritance:
// C++ program
#include for Hybrid Inheritance
<iostream.h>
class Vehicle
{
public: int main()
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
{
};
class Fare
{
Bus obj2;
return 0;
public:
Fare()
{

}
cout<<"Fare of Vehicle\n";
}
};
class Car: public Vehicle
{ Output:
}; This is a Vehicle
class Bus: public Vehicle, public Fare
{ Fare of Vehicle
};

You might also like