Inheritance along with its types and examples of each type
Size: 154.46 KB
Language: en
Added: Aug 14, 2019
Slides: 16 pages
Slide Content
Object Oriented Programming Unit III Operators & Inheritance In C ++ Prepared By Prof. Vishal S Patil Asst. Prof AEC/CSE 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 > class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl ; } }; class FourWheeler { public: FourWheeler () { cout << "This is a 4 wheeler Vehicle“; } }; class Car: public Vehicle, public FourWheeler { }; int main() { // creating object of sub class will // invoke the constructor of base classes 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 .
C ++ program to implement Multilevel Inheritance # include < iostream.h > class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl ; } }; class fourWheeler : public Vehicle { public: fourWheeler () { cout <<"Objects with 4 wheels are vehicles"<< endl ; } }; class Car: public fourWheeler { public: car() { cout <<"Car has 4 Wheels"<< endl ; } }; 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 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 > class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl ; } }; 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 . For example: Combining Hierarchical inheritance and Multiple Inheritance. Below image shows the combination of hierarchical and multiple inheritance:
// C++ program for Hybrid Inheritance #include < iostream.h > class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl ; } }; 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