Вы находитесь на странице: 1из 28

Session Objectives

Discuss Inheritance
Types of Inheritance Example for 5 types of Inheritance Member function Overriding

It is the Process of creating a New class from an existing class. The Existing class is called Base or Parent class. The New class is called as Child or Derived Class

Advantages
It permits code reusability. So, save time and
increase the program reliability. A programmer can use a class created by another person or company without modifying it derive other classes from it that are suited to particular situations Improve Program Reliability It Permits code sharing

Advantages
The base class need not be changed but can be
adapted 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

Class <derived classname> : public <Base classname> { -----};

Inheritance is the property that allows the reuse of an existing class to build a new class
Base Class Methods and Properties Derived class Base class methods + Additional methods

Inheritance

Single Inheritance Multilevel Inheritance

Hierarchical Inheritance
Multiple Inheritance

Single Inheritance A class can be derived from a single base class is called single inheritance Base Class
Multiple Inheritance
Class A

Class B

sub Class
Class C

Multilevel Inheritance

Class A
Class B
Hierarchical Inheritance

Class C
sub Class 3

Base Class

sub Class 1

sub Class 2

Hybrid Inheritance
Hybrid is nothing but the combination of Multilevel and multiple Inheritance
Lecturer
Multi level Inheritance

Marks

Department

Multiple Inheritance

Student

Base Class Visibility

Derived class Visibility


Public derivation

Private Derivation

Private Public
Protected

Not inherited protected


public

Not Inherited private


private

//Example for Single inheritance


#include<iostream.h> class 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() { cout<<"Enter the fees amount for "; cin>>dos>>msoffice>>foxpro; } void pcp::pcp_listfees() { cout<<"Dos: "<<dos<<endl; cout<<"Msoffice: "<<msoffice<<endl; cout<<"Foxpro: "<<foxpro<<endl; }

void hdca::hdca_getfees() { cout<<"Enter the fees amount :"; cin>>unix>>c>>cpp; } void hdca::hdca_listfees() { cout<<"Unix: "<<unix<<endl; cout<<"C: "<<c<<endl; cout<<"C++: "<<cpp<<endl; } void main() { clrscr(); hdca h; cout<<endl<<"Fees detail for PCP"<<endl; h.pcp_getfees(); cout<<endl<<"Fees detail for HDCA"<<endl; h.hdca_getfees(); cout<<endl<<"Fees list for PCP"<<endl; h.pcp_listfees(); cout<<endl<<"Fees list for HDCA"<<endl; h.hdca_listfees(); getch(); }

It is the process of creating new class from more than one base classes.

