240 likes | 253 Views
Chapter 4. Inheritance:Concept of Re-usability. 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)
E N D
Chapter 4 Inheritance:Concept of Re-usability
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.
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
Single Inheritance: In this type of inheritance one derived classinherits from only one base class. It is the most simplest form of Inheritance.
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
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
Hierarchical Inheritance In this type of inheritance, multiple derived classes inherits from a single base class. Base class derive classes
Hybrid (Virtual) Inheritance: Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.
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 ( ); }
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.
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.
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.
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.
Derived class Base class
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
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
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
Accessing private function by calling them into public functions of class
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