SlideShare a Scribd company logo
1 of 39
Prof. Jinal Bhagat
102000212
Object Oriented
Programming
Unit-5
Inheritance
Unit-5 Objects and Classes A D Patel Institute of Technology 2
Outline
 Concept of inheritance
 Types of inheritance
1. Single
2. Multiple
3. Multilevel
4. Hierarchical
5. Hybrid
 Protected members
 Overriding
 Virtual base class
Weightage: 15%
Inheritance
Unit-5 Objects and Classes A D Patel Institute of Technology 4
Concept of Inheritance
Attributes:
Age, Height, Weight
Methods:
Talk()
Walk()
Eat()
Diagnose()
class Doctor
Attributes:
Age, Height, Weight
Methods:
Talk()
Walk()
Eat()
Playfootball()
class Footballer
Attributes:
Age, Height, Weight
Methods:
Talk()
Walk()
Eat()
Runbusiness()
class Businessman
 All of the classes have common attributes (Age, Height, Weight)
and methods (Walk, Talk, Eat).
 However, they have some special skills like Diagnose, Playfootball
and Runbusiness.
 In each of the classes, you would be copying the same code for
Walk, Talk and Eat for each character.
Unit-5 Objects and Classes A D Patel Institute of Technology 5
Concept of Inheritance(Cont…)
Methods:
Diagnose()
class Doctor
Methods:
Playfootball()
class Footballer
Methods:
Runbusiness()
class Businessman
Attributes:
Age, Height, Weight
Methods:
Talk() , Walk(), Eat()
class Person class Person
is called
Base class
These classes
are called
Derived class
Unit-5 Objects and Classes A D Patel Institute of Technology 6
Concept of Inheritance(Cont…)
Attributes:
privatevehicle
class Car
Attributes:
publicvehicle
class Bus
Attributes:
goodsvehicle
class Truck
Attributes:
Engineno, color
Methods:
applybreaks()
class Vehicle
Unit-5 Objects and Classes A D Patel Institute of Technology 7
Inheritance
 Inheritance is the process, by which class can acquire(reuse) the
properties and methods of another class.
 The mechanism of deriving a new class from an old class is called
inheritance.
 The new class is called derived class and old class is called base
class.
 The derived class may have all the features of the base class and
the programmer can add new features to the derived class.
Unit-5 Objects and Classes A D Patel Institute of Technology 8
Syntax to Inherit class
Syntax:
class derived-class-name : access-mode base-class-name
{
// body of class
};
Example:
class person{
//body of class
};
class doctor: person
{
//body of class
}
public
Base Class
Derived Class
Access Mode
protected
private
By default access mode is
private
Unit-5 Objects and Classes A D Patel Institute of Technology 9
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance (also known as Virtual Inheritance)
Unit-5 Objects and Classes A D Patel Institute of Technology 10
1. Single Inheritance
 If a class is derived from a single class then it
is called single inheritance.
 Class B is derived from class A
A
B
Example:
class Animal
{ public:
int legs = 4;
};
class Dog : public Animal
{ public:
int tail = 1;
};
Unit-5 Objects and Classes A D Patel Institute of Technology 11
Single Inheritance Program
class Animal{
int legs=4;
public:
void display1(){
cout<<"nLegs="<<legs;
}
};
class Dog : public Animal{
bool tail = true;
public:
void display2(){
cout<<"nTail="<<tail;
}
};
int main()
{
Animal a1;
Dog d1;
d1.display1();
d1.display2();
}
Output:
Legs=4
Tail=1
Unit-5 Objects and Classes A D Patel Institute of Technology 12
2. Multilevel Inheritance
 Any class is derived from a class which is derived
from another class then it is called multilevel
inheritance.
 Here, class C is derived from class B and class B is
derived from class A, so it is called multilevel
inheritance.
A
B
C
Example:
class Person
{
//content of class person
};
class Student :public Person
{
//content of Student class
};
class ITStudent :public
Student
{
//content of ITStudent
class
};
class Person{
public:
void display1(){
cout<<"nPerson class";
}
};
class Student:public Person{
public:
void display2(){
cout<<"nStudent class";
}
};
class ITStudent:public Student{
public:
void display3(){
cout<<"nITStudent class";
}
};
int main()
{
Person p;
Student s;
ITStudent i;
p.display1();
s.display2();
s.display1();
i.display3();
i.display2();
i.display1();
}
Output:
Person class
Student class
Person class
ITStudent class
Student class
Person class
Unit-5 Objects and Classes A D Patel Institute of Technology 14
3. Multiple Inheritance
 If a class is derived from more than one class
