0% found this document useful (0 votes)
10 views3 pages

Inheritance

Inheritance is a feature in object-oriented programming where a derived class inherits properties from a base class, allowing for the creation of new classes based on existing ones. There are several types of inheritance, including single, multiple, multilevel, hierarchical, and hybrid inheritance. Each type has its own structure and rules for how classes relate to one another.

Uploaded by

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

Inheritance

Inheritance is a feature in object-oriented programming where a derived class inherits properties from a base class, allowing for the creation of new classes based on existing ones. There are several types of inheritance, including single, multiple, multilevel, hierarchical, and hybrid inheritance. Each type has its own structure and rules for how classes relate to one another.

Uploaded by

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

INHERITANCE

The capability of a class to derive properties and characteristics from another


class is called Inheritance.
Inheritance is a feature or a process in which, new classes are created from the
existing classes. The new class created is called “derived class” or “child class”
and the existing class is known as the “base class” or “parent class”. The derived
class now is said to be inherited from the base class.

When we say derived class inherits the base class, it means, the derived class
inherits all the properties of the base class, without changing the properties of
base class and may add new features to its own. These new features in the derived
class will not affect the base class.
*Sub Class: The class that inherits properties from another class is called
Subclass or Derived Class.
*Super Class: The class whose properties are inherited by a subclass is called Base
Class or Superclass.

Types Of Inheritance:-
1.Single inheritance
2.Multilevel inheritance
3.Multiple inheritance
4.Hierarchical inheritance
5.Hybrid inheritance

1. Single Inheritance: In single inheritance, a class is allowed to inherit from


only one class. i.e. one subclass is inherited by one base class only.
__________________________________________________________________
#include<iostream>
using namespace std;

class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
class Car : public Vehicle {

};
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
_________________________________________________________________
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can
inherit from more than one class. i.e one subclass is inherited from more than one
base class.

__________________________________________________________________

#include <iostream>
using namespace std;

class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
class Car : public Vehicle, public FourWheeler {
};
int main()
{
Car obj;
return 0;
}
Output
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.
________________________________________________________________
#include <iostream>
using namespace std;

class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class fourWheeler : public Vehicle {
public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
class Car : public fourWheeler {
public:
Car() { cout << "Car has 4 Wheels\n"; }
};
int main()
{
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
________________________________________________________________
4. Hierarchical Inheritance: In this type of inheritance, more than one subclass is
inherited from a single base class. i.e. more than one derived class is created
from a single base class.
_______________________________________________________________
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class Car : public Vehicle {
};
class Bus : public Vehicle {
};
int main()
{
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
_______________________________________________________________
5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining
more than one type of inheritance
_______________________________________________________________
#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
class Car : public Vehicle {
};
class Bus : public Vehicle, public Fare {
};
int main()
{
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
_______________________________________________________________

You might also like