inheritance_OOPC_datastream
inheritance_OOPC_datastream
MADE BY:
• SANSKRITI GUPTA-10378
• VEDIKA PONDE-10354
• YASH HADKE-10318
• MAYURESH SARODE-10360
• SHRAVANI THOOL-10370
• JAINAM DUDHEDIYA-10377
WHAT IS AN INHERTANCE?
The derived classes have all the features of the base class
and the programmer can choose to add new features
specific to the newly created derived class.
Dog is a mammal. It has all features Cat is a mammal. It has all features
of mammals in addition to its own of mammals in addition to its own
unique features unique features
FEATURES /ADVANTAGES OF
INHERITANCE
Reusability of Code
Saves Time and Effort
Faster development, easier maintenance
and easy to extend
Capable of expressing the inheritance
relationship and its transitive nature which
ensures closeness with real world
problems .
SYNTAX
….
…..
};
ACCESS CONTROL AND
INHERITENCE
A derived class can access all the protected and public members of its
base class.
It can not access private members of the base class.
PUBLIC
BASE CLASS
CA
PROTECTED INH N BE
ER
ITE
D
PRIVATE
CA
INH N BE
ER CHILD CLASS
IT E
D
NOTE: Constructors and destructors of the base class are never inherited.
VISIBILTY MODES AND
INHERITANCE
A child class can inherit base class in three ways. These are:
M em
I nh e bas bers of PRIVATE PROTECTED PUBLIC
ritan e cl
ce t ass
y pe
PRIVATE NOT Become private Become private
INHERITED members of child members of child
class class
ed
ed
erit
erit
erit
PROTECTE PROTECTE PROTECTE
in h
in h
in h
D D D
PUBLIC PUBLIC PUBLIC
Not
Not
Not
PUBLIC PUBLIC PUBLIC
PROTECTE PROTECTE PROTECTE
D D D
PRIVAT PRIVAT PRIVAT
E E E
CHILD CHILD CHILD
CLAS CLAS CLAS
S
PRIVATE INHERTANCE S S
PROTECTED INHERITANCE
PUBLIC INHERITANCE
PRIVATE INHERITANCE
In private inheritance protected and public members of the base class
become the private members of the derived class.
class child
{
private:
class base class child : private base int x;
{ { void funcx();
Protected members
private: private: int b;
inherited from base
int a; int x; void funcb(); class
void funca(); Private void funcx(); int c;
Public members
protected: protected: void funcc(); inherited from base
int b; inheritance int y; protected: class
void funcb(); void funcy(); int y;
public: public: void funcy();
int c; int z; public:
void funcc(); void funcz(); int z;
} } void funcz();
}
New child class after
inheritance
PROTECTED
become the protected INHERITANCE
In protected inheritance protected and public members of the base class
members of the derived class.
class child
{
private:
class base class child : protected base int x;
{ { void funcx();
private: private: protected:
int a; int x; int y;
void funca(); Protected void funcx(); void funcy();
int b; Protected members
protected: protected: inherited from base
int b; inheritanceint y; void funcb(); class
void funcb(); void funcy(); int c;
void funcc(); Public members
public: public: inherited from base
int c; int z; public: class
void funcc(); void funcz(); int z;
} } void funcz();
}
New child class after
inheritance
PUBLIC INHERITANCE
In protected inheritance protected members become the protected members of the base
class and public members of the base class become the public members of the derived
class. class child
{
private:
class child : public int x;
class base
base void funcx();
{
{ protected:
private:
private: int y;
int a;
int x; void funcy();
void funca(); Public void funcx(); int b; Protected members
protected:
protected: void funcb();
inherited from base
int b; inheritance int y; class
void funcb(); public:
void funcy(); int z;
public:
public: void funcz();
int c;
int z; int c; Public members
void funcc();
void funcz(); void funcc(); inherited from base
} class
} }
New child class after
inheritance
TYPES OF INHERITANCE
1.Single Inheritance
2.Multiple Inheritance
3.Multilevel Inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
SINGLE INHERITENCE
EXAMPL COURSE
E
class student
class course : public
{
student
private:
{
char name[20];
long course_code;
float marks;
char course_name;
protected:
public:
void result();
course();
public:
void commence();
student();
void cdetail();
void enroll();
}
void display();
}
MULTILEVEL
INHERITENCE
In Multi level inheritance, a subclass inherits
from a class that itself inherits from another
class.
FURNITUR
E
SOFA
EXAMPLE
OFFICE
INHERITENCE
EXAMPLE
COMPANY
class chaiperson
class company: private
{
class director chairperson, public
long chairid;
{ director
char name[20];
long directorid; {
protected:
char dname[20]; int companyid;
char description[20];
public: char city[20];
void allocate();
director(); char country[20];
public:
void entry(); public:
chairperson();
void display(); void ctentry();
void assign();
}; void ctdisplay();
void show();
};
};
HIERARCHICAL INHERITENCE
class toys
class softtoys: public
{ class electronictoys:
toys
char tcode[5]; public toys
{
protected: {
chat stname[20];
float price; char etname[20];
float weight;
void assign(float); int no_of_batteries;
public:
public: public:
softtoys();
toys(); void etentry();
void stentry();
void tentry(); void etdisplay();
void stdisplay();
void tdisplay(); };
};
};
HYBRID INHERITENCE
It combines two or more types of inheritance. In this
type of inheritance we can have a mixture of number
of inheritances.
CONSTRUCTORS AND DESTRUCTORS IN
BASE AND DERIVED CLASSES