Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance:Concept of Re-usability

Similar presentations


Presentation on theme: "Inheritance:Concept of Re-usability"— Presentation transcript:

1 Inheritance:Concept of Re-usability
Chapter 4 Inheritance:Concept of Re-usability

2 What is Inheritance Inheritance is the capability of one class to acquire properties and characteristics from another class. The mechanism of deriving a new class from an old one is called inheritance (or derivation) Old class whose properties are inherited by other class is called the Parent or Base or Super class. New class class who inherits property of other class is called the Parent or Base or Super class.

3 The derived class can inherits some or all traits from the base class.
A class can also inherit properties from more than one class. Types of inheritance Single inheritance Multiple inheritance Multilevel inheritance Hierarchical inheritance Hybrid inheritance

4 Single Inheritance: In this type of inheritance one derived class inherits from only one base class. It is the most simplest form of Inheritance.

5 Multiple Inheritance In this type of inheritance a single derived class may inherited from two or more than two base classes. Base class Derived Class

6 Multilevel Inheritance
In the type of inheritance in which derived class get derived from another derived class. Base class Base for C and derive Class for A derive class of B

7 Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base class. Base class derive classes

8 Hybrid (Virtual) Inheritance:
Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

9 Defining Derived Classes
Syntax: Class derived_class name : visibility-mode base_class name { member of derived class; member function of derived class; }; Example: class ABC : Public XYZ int a,b; void getdata ( ); }

10 The colon indicates that the derived_class name is derived from base_class name
The visibility mode is optional if it is present my be either private or public. The default visibility mode is private. Visibility mode specifies that whether the features of the base class are privately derived or publicly derived.

11 Base class can be inherited in following ways:
Privately inheritance Publicly inheritance No private data members can be inherited in any of the above method. Only public data members of base class can be inherited.

12 When base class is privately inherited by derived class all data members and member function (public/Private) are become private data of derived class. As we have seen that all member function should declared as public because we are going to access it in main() function using object of class In this case we are deriving it privately automatically all member function get private which are not accessible by object of derived class.

13 When the base class is publicly inherited the 'public members' of the base class become 'public members' of the derived class. They are accessible to the objects of the derived class.

14 Derived class Base class

15 If the class inherited as Public

16 The program shows that the object of class D have access to all public members of class B.s a is a private in class B & can’t be inherited by D Although the data member ‘a’ is private & can’t be inherited, object of D are able to access it by an inherited functions of class B

17 The program shows that the object of class D have access to all public members of class B.s a is a private in class B & can’t be inherited by D Although the data member ‘a’ is private & can’t be inherited, object of D are able to access it by an inherited functions of class B

18 If the class is inherited Privately

19 In private derivation, the public memebers of the base class become private memebers of the derived class. The object of D can’t access to the public member of B Such as d.get_ab(); d.get_a(); d.show_a(); We can access this private member function by calling them into a public member function of a class

20 Accessing private function by calling them into public functions of class

21 We can access private data members of base class in derived class by making them as public.
Another way is that c++ provides third visibility modifier PROTECTED A member declared as protected is accessible by member of the same class and any immediately derived class

22 Virtual Base class

23

24


Download ppt "Inheritance:Concept of Re-usability"

Similar presentations


Ads by Google