SlideShare a Scribd company logo
INHERITANCE IN C++
Inheritance is the process by which new classes called derived
classes are created from existing classes called base classes.
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.
The idea of inheritance implements the is a relationship. For
example, mammal IS-A animal, dog IS-A mammal hence dog IS-A
animal as well and so on.
WHAT IS AN INHERTANCE?
WHAT IS AN INHERTANCE? contd…
MAMMAL
All mammals have
certain characteristics.
Dog is a mammal. It has all features of
mammals in addition to its own unique
features
Cat is a mammal. It has all features of
mammals in addition to its own unique
features
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 .
FEATURES /ADVANTAGES OF
INHERITANCE
To create a derived class from an already existing base class the syntax
is:
class derived-class: access-specifier base-class
{
….
}
Where access specifier is one of public, protected, or private.
SYNTAX
For example, if the base class is animals and
the derived class is amphibians it is specified
as:
class animals //base class
{
…….
};
class amphibians : public animals
{ //derived class
…..
};
SYNTAX contd……
In this example class amphibians
have access to both public and
protected members of base class
animals.
NOTE: A class can be derived from
more than one class, which means it
can inherit data and functions from
multiple base classes. In that case a
class derivation lists names of one
or more base classes each
separated by comma.
 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.
ACCESS CONTROL AND INHERITENCE
PRIVATE
PROTECTED
PUBLIC
BASE CLASS
CHILD CLASS
CAN NOT BE INHERITED
We can summarize the different access types according to who can access them in
the following way:
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no
NOTE: Constructors and destructors of the base class are never inherited.
ACCESS CONTROL AND INHERITENCE contd…
VISIBILTY MODES AND INHERITANCE
A child class can inherit base class in three ways. These are:
PRIVATE PROTECTED PUBLIC
PRIVATE NOT
INHERITED
Become private
of child class
Become private
of child class
PROTECTED NOT
INHERITED
Become protected
members of child class
Become protected
members of child class
PUBLIC NOT
INHERITED
Become protected
members of child class
Become public members
of child class
PRIVATE
BASE
CLASS
PRIVATE
CHILD
CLASS
PRIVATE
BASE
CLASS
PRIVATE
CHILD
CLASS
PRIVATE
BASE
CLASS
PRIVATE
CHILD
CLASS
PROTECTED
PROTECTED PROTECTED
PROTECTEDPROTECTED
PROTECTED
PUBLIC
PUBLIC PUBLICPUBLIC
PUBLICPUBLIC
PRIVATE INHERTANCE PROTECTED INHERITANCE PUBLIC INHERITANCE
PRIVATE INHERITANCE
class child : private base
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
class child
{
private:
int x;
void funcx();
int b;
void funcb();
int c;
void funcc();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
In private inheritance protected and public members of the base class become the
private members of the derived class.
class base
{
private:
int a;
void funca();
protected:
int b;
void funcb();
public:
int c;
void funcc();
}
Private
inheritance
New child class after inheritance
Protected members
inherited from base
class
Public members
inherited from base
class
PROTECTED INHERITANCE
class child : protected base
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
class child
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
int b;
void funcb();
int c;
void funcc();
public:
int z;
void funcz();
}
In protected inheritance protected and public members of the base class become the
protected members of the derived class.
class base
{
private:
int a;
void funca();
protected:
int b;
void funcb();
public:
int c;
void funcc();
}
Protected
inheritance
New child class after inheritance
Protected members
inherited from base
class
Public members
inherited from base
class
PUBLIC INHERITANCE
class child : public base
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
public:
int z;
void funcz();
}
class child
{
private:
int x;
void funcx();
protected:
int y;
void funcy();
int b;
void funcb();
public:
int z;
void funcz();
int c;
void funcc();
}
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 base
{
private:
int a;
void funca();
protected:
int b;
void funcb();
public:
int c;
void funcc();
}
Public
inheritance
New child class after inheritance
Protected members
inherited from base
class
Public members
inherited from base
class
TYPES OF INHERITANCE
There are five different types of inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
SINGLE INHERITENCE
Single inheritance is the one where you have a single base
class and a single derived class.
EXAMPLE
class student
{
private:
char name[20];
float marks;
protected:
void result();
public:
student();
void enroll();
void display();
}
class course : public student
{
long course_code;
char course_name;
public:
course();
void commence();
void cdetail();
}
STUDENT
COURSE
MULTILEVEL INHERITENCE
In Multi level inheritance, a subclass inherits from a
class that itself inherits from another class.
EXAMPLE
class furniture
{
char type;
char model[10];
public:
furniture();
void readdata();
void dispdata();
}
class sofa: public furniture
{
int no_of_seats;
float cost;
public:
void indata();
void outdata();
};
class office: private sofa
{
int no_of_pieces;
char delivery_date[10];
public:
void readdetails()
void displaydetails();
}
FURNITURE
OFFICE
SOFA
MULTIPLE INHERITENCE
In Multiple inheritances, a derived class inherits from multiple base
classes. It has properties of both the base classes.
MULTIPLE INHERITENCE
EXAMPLE
class chaiperson
{
long chairid;
char name[20];
protected:
char description[20];
void allocate();
public:
chairperson();
void assign();
void show();
};
class director
{
long directorid;
char dname[20];
public:
director();
void entry();
void display();
};
class company: private
chairperson, public director
{
int companyid;
char city[20];
char country[20];
public:
void ctentry();
void ctdisplay();
};
COMPANY
CHAIRPERSON DIRECTOR
HIERARCHICAL INHERITENCE
In hierarchical Inheritance, it's like an inverted tree. So
multiple classes inherit from a single base class.
HIERARCHICAL INHERITENCE
EXAMPLE
class toys
{
char tcode[5];
protected:
float price;
void assign(float);
public:
toys();
void tentry();
void tdisplay();
};
class softtoys: public toys
{
chat stname[20];
float weight;
public:
softtoys();
void stentry();
void stdisplay();
};
class electronictoys: public
toys
{
char etname[20];
int no_of_batteries;
public:
void etentry();
void etdisplay();
};
TOYS
ELECTRONIC
TOYS
SOFT
TOYS
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
 Derived classes can have their own constructors and
