Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Concept of Inheritance . Types of Inheritance .

Similar presentations


Presentation on theme: "Inheritance Concept of Inheritance . Types of Inheritance ."— Presentation transcript:

1 Inheritance Concept of Inheritance . Types of Inheritance .

2 Inheritance Inheritance is a method by which new classes are created or derived from the existing classes. Using Inheritance some qualities of the base classes are added to the newly derived class, apart from its own features The advantage of using "Inheritance" is due to the reusability of classes in multiple derived classes. The ":" operator is used for inheriting a class.

3 Derived Class Visibility
The following table lists the visibility of the base class members in the derived classes Derived Class Visibility Base Class Visibility Public derivation Private derivation Protected derivation Private Not inherited Protected Public

4 Following are the different types of inheritance followed in C++.
Single Inheritance Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance

5 Inheritance: Introduction
The inheritance allows subclasses to inherit all properties (variables and methods) of their parent classes. The different forms of inheritance are: Single inheritance (only one super class) Multiple inheritance (several super classes) Hierarchical inheritance (one super class, many sub classes) Multi-Level inheritance (derived from a derived class) Hybrid inheritance (more than two types)

6 Forms of Inheritance A A B A B C B C D (a) Single Inheritance
(b)Multiple Inheritance (c(Hierarchical Inherit. A A c B B C D (d) Multi-Level Inheritance ( e) Hybrid Inheritance

7 Example: #include <iostream.h> class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Square: public Value int square() { return (val*val); } int main () { Square sq; sq.set_values (5); cout << "The square of 5 is::" << sq.square() << endl; return 0; } Result: The square of 5 is:: 25 In the above example the object "val" of class "Value" is inherited in the derived class "Square".

8 Explanation Single Inheritance is method in which a derived class has only one base class.

9 Example: # include <iostream.h> class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Cube: public Value int cube() { return (val*val*val); } int main () { Cube cub; cub.set_values (5); cout << "The Cube of 5 is::" << cub.cube() << endl; return 0; } Result: The Cube of 5 is:: 125 In the above example the derived class "Cube" has only one base class "Value". This is the single inheritance OOP's concept.

10 Multiple Inheritance Explanation Multiple Inheritance is a method by which a class is derived from more than one base class.

11 Example: Result: The area of the square is:: 25 int main () { Area r;
#include <iostream.h> using namespace std; class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow { void show(int i); }; void CShow::show (int i) { cout << "The area of the square is::" << i << endl; } class Area: public Square, public CShow int area() { return (l *l); } int main () { Area r; r.set_values (5); r.show(r.area()); return 0; } Result: The area of the square is:: 25

12 In the above example the derived class "Area" is derived from two base classes "Square" and "CShow". This is the multiple inheritance OOP's concept in C++.

13 Hierarchical Inheritance
Explanation Hierarchical Inheritance is a method of inheritance where one or more derived classes is derived from common base class.

14 cout << "The square value is::" << s.sq() << endl;
Example: #include <iostream.h> class Side { protected: int l; public: void set_values (int x) { l=x;} }; class Square: public Side { int sq() { return (l *l); } class Cube: public Side public: int cub() { return (l*l*l); } int main () { Square s; s.set_values (10); cout << "The square value is::" << s.sq() << endl; Cube c; c.set_values (20); cout << "The cube value is::" << c.cub() << endl; return 0; } Result: The square value is:: 100 The cube value is::8000

15 In the above example the two derived classes "Square", "Cube" uses
a single base class "Side". Thus two classes are inherited from a single class. This is the hierarchical inheritance OOP's concept in C++.


Download ppt "Inheritance Concept of Inheritance . Types of Inheritance ."

Similar presentations


Ads by Google