Syntax :
class <derived class >:<access specifier> base_class1,<access specifier> base_class2... { private : // members; protected : // members; public : //memebers; };

#include<iostream.h> class base1 { protected : int var1; public : void disp_base1() { cout<<"var1 "<<var1<<endl; }}; class base2 { protected : int var2; public : void disp_base2() { cout<<"var2 is"<<var2<<endl; }};

class deri:public base1,public base2 { private: int var3; public : deri(int a,int b,int c) { var1=a; var2=b; var3=c; } void disp_me() { cout<<"var3 is"<<var3; } }; Void main() {

void main() { deri d(10,20,30); clrscr(); d.disp_base1(); d.disp_base2(); d.disp_me(); getch(); }

AMBIGUITY IN MULTIPLE INHERITANCE


#include<iostream.h> class base1 { public: int i; void disp() { cout<<"base1 value"<<i<<"\n"; } }; class base2 { public: int i; void disp() { cout<<"base2 value"<<i; } };

class deri:public base1,public base2 { };


void main()

{ deri d; d.i=10; d.disp(); clrscr(); /*deri d; d.base1::i=10; d.base2::i=20; d.base1::disp(); d.base2::disp(); getch();*/ }

RESULT : AMBIGUITY ERROR OCCURRED

//Example for MultiLevel inheritance


#include<iostream.h> #include<conio.h> class publications { private : int book_no; public : void getdata() { cout<<"\n"<<"Enter Book Number:"; cin>>book_no; } void putdata() { cout<<book_no<<endl; } }; class author: public publications { private : char author_name[20]; public : void getdata() { publications::getdata(); cout<<"Enter author name"; cin>>author_name; }

class deri:public base1,public base2 { };

void main() { /*deri d; d.i=10; d.disp();*/ clrscr(); deri d; d.base1::i=10; d.base2::i=20; d.base1::disp(); d.base2::disp(); getch(); }

Inheritance
Class Employee Common to Name Director Manager Secretary Clerk

Age
Employee id Salary Department

Inheritance
Employee

Director

Manager

Secretary

Clerk

Each of the sub-classes is considered to be derived from the parent class Employee

Example for Hierarchical inheritance


#include<iostream.h> class employee { private : int empno; char empname[20]; public : void getdata(){ cout<<"\n Enter Employee No"; cin>>empno; cout<<"\n Enter Employee name "; cin>>empname;} void putdata(){ cout<<"\n Employee No"<<empno; cout<<"\n Employee name "<<empname;} }; class manager :public employee { private : float basic; public : float salary; float da; float hra; float cca; float pf; float special_allowances;

void getdata() { employee::getdata(); cout<<"Enter Basic Salary \n"; cin>>basic; } } 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_allowance s-pf; employee::putdata(); cout<<endl<<salary; }}; class supervisor : public employee { private : float basic; public : float salary; float da; float hra; float cca; float pf; float special_allowances;

void getdata() { employee::getdata(); cout<<"Enter Basic Salary \n"; cin>>basic; } 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_allowa nces-pf; employee::putdata(); cout<<endl<<salary; } }; class worker :public employee { public : float salary; float wages; float incentive; void getdata() { employee::getdata(); cout<<"\n Enter wages & incentives"; cin>>wages>>incentive; }

void putdata() { salary=wages+incentive; employee::putdata(); cout<<endl<<salary; } }; void main() { manager m; supervisor s; worker w; clrscr(); m.getdata(); s.getdata(); w.getdata(); w.putdata(); s.putdata(); m.putdata(); getch(); }

HYBRID INHERITANCE
#include<iostream.h> class lecturer { private : char lecturer_name[20]; public : void getdata() { cout<<"Enter Lecturer name"; cin>>lecturer_name; } void putdata() { cout<<"lecturer name is "<<lecturer_name; } }; class department : public lecturer { private : char department_name[20]; public: void getdata() { cout<<"Enter Department name"; cin>>department_name; }

void putdata() { cout<<"Department name is "<<department_name; } }; class marks { public : int mark1; int mark2; int mark3; void getdata() { cout<<"Enter the marks for 3 subjects"; cin>>mark1>>mark2>>mark3; } void putdata() { cout<<"The marks for 3 subjects are "<<mark1<<"\t"<<mark2<<"\t"<<mark3; } }; class student:public department,public marks { private: int roll_no;

char student_name[20]; public: void getdata() { department::getdata(); cout<<"Enter the student name"; cin>>student_name; cout<<"Enter the student Enrollment no"; cin>>roll_no; marks::getdata(); } void putdata() { department::putdata(); cout<<"The name & rollno is "<<student_name<<"\t"<<roll_no; marks::putdata(); } }; void main() { student s; clrscr(); s.getdata(); s.putdata(); getch(); }

#include<iostream.h> class baseA { public: void show() { cout<<"base class show function; } }; class deriA:public baseA { public: void show() { cout<<"derive class show function; }};

void main() { baseA b; deriA d; clrscr(); b.show(); d.show(); getch(); }

The class from which members are derived are called as base class or parent class and the class which derives data from another class is called as the derived class or child class A derived class is always as big as the base class, since the object of the derived class inherits all the characteristics of the base class and can add properties of its own. Depending upon the access specifiers inheritance is classified as private, protected and public inheritance

The private members inherited from the base class are

inaccessible to the member functions of the derived class in all


types of inheitance A derived class with only one base class is called as Single Inheritance A derived class with one or more than one base class is called

as multiple inheritance
A base class with more than one derived class is referred to as the Hierarchical inheritance The combination of multilevel and multiple inheritance is called as Hybrid Inheritance

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?

Вам также может понравиться