destructors.
 When an object of a derived class is created, the base
class’s constructor is executed first, followed by the derived
class’s constructor.
 When an object of a derived class goes out of scope, its
destructor is called first, then that of the base class.
IMPROTANT POINTS TO
NOTE Calculating the size of the object of the child class:
 While calculating the size of the object of the child class, add the size of all data members
of base class including the private members of the base class and the child class.
 If child class is inheriting from multiple base classes, add the size of data members of all
base classes and the child class.
 In case of multilevel inheritance the size of all base classes(directly /indirectly) inherited by
child class is added to the size of child class data members
 Members accessible to the object of the child class:
Only public members of the new modified child class(after inheritance) are accessible to the
object of the child class.
 Members accessible to the functions of the child class:
All members: public, protected, private, of the new modified child class(after inheritance)
are accessible to the functions of the child class.
If a base class has parametrized constructor then it is the duty of child class to pass the
parameters for base class constructor also at the time of creation of object.
PASSING ARGUMENTS TO BASE CLASS CONSTRUCTOR
class student
{
private:
char name[20];
float marks;
protected:
void result();
public:
student(char nam[20], float mar);
void enroll();
void display();
}
class course : public student
{
long course_code;
char course_name[20];
public:
course(long cc, char cn[20],char nam[20], float mar ) :
student(char nam[20], float mar);
void commence();
void cdetail();
}
course c1(01,”CS”,”Naman”, 460);
Base class
constructor
Child class
constructor
Base class constructor
parameters

More Related Content

What's hot

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
GirdharRatne
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
Iqra khalil
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 

What's hot (20)

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
class and objects
class and objectsclass and objects
class and objects
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Java package
Java packageJava package
Java package
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 

Viewers also liked

inheritance c++
inheritance c++inheritance c++
inheritance c++
Muraleedhar Sundararajan
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Laxman Puri
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Rosie Jane Enomar
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Digvijay Singh Karakoti
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++
Abhishek Pratap
 
Inheritance
InheritanceInheritance
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
k v
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
HalaiHansaika
 
Cisco Industry template - 4x3 dark
Cisco Industry template - 4x3 darkCisco Industry template - 4x3 dark
Cisco Industry template - 4x3 darkKVEDesign
 
Computer Hardware Basics (Components to be understand)
Computer Hardware Basics (Components to be understand)Computer Hardware Basics (Components to be understand)
Computer Hardware Basics (Components to be understand)
Digvijay Singh Karakoti
 
Programming in c++
Programming in c++Programming in c++
Programming in c++Baljit Saini
 
Analog Electronics ppt on Transition & diffusion capacitance by Being topper
Analog Electronics  ppt on Transition & diffusion capacitance by Being topperAnalog Electronics  ppt on Transition & diffusion capacitance by Being topper
Analog Electronics ppt on Transition & diffusion capacitance by Being topper
Vipin Kumar
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
sanya6900
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritanceharshaltambe
 

Viewers also liked (20)

inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
Cisco Industry template - 4x3 dark
Cisco Industry template - 4x3 darkCisco Industry template - 4x3 dark
Cisco Industry template - 4x3 dark
 
Computer Hardware Basics (Components to be understand)
Computer Hardware Basics (Components to be understand)Computer Hardware Basics (Components to be understand)
Computer Hardware Basics (Components to be understand)
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Analog Electronics ppt on Transition & diffusion capacitance by Being topper
Analog Electronics  ppt on Transition & diffusion capacitance by Being topperAnalog Electronics  ppt on Transition & diffusion capacitance by Being topper
Analog Electronics ppt on Transition & diffusion capacitance by Being topper
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
 

Similar to Inheritance in c++

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
urvashipundir04
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
Nikunj Patel
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
NAVANEETCHATURVEDI2
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
Inheritance
InheritanceInheritance
Inheritance
Pranali Chaudhari
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
preetirawat242004
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Shweta Shah
 
Inheritance
InheritanceInheritance
Inheritance
prashant prath
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Inheritance
InheritanceInheritance
Inheritance
Munsif Ullah
 
Lec5.ppt
Lec5.pptLec5.ppt
Lec5.ppt
CharlesAsiedu4
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
Mirza Hussain
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
MASQ Technologies
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
kashafishfaq21
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Manish Tiwari
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
sushamaGavarskar1
 

Similar to Inheritance in c++ (20)

inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance
InheritanceInheritance
Inheritance
 
Lec5.ppt
Lec5.pptLec5.ppt
Lec5.ppt
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
00ps inheritace using c++
00ps inheritace using c++00ps inheritace using c++
00ps inheritace using c++
 

More from Vineeta Garg

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
Vineeta Garg
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
GUI programming
GUI programmingGUI programming
GUI programming
Vineeta Garg
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
Vineeta Garg
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
Vineeta Garg
 
SQL
SQLSQL
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
Vineeta Garg
 

More from Vineeta Garg (10)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
 
SQL
SQLSQL
SQL
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 

Recently uploaded

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 

Recently uploaded (20)

Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 