then it is called multiple inheritance.
 Here, class C is derived from two classes, class A
and class B.
Example:
class Liquid
{
//content of Liquid class
};
class Fuel
{
//content of Fuel class
};
class Petrol: public
Liquid, public Fuel
{
//content of
Petrol class
};
A B
C
class Liquid{
public:
void display1(){
cout<<"nLiquid class";
}
};
class Fuel{
public:
void display2(){
cout<<"nFuel class";
}
};
class Petrol:public Liquid,public Fuel{
public:
void display3(){
cout<<"nPetrol class";
}
};
int main()
{
Liquid l;
Fuel f;
Petrol p;
l.display1();
f.display2();
p.display3();
p.display2();
p.display1();
}
Output:
Liquid class
Fuel class
Petrol class
Fuel class
Liquid class
Unit-5 Objects and Classes A D Patel Institute of Technology 16
4. Hierarchical Inheritance
 If one or more classes are derived from
one class then it is called hierarchical
inheritance.
 Here, class B, class C and class D are
derived from class A.
Example:
class Animal
{
//content of class Animal
};
class Elephant :public Animal
{
//content of class Elephant
};
A
B C D
class Horse :public Animal
{
//content of class Horse
};
class Cow :public Animal
{
//content of class Cow
};
class Animal{
public:
void display1(){
cout<<"nAnimal Class";
}
};
class Elephant:public Animal{
public:
void display2(){
cout<<"nElephant class";
}
};
class Horse:public Animal{
public:
void display3(){
cout<<"nHorse class";
}
};
class Cow:public Animal{
public:
void display4(){
cout<<"nCow class";
}
};
int main(){
Animal a; Elephant e; Horse h; Cow c;
a.display1();
e.display2(); e.display1();
h.display3(); h.display1();
c.display4(); c.display1();
}
Output:
Animal Class
Elephant class
Animal Class
Horse class
Animal Class
Cow class
Animal Class
Unit-5 Objects and Classes A D Patel Institute of Technology 18
5. Hybrid Inheritance
 It is a combination of any other inheritance types. That is either
multiple or multilevel or hierarchical or any other combination.
 Here, class B and class C are derived from class A and class D is
derived from class B and class C.
 class A, class B and class C is example of Hierarchical Inheritance
and class B, class C and class D is example of Multiple Inheritance
so this hybrid inheritance is combination of Hierarchical and
Multiple Inheritance.
A
B C
D
Unit-5 Objects and Classes A D Patel Institute of Technology 19
Hybrid Inheritance (Cont…)
class Car
{
//content of class Car
};
class FuelCar:public Car
{
//content of class FuelCar
};
Class ElectricCar:public Car
{
//content of class ElectricCar
};
Class HybridCar:public FuelCar, public ElectricCar
{
//content of classHybridCar
};
class Car{
public:
void display1(){
cout<<"nCar class";
}
};
class FuelCar:public Car{
public:
void display2(){
cout<<"nFuelCar class";
}
};
class ElecCar:public Car{
public:
void display3(){
cout<<"nElecCar class";
}
};
class HybridCar:public
FuelCar, public ElecCar{
public:
void display4(){
cout<<"nHybridCar class";
}
};
int main(){
Car c; FuelCar f; ElecCar e;
HybridCar h;
h.display4();
h.display3();
h.display2();
}
Output:
HybridCar class
ElecCar class
FuelCar class
Unit-5 Objects and Classes A D Patel Institute of Technology 21
GTU Program
 Create a class student that stores roll_no, name. Create a class test
that stores marks obtained in five subjects. Class result derived
from student and test contains the total marks and percentage
obtained in test. Input and display information of a student.
Attributes:
roll_no
name
class student
Attributes:
marks[5]
Class test
Attributes:
totalmarks
percentage
class result
Unit-5 Objects and Classes A D Patel Institute of Technology 22
Protected access modifier
 Protected access modifier plays a key role in inheritance.
 Protected members of the class can be accessed within the class
