Types of Inheritance
Types of Inheritance
Single Inheritance
When a subclass inherits only from one base, it is known as Single Inheritance.
2. Multiple Inheritance
When a subclass inherits from multiple base classes, it is known as Multiple Inheritance.
Derived Class - Y
3. Hierarchical inheritance
When many subclasses inherit from a single base class, it is known as Hierarchical
inheritance.
BASE CLASS - A
4. Multilevel Inheritance
When a subclass inherits from a class that itself inherits from another class, it is known
as Multilevel Inheritance. i.e. A derived class becomes base class of another class.
BASE CLASS - A
Derived Class - Z
5. Hybrid Inheritance
Eg., When a sub class inherit from multiple base classes and all of its base classes
inherits from a single base class, This form of Inheritance is known as Hybrid
Inheritance.
BASE CLASS - H
Derived Class - Y
1. Single inheritance
When a subclass inherits only from one base, it is known as Single Inheritance.
Syntax:
…….
};
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Employee
private:
char name[LEN];
public:
void getdata()
cin>>enumber;
void putdata()
cout<<"Name:"<<name<<"\n";
cout<<"Employee Number:"<<enumber<<"\n";
cout<<"Basic Salary:"<<basic<<"\n";
protected:
float basic;
void getbasic()
cin>>basic;
};
private:
char title[LEN];
public:
void getdata()
{
Employee::getdata();
getbasic();
cout<<"Enter Title:";
void putdata()
Employee::putdata();
cout<<"Title:"<<title<<"\n";
};
void main()
clrscr();
Manager m1,m2;
cout<<"Manager1\n"; m1.getdata();
cout<<"Manager2\n"; m2.getdata();
getch();
}
Function Overriding:
When the base class and derived class have member functions with exactly the same
name, same return-type, and same arguments list, then it is said to be function
overriding.
If you create an object of the derived class and call the member function which exists in
both classes (base and derived), the member function of the derived class is invoked and
the function of the base class is ignored.
2) In function overloading function signature should be different for all the overloaded
functions. In function overriding the signature of both the functions (overriding function
and overridden function) should be same.
3) Overloading happens at the compile time that’s why it is also known as compile time
polymorphism while overriding happens at run time which is why it is known as run
time polymorphism.
When a subclass inherits from multiple base classes, it is known as Multiple Inheritance.
Example code:
#include<iostream.h>
#include<conio.h>
class chairperson
long CID;
char CName[20];
protected:
char Description[40];
void allocate();
public:
chairperson();
void assign();
void show();
};
Class Director
int DID;
char DName[20];
Protected:
char profile[30];
public:
Director();
void input();
void output();
};
int CID;
public:
company();
void Enter();
void Display();
};
void main()
clrscr();
comp1.Enter();
comp1.Display();
comp2.Enter();
comp2.Display();
}
Consider, the following Scenarios of Inheritance,
(i)
BASE CLASS - A
Derived Class - Y
(ii)
Base CLASS – A Base CLASS – B (With
(With same same member in Class A)
member in Class B)
Base CLASS - C
In Scenario1:
However, Y Inherits both B and C. This means that there will be two copies of Base class A in Derived
class Y.
Then when you are accessing the member of Base class A using the object of Derived class Y, the
Statement becomes ambiguous.
Ex: obj.B1::a=25;
2. When 2 or more classes are derived from common base class, you can prevent multiple copies of the
base class from being present in an object of the derived class by declaring the base class as virtual when
it is inherited.
This is accomplished by putting the keyword virtual before the base class’ name when it is inherited.
Syntax:
Class base
};
};
};
};
4. Multilevel Inheritance
When a subclass inherits from a class that itself inherits from another class, it is known
as Multilevel Inheritance. i.e. A derived class becomes base class of another class.
Example:
5. Hybrid Inheritance
Eg., When a sub class inherit from multiple base classes and all of its base classes
inherits from a single base class, This form of Inheritance is known as Hybrid
Inheritance.
Nesting of Classes:
One class may have another class defined within it known as nested class.
One other way of doing so is that a class contains objects of other classes as its members.
Example code:
class X { };
class Y { };
class Z
X obj1;
Y obj2;
};
All the objects of class Z will be containing objects obj1 and obj2 of classes X and Y types respectively.
Definition:
When a class contains objects of other class types as its member, it is referred to as Containment or
Composition or Aggregation or Containership or Nesting.
You can also say that Class Z has Has-A Relationship with class X and class Y objects.
When a class inherits from another class, is known as IS-A relationship between the classes.
When a class contains objects of other class types as its member, the relationship between the classes is
referred HAS-A Relationship.
A similar relation between two classes is HOLDS-A relation. It is similar to HAS-A relationship but
ownership is missing in HOLDS-A relationship. A class that indirectly containds another object i.e, via
pointer or reference, is said to have HOLDS-A relationship with class whose object is its indirect
member,
Example code:
class Fax
};
class modem
Fax *connect;
long bps;
char type;
public:
bps=sp;
type=c;
};
In the example code, modem object has HOLDS-A relationship with a Fax object.