Inheritance in c++

  • 2. Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. 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. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on. WHAT IS AN INHERTANCE?
  • 3. WHAT IS AN INHERTANCE? contd… MAMMAL All mammals have certain characteristics. Dog is a mammal. It has all features of mammals in addition to its own unique features Cat is a mammal. It has all features of mammals in addition to its own unique features
  • 4. 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 . FEATURES /ADVANTAGES OF INHERITANCE
  • 5. To create a derived class from an already existing base class the syntax is: class derived-class: access-specifier base-class { …. } Where access specifier is one of public, protected, or private. SYNTAX
  • 6. For example, if the base class is animals and the derived class is amphibians it is specified as: class animals //base class { ……. }; class amphibians : public animals { //derived class ….. }; SYNTAX contd…… In this example class amphibians have access to both public and protected members of base class animals. NOTE: A class can be derived from more than one class, which means it can inherit data and functions from multiple base classes. In that case a class derivation lists names of one or more base classes each separated by comma.
  • 7.  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. ACCESS CONTROL AND INHERITENCE PRIVATE PROTECTED PUBLIC BASE CLASS CHILD CLASS CAN NOT BE INHERITED
  • 8. We can summarize the different access types according to who can access them in the following way: Access public protected private Same class yes yes yes Derived classes yes yes no Outside classes yes no no NOTE: Constructors and destructors of the base class are never inherited. ACCESS CONTROL AND INHERITENCE contd…
  • 9. VISIBILTY MODES AND INHERITANCE A child class can inherit base class in three ways. These are: PRIVATE PROTECTED PUBLIC PRIVATE NOT INHERITED Become private of child class Become private of child class PROTECTED NOT INHERITED Become protected members of child class Become protected members of child class PUBLIC NOT INHERITED Become protected members of child class Become public members of child class
  • 11. PRIVATE INHERITANCE class child : private base { private: int x; void funcx(); protected: int y; void funcy(); public: int z; void funcz(); } class child { private: int x; void funcx(); int b; void funcb(); int c; void funcc(); protected: int y; void funcy(); public: int z; void funcz(); } In private inheritance protected and public members of the base class become the private members of the derived class. class base { private: int a; void funca(); protected: int b; void funcb(); public: int c; void funcc(); } Private inheritance New child class after inheritance Protected members inherited from base class Public members inherited from base class
  • 12. PROTECTED INHERITANCE class child : protected base { private: int x; void funcx(); protected: int y; void funcy(); public: int z; void funcz(); } class child { private: int x; void funcx(); protected: int y; void funcy(); int b; void funcb(); int c; void funcc(); public: int z; void funcz(); } In protected inheritance protected and public members of the base class become the protected members of the derived class. class base { private: int a; void funca(); protected: int b; void funcb(); public: int c; void funcc(); } Protected inheritance New child class after inheritance Protected members inherited from base class Public members inherited from base class
  • 13. PUBLIC INHERITANCE class child : public base { private: int x; void funcx(); protected: int y; void funcy(); public: int z; void funcz(); } class child { private: int x; void funcx(); protected: int y; void funcy(); int b; void funcb(); public: int z; void funcz(); int c; void funcc(); } 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 base { private: int a; void funca(); protected: int b; void funcb(); public: int c; void funcc(); } Public inheritance New child class after inheritance Protected members inherited from base class Public members inherited from base class
  • 14. TYPES OF INHERITANCE There are five different types of inheritance: 1. Single Inheritance 2. Multiple Inheritance 3. Multilevel Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance
  • 15. SINGLE INHERITENCE Single inheritance is the one where you have a single base class and a single derived class.
  • 16. EXAMPLE class student { private: char name[20]; float marks; protected: void result(); public: student(); void enroll(); void display(); } class course : public student { long course_code; char course_name; public: course(); void commence(); void cdetail(); } STUDENT COURSE
  • 17. MULTILEVEL INHERITENCE In Multi level inheritance, a subclass inherits from a class that itself inherits from another class.
  • 18. EXAMPLE class furniture { char type; char model[10]; public: furniture(); void readdata(); void dispdata(); } class sofa: public furniture { int no_of_seats; float cost; public: void indata(); void outdata(); }; class office: private sofa { int no_of_pieces; char delivery_date[10]; public: void readdetails() void displaydetails(); } FURNITURE OFFICE SOFA
  • 19. MULTIPLE INHERITENCE In Multiple inheritances, a derived class inherits from multiple base classes. It has properties of both the base classes.
  • 20. MULTIPLE INHERITENCE EXAMPLE class chaiperson { long chairid; char name[20]; protected: char description[20]; void allocate(); public: chairperson(); void assign(); void show(); }; class director { long directorid; char dname[20]; public: director(); void entry(); void display(); }; class company: private chairperson, public director { int companyid; char city[20]; char country[20]; public: void ctentry(); void ctdisplay(); }; COMPANY CHAIRPERSON DIRECTOR
  • 21. HIERARCHICAL INHERITENCE In hierarchical Inheritance, it's like an inverted tree. So multiple classes inherit from a single base class.
  • 22. HIERARCHICAL INHERITENCE EXAMPLE class toys { char tcode[5]; protected: float price; void assign(float); public: toys(); void tentry(); void tdisplay(); }; class softtoys: public toys { chat stname[20]; float weight; public: softtoys(); void stentry(); void stdisplay(); }; class electronictoys: public toys { char etname[20]; int no_of_batteries; public: void etentry(); void etdisplay(); }; TOYS ELECTRONIC TOYS SOFT TOYS
  • 23. HYBRID INHERITENCE It combines two or more types of inheritance. In this type of inheritance we can have a mixture of number of inheritances.
  • 24. CONSTRUCTORS AND DESTRUCTORS IN BASE AND DERIVED CLASSES  Derived classes can have their own constructors and destructors.  When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor.  When an object of a derived class goes out of scope, its destructor is called first, then that of the base class.
  • 25. IMPROTANT POINTS TO NOTE Calculating the size of the object of the child class:  While calculating the size of the object of the child class, add the size of all data members of base class including the private members of the base class and the child class.  If child class is inheriting from multiple base classes, add the size of data members of all base classes and the child class.  In case of multilevel inheritance the size of all base classes(directly /indirectly) inherited by child class is added to the size of child class data members  Members accessible to the object of the child class: Only public members of the new modified child class(after inheritance) are accessible to the object of the child class.  Members accessible to the functions of the child class: All members: public, protected, private, of the new modified child class(after inheritance) are accessible to the functions of the child class.
  • 26. If a base class has parametrized constructor then it is the duty of child class to pass the parameters for base class constructor also at the time of creation of object. PASSING ARGUMENTS TO BASE CLASS CONSTRUCTOR class student { private: char name[20]; float marks; protected: void result(); public: student(char nam[20], float mar); void enroll(); void display(); } class course : public student { long course_code; char course_name[20]; public: course(long cc, char cn[20],char nam[20], float mar ) : student(char nam[20], float mar); void commence(); void cdetail(); } course c1(01,”CS”,”Naman”, 460); Base class constructor Child class constructor Base class constructor parameters