Download - C++Day-10
-
7/31/2019 C++Day-10
1/28
-
7/31/2019 C++Day-10
2/28
Discuss Inheritance
Types of Inheritance
Example for 5 types of Inheritance
Member function Overriding
-
7/31/2019 C++Day-10
3/28
It is the Process of creating a Newclass from an existing class.
The Existing class is called Base orParent class.
The New class is called as Child orDerived Class
-
7/31/2019 C++Day-10
4/28
Advantages
It permits code reusability. So, save time andincrease the program reliability.
A programmer can use a class created by anotherperson or company without modifying it derive other
classes from it that are suited to particular situations
Improve Program Reliability
It Permits code sharing
-
7/31/2019 C++Day-10
5/28
Advantages
The base class need not be changed but can beadapted to suit the requirements in different
applications.
Saves developers time and effort so that they need
not to spend time to know the core technical facts
-
7/31/2019 C++Day-10
6/28
Class : public
{
-------};
-
7/31/2019 C++Day-10
7/28
Base Class
MethodsandProperties
Derived class
Base class methods+
Additional methods
Inheritance is the property that allows the
reuse of an existing class to build a new class
-
7/31/2019 C++Day-10
8/28
Inheritance
MultilevelInheritance
Single Inheritance
Multiple Inheritance
HierarchicalInheritance
-
7/31/2019 C++Day-10
9/28
Single InheritanceA class can be derived from a
single base class is called single inheritance
Base Class
sub Class
Multiple Inheritance
Class A
Class C
-
7/31/2019 C++Day-10
10/28
Class A
Class B
Class C
Multilevel Inheritance
Hierarchical Inheritance
Base Class
sub Class 1sub Class 3 sub Class 2
-
7/31/2019 C++Day-10
11/28
Hybrid Inheritance
Hybrid is nothing but the combination of Multilevel and multiple Inheritance
Lecturer
Marks Department
Student
Multi levelInheritance
MultipleInheritance
-
7/31/2019 C++Day-10
12/28
Base ClassVisibility
Derived class Visibility
Private Not inherited Not Inherited
Public protected private
Protected public private
Public derivation Private Derivation
-
7/31/2019 C++Day-10
13/28
//Example for Single inheritance#includeclass pcp{private:
float dos,msoffice,foxpro;public:void pcp_getfees();void pcp_listfees();
};class hdca:public pcp{private:
float unix,c,cpp;public:
void hdca_getfees();void hdca_listfees();
};void pcp::pcp_getfees(){
coutdos>>msoffice>>foxpro;}
void pcp::pcp_listfees(){cout
-
7/31/2019 C++Day-10
14/28
It is the process of creating new class from more than one baseclasses.Syntax :class :
base_class1, base_class2...{private :
// members;protected :
// members;public ://memebers;
};
-
7/31/2019 C++Day-10
15/28
#includeclass base1{protected :
int var1;public :
void disp_base1() {cout
-
7/31/2019 C++Day-10
16/28
void main(){deri d(10,20,30);clrscr();d.disp_base1();d.disp_base2();d.disp_me();getch();
}
-
7/31/2019 C++Day-10
17/28
#include
class base1{public:
int i;void disp(){cout
-
7/31/2019 C++Day-10
18/28
//Example for MultiLevel inheritance#include#includeclass publications{
private :int book_no;public :void getdata(){cout
-
7/31/2019 C++Day-10
19/28
Inheritance
Class Employee
Name
Age
Employee id
Salary
Department
Director
Manager
Secretary
Clerk
Common to
-
7/31/2019 C++Day-10
20/28
Inheritance
Employee
Director Manager Secretary Clerk
Each of the sub-classes is considered to be derived from theparent class Employee
-
7/31/2019 C++Day-10
21/28
#includeclass employee{
private :int empno;char empname[20];public :void getdata(){coutempno;coutempname;}void putdata(){cout
-
7/31/2019 C++Day-10
22/28
void getdata(){employee::getdata();coutbasic;}void putdata(){da=0.30*basic;hra=0.15*basic;cca=0.05*basic;pf=0.12*basic;special_allowances=0.4*basic;
salary=basic+da+hra+cca+special_allowances-pf;employee::putdata();cout
-
7/31/2019 C++Day-10
23/28
#includeclass lecturer{private :char lecturer_name[20];public :void getdata(){coutlecturer_name;}
void putdata(){cout
-
7/31/2019 C++Day-10
24/28
-
7/31/2019 C++Day-10
25/28
#includeclass baseA{public:void show(){
cout
-
7/31/2019 C++Day-10
26/28
The class from which members are derived are called as baseclass or parent class and the class which derives data fromanother class is called as the derived class or child class
A derived class is always as big as the base class, since theobject of the derived class inherits all the characteristics of thebase class and can add properties of its own.
Depending upon the access specifiers inheritance is classifiedas private, protected and public inheritance
-
7/31/2019 C++Day-10
27/28
-
7/31/2019 C++Day-10
28/28
EXERCISES
1. Define Inheritance?
2. Explain with an example the specification of a derived class?
3. Explain how inheritance are classified?
4. Explain Single Inheritance with an Example?
5. Explain Multiple inheritance with an Example?
6. Explain Hierarchical Inheritance with an Example?