and from derived class but cannot be accessed from any other
class or program.
 It works like public for derived class and private for other class.
class ABC {
public:
void setProtMemb(int i){
m_protMemb = i; }
void Display(){
cout<<m_protMemb<<endl;}
protected:
int m_protMemb;
void Protfunc(){
cout<<"nAccess allowedn";}
};
class XYZ : public ABC{
public:
void useProtfunc(){
Protfunc(); }
};
int main() {
ABC a; XYZ x;
a.m_protMemb;
a.setProtMemb(0);
a.Display();
a.Protfunc();
x.setProtMemb(5);
x.Display();
x.useProtfunc();}
//error, m_protMemb is protected
//OK,uses public access function
//OK,uses public access function
//error, Protfunc() is protected
// OK, uses public access function
Unit-5 Objects and Classes A D Patel Institute of Technology 25
Class access modifiers
 public – Public members are visible to all classes.
 private – Private members are visible only to the class to which
they belong.
 protected – Protected members are visible only to the class to
which they belong, and derived classes.
Unit-5 Objects and Classes A D Patel Institute of Technology 26
Access modifiers
Base class
members
How inherited base class
members
appear in derived class
private: x
protected: y
public: z
x is inaccessible
protected: y
public: z
public
base class
private: x
protected:y
public: z
x is inaccessible
private: y
private: z
private
base class
private: x
protected:y
public: z
x is inaccessible
protected: y
protected: z
protected
base class
class base{
private:
int z;
public:
int x;
protected:
int y;
};
class publicDerived: public base{
// x is public
// y is protected
// z is not accessible from publicDerived
};
class protectedDerived: protected base{
// x is protected
// y is protected
// z is not accessible from
protectedDerived
};
class privateDerived: private base
{
// x is private
// y is private
// z is not accessible from
privateDerived
};
Unit-5 Objects and Classes A D Patel Institute of Technology 28
Inheritance using Public Access
private members:
char letter;
float score;
void calcGrade();
public members:
void setScore(float);
float getScore();
char getLetter();
class Grade
private members:
int numQuestions;
float pointsEach;
int numMissed;
public members:
Test(int, int);
class Test : public Grade
When Test class inherits
from Grade class using
public class access, it
looks like this:
private members:
int numQuestions;
float pointsEach;
int numMissed;
public members:
Test(int, int);
void setScore(float);
float getScore();
char getLetter();
Unit-5 Objects and Classes A D Patel Institute of Technology 29
Inheritance using Private Access
private members:
char letter;
float score;
void calcGrade();
public members:
void setScore(float);
float getScore();
char getLetter();
class Grade
private members:
int numQuestions;
float pointsEach;
int numMissed;
public members:
Test(int, int);
class Test : private Grade
When Test class inherits
from Grade class using
private class access, it
looks like this:
private members:
int numQuestions:
float pointsEach;
int numMissed;
void setScore(float);
float getScore();
float getLetter();
public members:
Test(int, int);
Unit-5 Objects and Classes A D Patel Institute of Technology 30
Inheritance using Protected Access
private members:
char letter;
float score;
void calcGrade();
public members:
void setScore(float);
float getScore();
char getLetter();
class Grade
private members:
int numQuestions;
float pointsEach;
int numMissed;
public members:
Test(int, int);
class Test : protected Grade
When Test class inherits
from Grade class using
protected class access, it
looks like this:
private members:
int numQuestions:
float pointsEach;
int numMissed;
public members:
Test(int, int);
protected members:
void setScore(float);
float getScore();
float getLetter();
Unit-5 Objects and Classes A D Patel Institute of Technology 31
Visibility of inherited members
Base class
visibility
Derived class visibility
Public
derivation
Private
derivation
Protected
derivation
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected
Unit-5 Objects and Classes A D Patel Institute of Technology 32
Function Overriding / Method Overriding
 If base class and derived class have member functions with same
name and arguments then method is said to be overridden and it
is called function overriding or method overriding in C++.
class ABC
{
public:
void display(){
cout<<"This is parent class";
}
};
class XYZ:public ABC{
public:
void display(){//overrides the display()mehtod of class ABC
cout<<"nThis is child class";
}
};
int main(){
XYZ x;
x.display();//method of class XYZ invokes, instead of class ABC
x.ABC::display();
}
Unit-5 Objects and Classes A D Patel Institute of Technology 34
Virtual Base Class
A
memberA
B
memberA
C
memberA
D
memberA Multiple copies of member A
we can prevent multiple copies
of the base class by declaring
the base class as virtual when
it is being inherited.
Unit-5 Objects and Classes A D Patel Institute of Technology 35
Virtual base class (Cont…)
 Virtual base class is used to prevent the duplication/ambiguity.
 In hybrid inheritance child class has two direct parents which
