You are on page 1of 17

Inheritance

Introduction
The mechanism of deriving a new class from an old one is called inheritance (or derivation) (Balagurusamy) The old class called base class and the new class called derived class or subclass Often referred to as an is-a relationship because every object of the class being defined is also an object of the inherited class

Types of Inheritance

Single Inheritance

Multiple Inheritance

Hierarchical Inheritance

Multilevel Inheritance

Hybrid Inheritance

Example
Ba e la S e S ape De e la e G a a eS e U e a a eS le a le Re a le Ca L a H me mp eme L a M a eL a Fa l Mem e S a Mem e C e k A Sa A C e

L a

Empl ee A

Derived lass
class derived-class-name : visibility-mode base-class-name { //Members of derived class ... };

Example:
class ABC :private XYZ //private derivation { //members of ABC }; class ABC :public XYZ //public derivation { //members of ABC }; class ABC : XYZ //private derivation by default { //members of ABC };

Derived lass (Contd..)


When a base class is privately inherited by a derived class, public members of the base class become private members of the derived class re public member of the base class accessible to the objects of the derived class ? When a base class is publicly inherited by a derived class, public members of the base class become public members of the derived class re private members inherited? ome of the base class data elements and member functions are inherited into the derived class. We can add our own data and member functions and thus extend the functionality of the base class.

Single Inheritance
class base //base class { public: int id; void get_id() { cout<<"\nEnter your id: "; cin>>id; } void display_id() { cout<<"I'm from base class and my id = "<<id<<"\n"; } };

class derived : public base //derived class { public: float age; void get_age() { cout<<"\nEnter your age: "; cin>>age; } void display_age() { cout<<"I'm from derived class and my id = "<<id<<"\n"; cout<<"I'm from derived class and my age = "<<age<<"\n"; } }; main() { derived d; d.get_id(); d.get_age(); //cout<<"\nI am from main and my id is: "<<d.id<<"\n"; d.display_id(); d.display_age(); }

What if you ma e id private? What if you inherit the base class privately? Or ow can you inherit base class privately? What if base class and derived class define a function of the same name ?
The base class function will be called only if the derived class does not redefine the function

Ma ing a Private Member Inheritable


C++ provides a visibility modifier protected A member declared as protected is accessible by the member functions within its class and any class immediately derived from it It can not be accessed from the functions outside these two classes When a protected member is inherited in public mode, it becomes protected in derived class too When a protected member is inherited in private mode, it becomes private in derived class too When a base class is inherited in protected mode, both public and protected members of base class becomes protected member in the derived class

Multilevel inheritance
The chain ABC is nown as inheritance path Code Example

Multiple Inheritance
Code Example

Hierarchical Inheritance

Hybrid Inheritance

Virtual Base Class

Here randparent is sometimes referred to as indirect base class Code Example

Code Example

WAP for following scenario

You might also like