31

Inheritance

Embed Size (px)

Citation preview

Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

FASIL KM

[email protected]

www.facebook.com/Fasil faas

twitter.com/fasilemv

in.linkedin.com/in/fasilemv

9544334000

INHERITANCE

CONTENTS• CLASS

• OBJECT

• INHERITANCE

SINGLE INHERITANCE

MULTIPLE INHERITANCE

HIERARCHICAL INHERITANCE

MULTILEVEL INHERITANCE

HYBRID INHERITANS(Virtual

Inheritance)

CLASS• Class is the base design of objects

means expanded concept of a data,that can hold both data and functions.

• No memory is allocated when class iscreated.

• It is a user defined data type.Example : A car is consider

as a class Methods : Engine, wheels,

steering.

Properties : company,

model, colour,

speed, etc….

OBJECT

• Object is the instance of class, meansidentifiable entity with some

characteristics and behaviour. • Memory allocated when only an

object is created.

INHERITANCE

Deriving new class from existing class. Object of one class contains the

property of another class. Reusability. We can add features to an

existing class without modifying it.

TYPES OF INHERITANCE

• Single Inheritance • Hierarchical Inheritance • Multi Level Inheritance • Hybrid Inheritance • Multiple Inheritance

SINGLE INHERITANCE

• One derived class inheritsfrom only one base class• Most simplest form of Inheritance.

SINGLE INHERITANCE

A

B

EXAMPLE

#include<iostream>using namespace std;class father{public:

string name;int age,pincode;

};class child:public father{public:

string school;int standard;

};main(){

father obj1;

child obj2;cout<<"enter the name of father:";cin>>obj1.name;cout<<"enter the age of father:";cin>>obj1.age;cout<<"enter the pincode of father:";cin>>obj1.pincode;cout<<"-------------DETAIL OF CHILD--------------\n";cout<<"enter the name of child:";cin>>obj2.name;cout<<"enter the age of child:";cin>>obj2.age;cout<<"enter the pincode of child:";cin>>obj2.pincode;cout<<"enter the school name of child:";cin>>obj2.school;cout<<"enter the standard of child:";cin>>obj2.standard;}

HEIRARCHICAL INHERITANCE

• More than one derivedclasses is derived from common base class.

HIERARCHICAL INHERITANCE

A

C DB

EXAMPLE#include<iostream>using namespace std;class user{public:

string name,place;int age;

};class student:public user{public:

int rollno,classs,mark1,mark2;};class teacher:public user{public:

string department;int teacher_id,salary;

};main(){

user obj;student obj1;teacher obj2;cout<<"-----------------STUDENT-------------\n";cout<<"enter the name:";

cin>>obj1.name;cout<<"enter the age:";cin>>obj1.age;cout<<"enter the place:";cin>>obj1.place;cout<<"enter the rollno:";cin>>obj1.rollno;cout<<"enter the class:";cin>>obj1.classs;cout<<"enter the mark1:";cin>>obj1.mark1;cout<<"enter the mark2:";cin>>obj1.mark2;cout<<"---------------TEACHER---------------\n";cout<<"enter the name:";cin>>obj2.name;cout<<"enter the age:";cin>>obj2.age;cout<<"enter the place:";cin>>obj2.place;cout<<"enter the id:";cin>>obj2.teacher_id;cout<<"enter the salary:";cin>>obj2.salary;cout<<"enter the department:";cin>>obj2.department;}

MULTI LEVEL INHERITANCE

• Derived class is derived from another derived class.

MULTILEVEL INHERITANCE

A

B

C

EXAMPLE#include<iostream>using namespace std;class grandparent{public:

void gshow(){cout<<"intelligent";}

};class parent:public grandparent{public:

void pshow(){

cout<<"\nhandsom";}

};class child:public parent{

public:void cshow(){

cout<<"\nobedience";}

};main(){

grandparent obj1;parent obj2;child obj3;cout<<"QUALITY OF GRANDPA\n";

obj1.gshow();cout<<"\nQUALITIES OF FATHER\n";obj2.gshow();

obj2.pshow();cout<<"\nQUALITIES OF CHILD\n";obj3.gshow();obj3.pshow();obj3.cshow();

}

HYBRID INHERITANCE

• Combination of single Inheritance , hierarchical inheritance and multi level inheritance.

HYBRID INHERITANCE

A

DB

C

EXAMPLE#include<iostream>using namespace std;class grandparent{public:void gshow()

{cout<<"\nintelligent";}

};class parent:public grandparent{public:

void pshow(){

cout<<"\neducated";}

};class son:public parent{public:

void sshow(){

cout<<"\nobedience";}

};

class daugter:public parent{public:

void dshow(){

cout<<"\nbeautiful";}

};main(){

grandparent gobj;parent pobj;son sobj;daugter dobj;cout<<"QUALITY OF GRANDPA";gobj.gshow();cout<<"\nQUALITY OF PARENT";pobj.gshow();pobj.pshow();cout<<"\nQUALITIES OF SON";sobj.sshow();sobj.pshow();sobj.gshow();cout<<"\nQUALITIES OF DAUGHTER";dobj.dshow();dobj.pshow();dobj.gshow();

}

MULTIPLE INHERITANCE

• Derived class is derived from more than one base class.

MULTIPLE INHERITANCE

A B

C

EXAMPLE#include<iostream>using namespace std;class father{public:

void fshow(){cout<<"discipline";}

};class mother{public:

void mshow(){

cout<<"\nopen minded";}

};class child:public father,public mother{

public:void cshow(){

cout<<"\nintelligent";}

};main(){

father obj1;mother obj2;child obj3;cout<<"QUALITY OF FATHER\n";obj1.fshow();cout<<"\n";cout<<"\nQUALITY OF MOTHER";obj2.mshow();cout<<"\n";cout<<"\nQUALITIES OF CHILD\n";obj3.fshow();obj3.mshow();obj3.cshow();

}

CONCLUSION

We can REUSE the methods and data of the existing class

We can EXTEND the existing class by adding new data and new methods

We can MODIFY the existing class by overloading its methods with your own implementations

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.

Email: [email protected]

Contact Us