themselves have a common base class.
 So, the child class inherits the grandparent via two separate paths.
it is also called as indirect parent class.
 All the public and protected member of grandparent are inherited
twice into child.
 We can stop this duplication by making base class virtual.
class A
{
public:
int i;
};
class B:virtual public A
{
public:
int j;
};
class C: public virtual A
{
public:
int k;
};
class D:public B, public C
{
public:
int sum;
};
int main()
{
D ob1;
ob1.i=10;
ob1.j=20;
ob1.k=30;
ob1.sum=ob1.i+ob1.j+ob1.k;
cout<<ob1.sum;
}
Unit-5 Objects and Classes A D Patel Institute of Technology 37
Derived class constructor
class Base{
int x;
public:
Base() { cout << "Base default constrictors"; }
};
class Derived : public Base
{ int y;
public:
Derived() { cout<<"Derived default constructor"; }
Derived(int i) { cout<<"Derived parameterized constructor";}
};
int main(){
Base b;
Derived d1;
Derived d2(10);
}
Unit-5 Objects and Classes A D Patel Institute of Technology 38
Derived class constructor (Cont…)
class Base
{ int x;
public:
Base(int i){
x = i; cout << "x="<<x;
}
};
class Derived : public Base {
int y;
public:
Derived(int i,int j) : Base(j)
{ y = i; cout << "y="<<y;
}
};
int main()
{
Derived d(10,20) ;
}
Unit-5 Objects and Classes A D Patel Institute of Technology 39
Execution of base class constructor
Method of inheritance Order of execution
class Derived: public Base
{
};
Base();
Derived();
class C: public A, public B
{
};
A();//base(first)
B();//base(Second)
C();derived
class C:public A, virtual public B
{
};
B();//virtual base
A();//base
C();derived
Thank You

More Related Content

Similar to hhuhuihuhuihPresentations_PPT_Unit-5_OOP.pptx

core java material.pdf
core java material.pdfcore java material.pdf
core java material.pdfRasa72
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
Conceitos Fundamentais de Orientação a Objetos
Conceitos Fundamentais de Orientação a ObjetosConceitos Fundamentais de Orientação a Objetos
Conceitos Fundamentais de Orientação a Objetosguest22a621
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending ClassesNilesh Dalvi
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
inheritance in OOPM
inheritance in OOPMinheritance in OOPM
inheritance in OOPMPKKArc
 
Esoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basicsEsoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basicsRasan Samarasinghe
 

Similar to hhuhuihuhuihPresentations_PPT_Unit-5_OOP.pptx (20)

inhertance c++
inhertance c++inhertance c++
inhertance c++
 
core java material.pdf
core java material.pdfcore java material.pdf
core java material.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
chap11.ppt
chap11.pptchap11.ppt
chap11.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Object
ObjectObject
Object
 
Conceitos Fundamentais de Orientação a Objetos
Conceitos Fundamentais de Orientação a ObjetosConceitos Fundamentais de Orientação a Objetos
Conceitos Fundamentais de Orientação a Objetos
 
Questões de Certificação SCJP
Questões de Certificação SCJPQuestões de Certificação SCJP
Questões de Certificação SCJP
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
inheritance in OOPM
inheritance in OOPMinheritance in OOPM
inheritance in OOPM
 
Esoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basicsEsoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Certificate in java basics
 

More from ZeelGoyani

Os presentation final.pptxjjjjjdakajwsjjdhdfjff
Os presentation final.pptxjjjjjdakajwsjjdhdfjffOs presentation final.pptxjjjjjdakajwsjjdhdfjff
Os presentation final.pptxjjjjjdakajwsjjdhdfjffZeelGoyani
 
Java Presentation[1].pptxhhuvbhujhhujnnjoj
Java Presentation[1].pptxhhuvbhujhhujnnjojJava Presentation[1].pptxhhuvbhujhhujnnjoj
Java Presentation[1].pptxhhuvbhujhhujnnjojZeelGoyani
 
