Inheritance

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Inheritance

Def: The mechanism of deriving a new class from an old class


is called inheritance.
Old class is called as base class, also called as super class.
New class is called as derived class, also called as sub class.
i.e. old class = base class = super class
New class = derived class = sub class
OR
Inheritance is the capability of one class to inherit properties
from another class.
Base Class: It is the class whose properties are inherited by
another class.
Ex: A class
Derived Class: It is the class that inherits properties from
base class.
Ex: B class
Ex.
A Class

B Class

A class = old class / base class / super class


B class = new class / derived class / sub class
Advantages of Inheritance
1. Reusing existing code
2. Faster development time
3. Easy to maintain
4. Easy to extend
5. Memory utilization
*Defining derived class
When we want to declare derived class by writing a colon
after the class name and the type of derivation (public or
private or protected) and the class from which it derives.

Syntax:
class derived-class-name : visibility-mode base-class-name
{
------------------------------
Members of the derived class;
-------------------------------
};

In the above syntax


*Class Keyword
*derived-class-name  Name of derived class
* : (colon) shows the derivation from the base class
*visibility mode  specifies the type of derivation
*base class  Name of the base class(old class)

Note:
1) If visibility mode is not given, then by default the visibility
mode is private.
2) All members of the class except private are inherited

Derriving classes

1) Public derived class


Class A
{
---------------
};
Class B : public A
{
Derived class members;
};

2) Private derived class


class A
{
private:
public:
};
class B : private A
{
derived class members;
};

3) Protected derived class


C lass A
{
private :
protected:
public :
};
class B: protected A
{
derived class members;
};
Visibility Mode
Visibility mode such as private, public, protected which
controls the access specifier for inheritable member of base
class in the derived class.

Base class Derived class


public mode private mode protected mode
private Not inherited Not inherited Not inherited
Public public private protected
protected protected private protected
1)Public Inheritance
 Mostly used inheritance mode
 Public members of the base class become public in the
derived class
 Private members cannot be inherited to the derived
class.
 Protected members of the base class stay protected in a
derived class
2)Private Inheritance
 Public members of the base class become private in the
derived class
 Private members cannot be inherited to the derived
class.
 Protected members of the base class become private in a
derived class

3)Protected Inheritance
 Public members of the base class become protected in
the derived class
 Private members cannot be inherited to the derived
class.
 Protected members of the base class become protected
in a derived class
Levels/Types of Inheritance

1)Single Inheritance
If a class is derived from a single base class, it is called as
single inheritance.

2)Multilevel Inheritance
The classes can be also derived from the classes that are
already derived is called as multilevel inheritance.

3)Multiple Inheritance
If a class is derived from more than one base class is
called as multiple inheritance.
4)Hierarchical Inheritance
If a number of classes are derived from a single base
class is called as hierarchical inheritance.

5)Hybrid Inheritance
A combination of one or more types of inheritance is
called as hybrid inheritance.
OR
It is a combination of Hierarchical and multilevel
inheritance.

Hierarchical Inheritance

Multilevel Inheritance

*Virtual base classes


When two or more objects are derived from single base
class, which generate multiple copies of base classes and
generate ambiguity. So, to avoid this declare base classes as
virtual when it is being inherited. Such base classes are called
as virtual base class. This can be done by preceding the base
class name with the word virtual.
Ex.
class A
{
------------
-------------
};
class B : virtual public A
{
------------
-----------
};
class C : virtual public A
{
------------
-----------
};
class D : public B, public C
{
-----------
----------
};

Abstract Class
An abstract class is one that is not used to create an
object. An abstract class is designed only to act as a base
class(to be inherited by other classes).
Constructor in derived classes
When constructor is used in the inheritance ie both in the
base class and derived class, then base constructor is
executed first and then the constructor in the derived class is
executed.
Ex: use of constructor in single level inheritance.

class base
{
public: base() // base class constructor
{
}
};

class derived : public base


{
public: derived() // derived class constructor.
{
}
};
Destructor in the derived class
If the constructors are called down the line from the base to
the derived class, then destructors are called just in the
reverse order, i.e from the derived class up to the base class.
//JP-WAP to demonstrate single level inheritance.
#include<iostream.h>
#include<conio.h>
class base
{
int rollno;
char name[20];
public: void read()
{
cout<<"enter the rollno and name";
cin>>rollno>>name;
}
void display()
{
cout<<"Rollno = "<<rollno<<endl;
cout<<"Name="<<name<<endl;
}
};
class derived : public base
{
int m1,m2,total;
public : void read1()
{
cout<<"enter the two subject marks";
cin>>m1>>m2;
total = m1+m2;
}
void display1()
{
cout<<"subject 1 marks ="<<m1<<endl;
cout<<"subject2 marks = "<<m2<<endl;
cout<<"total marks ="<<total<<endl;
}
};
void main()
{
derived d; //Object is created using derived class
clrscr();
d.read(); // Member functions of base class

d.display();
d.read1();
//Member functions of derived class
d.display1();
getch();
}

You might also like