pptof5gtechnology16btece014-191201094719.pptx
pptof5gtechnology16btece014-191201094719.pptxpptof5gtechnology16btece014-191201094719.pptx
pptof5gtechnology16btece014-191201094719.pptxZeelGoyani
 
Journey-of-Personal-Development-Part-1.pptx
Journey-of-Personal-Development-Part-1.pptxJourney-of-Personal-Development-Part-1.pptx
Journey-of-Personal-Development-Part-1.pptxZeelGoyani
 
12202080601124-Sargam Desai.ppt df TG vs TV TB yx
12202080601124-Sargam Desai.ppt df TG vs TV TB yx12202080601124-Sargam Desai.ppt df TG vs TV TB yx
12202080601124-Sargam Desai.ppt df TG vs TV TB yxZeelGoyani
 
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptx
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptxppt-personalitydevelopment-130903024646-phpapp01 (2).pptx
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptxZeelGoyani
 
Self Discovery.pptnmkkeusuziiwudhwhdhueuur v
Self Discovery.pptnmkkeusuziiwudhwhdhueuur vSelf Discovery.pptnmkkeusuziiwudhwhdhueuur v
Self Discovery.pptnmkkeusuziiwudhwhdhueuur vZeelGoyani
 
291-02.l20.ppt
291-02.l20.ppt291-02.l20.ppt
291-02.l20.pptZeelGoyani
 

More from ZeelGoyani (8)

Os presentation final.pptxjjjjjdakajwsjjdhdfjff
Os presentation final.pptxjjjjjdakajwsjjdhdfjffOs presentation final.pptxjjjjjdakajwsjjdhdfjff
Os presentation final.pptxjjjjjdakajwsjjdhdfjff
 
Java Presentation[1].pptxhhuvbhujhhujnnjoj
Java Presentation[1].pptxhhuvbhujhhujnnjojJava Presentation[1].pptxhhuvbhujhhujnnjoj
Java Presentation[1].pptxhhuvbhujhhujnnjoj
 
pptof5gtechnology16btece014-191201094719.pptx
pptof5gtechnology16btece014-191201094719.pptxpptof5gtechnology16btece014-191201094719.pptx
pptof5gtechnology16btece014-191201094719.pptx
 
Journey-of-Personal-Development-Part-1.pptx
Journey-of-Personal-Development-Part-1.pptxJourney-of-Personal-Development-Part-1.pptx
Journey-of-Personal-Development-Part-1.pptx
 
12202080601124-Sargam Desai.ppt df TG vs TV TB yx
12202080601124-Sargam Desai.ppt df TG vs TV TB yx12202080601124-Sargam Desai.ppt df TG vs TV TB yx
12202080601124-Sargam Desai.ppt df TG vs TV TB yx
 
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptx
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptxppt-personalitydevelopment-130903024646-phpapp01 (2).pptx
ppt-personalitydevelopment-130903024646-phpapp01 (2).pptx
 
Self Discovery.pptnmkkeusuziiwudhwhdhueuur v
Self Discovery.pptnmkkeusuziiwudhwhdhueuur vSelf Discovery.pptnmkkeusuziiwudhwhdhueuur v
Self Discovery.pptnmkkeusuziiwudhwhdhueuur v
 
291-02.l20.ppt
291-02.l20.ppt291-02.l20.ppt
291-02.l20.ppt
 

Recently uploaded

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityVictorSzoltysek
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data SciencePaolo Missier
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

hhuhuihuhuihPresentations_PPT_Unit-5_OOP.pptx

  • 1. Prof. Jinal Bhagat 102000212 Object Oriented Programming Unit-5 Inheritance
  • 2. Unit-5 Objects and Classes A D Patel Institute of Technology 2 Outline  Concept of inheritance  Types of inheritance 1. Single 2. Multiple 3. Multilevel 4. Hierarchical 5. Hybrid  Protected members  Overriding  Virtual base class Weightage: 15%
  • 4. Unit-5 Objects and Classes A D Patel Institute of Technology 4 Concept of Inheritance Attributes: Age, Height, Weight Methods: Talk() Walk() Eat() Diagnose() class Doctor Attributes: Age, Height, Weight Methods: Talk() Walk() Eat() Playfootball() class Footballer Attributes: Age, Height, Weight Methods: Talk() Walk() Eat() Runbusiness() class Businessman  All of the classes have common attributes (Age, Height, Weight) and methods (Walk, Talk, Eat).  However, they have some special skills like Diagnose, Playfootball and Runbusiness.  In each of the classes, you would be copying the same code for Walk, Talk and Eat for each character.
  • 5. Unit-5 Objects and Classes A D Patel Institute of Technology 5 Concept of Inheritance(Cont…) Methods: Diagnose() class Doctor Methods: Playfootball() class Footballer Methods: Runbusiness() class Businessman Attributes: Age, Height, Weight Methods: Talk() , Walk(), Eat() class Person class Person is called Base class These classes are called Derived class
  • 6. Unit-5 Objects and Classes A D Patel Institute of Technology 6 Concept of Inheritance(Cont…) Attributes: privatevehicle class Car Attributes: publicvehicle class Bus Attributes: goodsvehicle class Truck Attributes: Engineno, color Methods: applybreaks() class Vehicle
  • 7. Unit-5 Objects and Classes A D Patel Institute of Technology 7 Inheritance  Inheritance is the process, by which class can acquire(reuse) the properties and methods of another class.  The mechanism of deriving a new class from an old class is called inheritance.  The new class is called derived class and old class is called base class.  The derived class may have all the features of the base class and the programmer can add new features to the derived class.
  • 8. Unit-5 Objects and Classes A D Patel Institute of Technology 8 Syntax to Inherit class Syntax: class derived-class-name : access-mode base-class-name { // body of class }; Example: class person{ //body of class }; class doctor: person { //body of class } public Base Class Derived Class Access Mode protected private By default access mode is private
  • 9. Unit-5 Objects and Classes A D Patel Institute of Technology 9 Types of Inheritance 1. Single Inheritance 2. Multilevel Inheritance 3. Multiple Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance (also known as Virtual Inheritance)
  • 10. Unit-5 Objects and Classes A D Patel Institute of Technology 10 1. Single Inheritance  If a class is derived from a single class then it is called single inheritance.  Class B is derived from class A A B Example: class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; };
  • 11. Unit-5 Objects and Classes A D Patel Institute of Technology 11 Single Inheritance Program class Animal{ int legs=4; public: void display1(){ cout<<"nLegs="<<legs; } }; class Dog : public Animal{ bool tail = true; public: void display2(){ cout<<"nTail="<<tail; } }; int main() { Animal a1; Dog d1; d1.display1(); d1.display2(); } Output: Legs=4 Tail=1
  • 12. Unit-5 Objects and Classes A D Patel Institute of Technology 12 2. Multilevel Inheritance  Any class is derived from a class which is derived from another class then it is called multilevel inheritance.  Here, class C is derived from class B and class B is derived from class A, so it is called multilevel inheritance. A B C Example: class Person { //content of class person }; class Student :public Person { //content of Student class }; class ITStudent :public Student { //content of ITStudent class };
  • 13. class Person{ public: void display1(){ cout<<"nPerson class"; } }; class Student:public Person{ public: void display2(){ cout<<"nStudent class"; } }; class ITStudent:public Student{ public: void display3(){ cout<<"nITStudent class"; } }; int main() { Person p; Student s; ITStudent i; p.display1(); s.display2(); s.display1(); i.display3(); i.display2(); i.display1(); } Output: Person class Student class Person class ITStudent class Student class Person class
  • 14. Unit-5 Objects and Classes A D Patel Institute of Technology 14 3. Multiple Inheritance  If a class is derived from more than one class then it is called multiple inheritance.  Here, class C is derived from two classes, class A and class B. Example: class Liquid { //content of Liquid class }; class Fuel { //content of Fuel class }; class Petrol: public Liquid, public Fuel { //content of Petrol class }; A B C
  • 15. class Liquid{ public: void display1(){ cout<<"nLiquid class"; } }; class Fuel{ public: void display2(){ cout<<"nFuel class"; } }; class Petrol:public Liquid,public Fuel{ public: void display3(){ cout<<"nPetrol class"; } }; int main() { Liquid l; Fuel f; Petrol p; l.display1(); f.display2(); p.display3(); p.display2(); p.display1(); } Output: Liquid class Fuel class Petrol class Fuel class Liquid class
  • 16. Unit-5 Objects and Classes A D Patel Institute of Technology 16 4. Hierarchical Inheritance  If one or more classes are derived from one class then it is called hierarchical inheritance.  Here, class B, class C and class D are derived from class A. Example: class Animal { //content of class Animal }; class Elephant :public Animal { //content of class Elephant }; A B C D class Horse :public Animal { //content of class Horse }; class Cow :public Animal { //content of class Cow };
  • 17. class Animal{ public: void display1(){ cout<<"nAnimal Class"; } }; class Elephant:public Animal{ public: void display2(){ cout<<"nElephant class"; } }; class Horse:public Animal{ public: void display3(){ cout<<"nHorse class"; } }; class Cow:public Animal{ public: void display4(){ cout<<"nCow class"; } }; int main(){ Animal a; Elephant e; Horse h; Cow c; a.display1(); e.display2(); e.display1(); h.display3(); h.display1(); c.display4(); c.display1(); } Output: Animal Class Elephant class Animal Class Horse class Animal Class Cow class Animal Class
  • 18. Unit-5 Objects and Classes A D Patel Institute of Technology 18 5. Hybrid Inheritance  It is a combination of any other inheritance types. That is either multiple or multilevel or hierarchical or any other combination.  Here, class B and class C are derived from class A and class D is derived from class B and class C.  class A, class B and class C is example of Hierarchical Inheritance and class B, class C and class D is example of Multiple Inheritance so this hybrid inheritance is combination of Hierarchical and Multiple Inheritance. A B C D
  • 19. Unit-5 Objects and Classes A D Patel Institute of Technology 19 Hybrid Inheritance (Cont…) class Car { //content of class Car }; class FuelCar:public Car { //content of class FuelCar }; Class ElectricCar:public Car { //content of class ElectricCar }; Class HybridCar:public FuelCar, public ElectricCar { //content of classHybridCar };
  • 20. class Car{ public: void display1(){ cout<<"nCar class"; } }; class FuelCar:public Car{ public: void display2(){ cout<<"nFuelCar class"; } }; class ElecCar:public Car{ public: void display3(){ cout<<"nElecCar class"; } }; class HybridCar:public FuelCar, public ElecCar{ public: void display4(){ cout<<"nHybridCar class"; } }; int main(){ Car c; FuelCar f; ElecCar e; HybridCar h; h.display4(); h.display3(); h.display2(); } Output: HybridCar class ElecCar class FuelCar class
  • 21. Unit-5 Objects and Classes A D Patel Institute of Technology 21 GTU Program  Create a class student that stores roll_no, name. Create a class test that stores marks obtained in five subjects. Class result derived from student and test contains the total marks and percentage obtained in test. Input and display information of a student. Attributes: roll_no name class student Attributes: marks[5] Class test Attributes: totalmarks percentage class result
  • 22. Unit-5 Objects and Classes A D Patel Institute of Technology 22 Protected access modifier  Protected access modifier plays a key role in inheritance.  Protected members of the class can be accessed within the class and from derived class but cannot be accessed from any other class or program.  It works like public for derived class and private for other class.
  • 23. class ABC { public: void setProtMemb(int i){ m_protMemb = i; } void Display(){ cout<<m_protMemb<<endl;} protected: int m_protMemb; void Protfunc(){ cout<<"nAccess allowedn";} }; class XYZ : public ABC{ public: void useProtfunc(){ Protfunc(); } }; int main() { ABC a; XYZ x; a.m_protMemb; a.setProtMemb(0); a.Display(); a.Protfunc(); x.setProtMemb(5); x.Display(); x.useProtfunc();} //error, m_protMemb is protected //OK,uses public access function //OK,uses public access function //error, Protfunc() is protected // OK, uses public access function
  • 24. Unit-5 Objects and Classes A D Patel Institute of Technology 25 Class access modifiers  public – Public members are visible to all classes.  private – Private members are visible only to the class to which they belong.  protected – Protected members are visible only to the class to which they belong, and derived classes.
  • 25. Unit-5 Objects and Classes A D Patel Institute of Technology 26 Access modifiers Base class members How inherited base class members appear in derived class private: x protected: y public: z x is inaccessible protected: y public: z public base class private: x protected:y public: z x is inaccessible private: y private: z private base class private: x protected:y public: z x is inaccessible protected: y protected: z protected base class
  • 26. class base{ private: int z; public: int x; protected: int y; }; class publicDerived: public base{ // x is public // y is protected // z is not accessible from publicDerived }; class protectedDerived: protected base{ // x is protected // y is protected // z is not accessible from protectedDerived }; class privateDerived: private base { // x is private // y is private // z is not accessible from privateDerived };
  • 27. Unit-5 Objects and Classes A D Patel Institute of Technology 28 Inheritance using Public Access private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); class Test : public Grade When Test class inherits from Grade class using public class access, it looks like this: private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); void setScore(float); float getScore(); char getLetter();
  • 28. Unit-5 Objects and Classes A D Patel Institute of Technology 29 Inheritance using Private Access private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); class Test : private Grade When Test class inherits from Grade class using private class access, it looks like this: private members: int numQuestions: float pointsEach; int numMissed; void setScore(float); float getScore(); float getLetter(); public members: Test(int, int);
  • 29. Unit-5 Objects and Classes A D Patel Institute of Technology 30 Inheritance using Protected Access private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); class Test : protected Grade When Test class inherits from Grade class using protected class access, it looks like this: private members: int numQuestions: float pointsEach; int numMissed; public members: Test(int, int); protected members: void setScore(float); float getScore(); float getLetter();
  • 30. Unit-5 Objects and Classes A D Patel Institute of Technology 31 Visibility of inherited members Base class visibility Derived class visibility Public derivation Private derivation Protected derivation Private Not inherited Not inherited Not inherited Protected Protected Private Protected Public Public Private Protected
  • 31. Unit-5 Objects and Classes A D Patel Institute of Technology 32 Function Overriding / Method Overriding  If base class and derived class have member functions with same name and arguments then method is said to be overridden and it is called function overriding or method overriding in C++.
  • 32. class ABC { public: void display(){ cout<<"This is parent class"; } }; class XYZ:public ABC{ public: void display(){//overrides the display()mehtod of class ABC cout<<"nThis is child class"; } }; int main(){ XYZ x; x.display();//method of class XYZ invokes, instead of class ABC x.ABC::display(); }
  • 33. Unit-5 Objects and Classes A D Patel Institute of Technology 34 Virtual Base Class A memberA B memberA C memberA D memberA Multiple copies of member A we can prevent multiple copies of the base class by declaring the base class as virtual when it is being inherited.
  • 34. Unit-5 Objects and Classes A D Patel Institute of Technology 35 Virtual base class (Cont…)  Virtual base class is used to prevent the duplication/ambiguity.  In hybrid inheritance child class has two direct parents which themselves have a common base class.  So, the child class inherits the grandparent via two separate paths. it is also called as indirect parent class.  All the public and protected member of grandparent are inherited twice into child.  We can stop this duplication by making base class virtual.
  • 35. class A { public: int i; }; class B:virtual public A { public: int j; }; class C: public virtual A { public: int k; }; class D:public B, public C { public: int sum; }; int main() { D ob1; ob1.i=10; ob1.j=20; ob1.k=30; ob1.sum=ob1.i+ob1.j+ob1.k; cout<<ob1.sum; }
  • 36. Unit-5 Objects and Classes A D Patel Institute of Technology 37 Derived class constructor class Base{ int x; public: Base() { cout << "Base default constrictors"; } }; class Derived : public Base { int y; public: Derived() { cout<<"Derived default constructor"; } Derived(int i) { cout<<"Derived parameterized constructor";} }; int main(){ Base b; Derived d1; Derived d2(10); }
  • 37. Unit-5 Objects and Classes A D Patel Institute of Technology 38 Derived class constructor (Cont…) class Base { int x; public: Base(int i){ x = i; cout << "x="<<x; } }; class Derived : public Base { int y; public: Derived(int i,int j) : Base(j) { y = i; cout << "y="<<y; } }; int main() { Derived d(10,20) ; }
  • 38. Unit-5 Objects and Classes A D Patel Institute of Technology 39 Execution of base class constructor Method of inheritance Order of execution class Derived: public Base { }; Base(); Derived(); class C: public A, public B { }; A();//base(first) B();//base(Second) C();derived class C:public A, virtual public B { }; B();//virtual base A();//base C();derived