SlideShare a Scribd company logo
Inheritance
10-2
What is Inheritance?
Generalization vs. Specialization
• Real-life objects are typically specialized versions of
other more general objects.
• The term “insect” describes a very general type of
creature with numerous characteristics.
• Grasshoppers and bumblebees are insects
– They share the general characteristics of an insect.
– However, they have special characteristics of their own.
• grasshoppers have a jumping ability, and
• bumblebees have a stinger.
• Grasshoppers and bumblebees are specialized
versions of an insect.
Inheritance
10-4
The “is a” Relationship
• The relationship between a superclass and an inherited
class is called an “is a” relationship.
– A grasshopper “is a” insect.
– A poodle “is a” dog.
– A car “is a” vehicle.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used to
create an “is a” relationship among classes.
10-5
The “is a” Relationship
• We can extend the capabilities of a class.
• Inheritance involves a superclass and a subclass.
– The superclass is the general class and
– the subclass is the specialized class.
• The subclass is based on, or extended from, the superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as parent
classes and child classes.
10-6
Inheritance, Fields and Methods
• Members of the superclass that are marked private:
– are not inherited by the subclass,
– exist in memory when the object of the subclass is created
– may only be accessed from the subclass by public methods
of the superclass.
• Members of the superclass that are marked public:
– are inherited by the subclass, and
– may be directly accessed from the subclass.
Inheritance
• It is the Process of creating a New class from an existing class.
• The Existing class is called Base or Parent class.
• The New class is called as Child or Derived Class
base
subclass1 subclass2
Derived class
Base Class
Methods
and
Properties
Base class methods
+
Additional methods
Inheritance is the property that allows the
reuse of an existing class to build a new class
Advantages
 It permits code reusability. So, save time and
increase the program reliability.
 Improve Program Reliability
 It Permits code sharing
Syntax for Inheritance
class <derived classname > : visibility mode <base classname >
{
---
----
};
visibility mode
• The visibility mode specifies whether the features are
privately derived or publicly derived.
• The default visibility mode is private.
• Privately Inherited: public members of the base class
become private members of the derived class, can be
accessed only by the member functions of the derived
class.
• Publicly inherited: public members of the base class
become public members of the derived class and can be
accessed by the derived class objects.
• Private members of the base class are not inherited in
both the cases.
Inheritance
Multilevel
Inheritance
Single Inheritance
Multiple Inheritance
Hierarchical
Inheritance
Single Inheritance
A class can be derived from a single base class is called single
inheritance
Person
Employee
• #include <iostream>
• using namespace std;
• // Base class
• class Shape {
• public:
• void setWidth(int w) {
• width = w;
• }
• void setHeight(int h) {
• height = h;
• }
• protected:
• int width;
• int height;
• };
• // Derived class
• class Rectangle : public
Shape {
• public:
• int getArea() {
• return (width *
height);
• }
• };
• int main(void) {
• Rectangle Rect;
• Rect.setWidth(5);
• Rect.setHeight(7);
• // Print the area of the object.
• cout << "Total area: " << Rect.getArea() <<
endl;
• return 0;
• }
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
Visibility of inherited members
10.Inheritance.ppt for oops programinggg
Access control to the members of a
class
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
Multilevel
Inheritance
Vehicle
Car
Racing car
Person
Employee
Part time
Employee
A class be can derived from a derived class which is known as multilevel
inheritance.
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
// members;
public :
//memebers;
};
Multiple Inheritance
Person Employee
Teacher
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
Same Data Member Name in
Base and Derived Class
Single level inheretance
• #include <iostream>
• using namespace std;
• class ClassA
• {
• protected :
• int width, height;
• public :
• void set_values(int x, int y)
• {
• width = x;
• height = y;
• }
• };
• class ClassC:public ClassA
• {
• protected:
• int width, height;
•
• public :
• int area()
• {
• return (width *height);
• }
• };
Same Data Member Name in Base
and Derived Class
• int main()
• {
• ClassC Obj;
• Obj.set_values(10, 20);
• cout << Obj.area() <<
endl;
• return 0;
• }
• Output: 0
• #include <iostream>
• using namespace std;
• class ClassA
• {
• protected :
• int width, height;
• public :
• void set_values(int x, int y)
• {
• width = x;
• height = y;
• }
• };
• class ClassC:public ClassA
• {
• protected:
• int width, height;
•
• public :
• int area()
• {
• return (ClassA::width
*ClassA::height);
• }
• };
Same Data Member Name in Base
and Derived Class
• int main()
• {
• ClassC Obj;
• Obj.set_values(10, 20);
• cout << Obj.area() <<
endl;
• return 0;
• }
• Output: 200
Same Data Member Name in
Base and Derived Class
Multiple inheretance
• #include <iostream>
• using namespace std;
• class person
• {
• protected:
• int regno;
• string name;
• };
• class student
• {
• protected:
• string name;
• int regno;
• };
• class studentgrade:public
person,public student
• {
• private:
• string grade;
• public:
• void details()
• {
• cin>>name;
• cin>>regno;
• }
• void display()
• {
• cout<<name;
• cout<<regno;
• }
• };
Same Data Member Name in Base
and Derived Class
• int main()
• {
• studentgrade a;
•
• a.details();
• a.display();
• }
• error: reference to
'name' is ambiguous
25 | cin>>name;
|
^~~~/tmp/TQ6cOvUM4
U.cpp:13:15: note:
candidates are:
'std::string
student::name' 13 |
string name;
• #include <iostream>
• using namespace std;
• class person
• {
• protected:
• int regno;
• string name;
• };
• class student
• {
• protected:
• string name;
• int regno;
• };
• class studentgrade:public
person,public student
• {
• private:
• string grade;
• public:
• void details()
• {
• cin>>person::name;
• cin>>person::regno;
• }
• void display()
• {
• cout<<person::name;
• cout<<person::regno;
Same Data Member Name in Base
and Derived Class
• int main()
• {
• studentgrade a;
•
• a.details();
• a.display();
• }
• saira
• 23
• saira23
Hierarchical Inheritance
Employee
Permanent
Employee
Temporary
Employee
If more than one class is inherited from a base class, it's known as hierarchical
inheritance. In general, all features that are common in child classes are included
in base class in hierarchical inheritance.
Employee: Data members: id and name
Permanent Employee: designation and department, getdetails()
and displaydetails()
Temporary employee: no.of working day and contrctor name,
getdetails and displaydetails
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
Hybrid Inheritance
The process of combining more than one type
of Inheritance together while deriving
subclasses in a program is called a Hybrid
Inheritance.
Person: name and age[protected], get() and print()
Student: rollno and marks[private], get() and print()
Gate Score: gatescore[private], get() and print()
PG-Student :dept_name[private]. get() and print()
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
10.Inheritance.ppt for oops programinggg
Ad

More Related Content

Similar to 10.Inheritance.ppt for oops programinggg (20)

11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
Aryan's pres. entation.pptx
Aryan's pres.               entation.pptxAryan's pres.               entation.pptx
Aryan's pres. entation.pptx
mohitsinha7739289047
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
amal68766
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
haider ali
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
Chandan Gupta Bhagat
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
VB.net&OOP.pptx
VB.net&OOP.pptxVB.net&OOP.pptx
VB.net&OOP.pptx
BharathiLakshmiAAssi
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
Tanmay Dhatrak
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
AteeqaKokab1
 
inheritance
   inheritance   inheritance
inheritance
krishna partiwala
 
27c
27c27c
27c
Sireesh K
 
27csharp
27csharp27csharp
27csharp
Sireesh K
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
MASQ Technologies
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
RAJ KUMAR
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
rayanbabur
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
amal68766
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
Arslan Waseem
 
Final presentation programming
Final presentation programmingFinal presentation programming
Final presentation programming
haider ali
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
WaqarRaj1
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
MASQ Technologies
 

More from sanketkashyap2023 (8)

18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx
sanketkashyap2023
 
19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh
sanketkashyap2023
 
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
sanketkashyap2023
 
15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg
sanketkashyap2023
 
9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp
sanketkashyap2023
 
4.Constructor.pptx for oops programming language
4.Constructor.pptx for oops programming language4.Constructor.pptx for oops programming language
4.Constructor.pptx for oops programming language
sanketkashyap2023
 
3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language
sanketkashyap2023
 
2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps
sanketkashyap2023
 
18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx18.Static Data Members and Static member function.pptx
18.Static Data Members and Static member function.pptx
sanketkashyap2023
 
19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh19.Virtual Base class.pptx kiujytfghjgfcghjkh
19.Virtual Base class.pptx kiujytfghjgfcghjkh
sanketkashyap2023
 
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
25.Pointers.pptx ihuhkhkhuhkhnuhuhkjihuhjhu
sanketkashyap2023
 
15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg15.Friend Function.pptx oops using cpp prg
15.Friend Function.pptx oops using cpp prg
sanketkashyap2023
 
9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp9.Manipulators.pptx oops programming in c pp
9.Manipulators.pptx oops programming in c pp
sanketkashyap2023
 
4.Constructor.pptx for oops programming language
4.Constructor.pptx for oops programming language4.Constructor.pptx for oops programming language
4.Constructor.pptx for oops programming language
sanketkashyap2023
 
3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language3.Syntax.pptx for oops programing language
3.Syntax.pptx for oops programing language
sanketkashyap2023
 
2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps2.Pillars of OOPS.pptx oops opspspspspspps
2.Pillars of OOPS.pptx oops opspspspspspps
sanketkashyap2023
 
Ad

Recently uploaded (20)

录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
Mining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - MicrosoftMining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - Microsoft
Process mining Evangelist
 
real illuminati Uganda agent 0782561496/0756664682
real illuminati Uganda agent 0782561496/0756664682real illuminati Uganda agent 0782561496/0756664682
real illuminati Uganda agent 0782561496/0756664682
way to join real illuminati Agent In Kampala Call/WhatsApp+256782561496/0756664682
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Process Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBSProcess Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBS
Process mining Evangelist
 
Transforming health care with ai powered
Transforming health care with ai poweredTransforming health care with ai powered
Transforming health care with ai powered
gowthamarvj
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docxAnalysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
hershtara1
 
AWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdfAWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdf
philsparkshome
 
Feature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record SystemsFeature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record Systems
Process mining Evangelist
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
Automated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptxAutomated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptx
handrymaharjan23
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
Understanding Complex Development Processes
Understanding Complex Development ProcessesUnderstanding Complex Development Processes
Understanding Complex Development Processes
Process mining Evangelist
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfjOral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
maitripatel5301
 
hersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distributionhersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distribution
hershtara1
 
Adopting Process Mining at the Rabobank - use case
Adopting Process Mining at the Rabobank - use caseAdopting Process Mining at the Rabobank - use case
Adopting Process Mining at the Rabobank - use case
Process mining Evangelist
 
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
Taqyea
 
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
disnakertransjabarda
 
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
录取通知书加拿大TMU毕业证多伦多都会大学电子版毕业证成绩单
Taqyea
 
Mining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - MicrosoftMining a Global Trade Process with Data Science - Microsoft
Mining a Global Trade Process with Data Science - Microsoft
Process mining Evangelist
 
Dynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics DynamicsDynamics 365 Business Rules Dynamics Dynamics
Dynamics 365 Business Rules Dynamics Dynamics
heyoubro69
 
Process Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBSProcess Mining and Official Statistics - CBS
Process Mining and Official Statistics - CBS
Process mining Evangelist
 
Transforming health care with ai powered
Transforming health care with ai poweredTransforming health care with ai powered
Transforming health care with ai powered
gowthamarvj
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docxAnalysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
hershtara1
 
AWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdfAWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdf
philsparkshome
 
Feature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record SystemsFeature Engineering for Electronic Health Record Systems
Feature Engineering for Electronic Health Record Systems
Process mining Evangelist
 
AWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptxAWS RDS Presentation to make concepts easy.pptx
AWS RDS Presentation to make concepts easy.pptx
bharatkumarbhojwani
 
Automated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptxAutomated Melanoma Detection via Image Processing.pptx
Automated Melanoma Detection via Image Processing.pptx
handrymaharjan23
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfjOral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
maitripatel5301
 
hersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distributionhersh's midterm project.pdf music retail and distribution
hersh's midterm project.pdf music retail and distribution
hershtara1
 
Adopting Process Mining at the Rabobank - use case
Adopting Process Mining at the Rabobank - use caseAdopting Process Mining at the Rabobank - use case
Adopting Process Mining at the Rabobank - use case
Process mining Evangelist
 
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
新西兰文凭奥克兰理工大学毕业证书AUT成绩单补办
Taqyea
 
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
indonesia-gen-z-report-2024 Gen Z (born between 1997 and 2012) is currently t...
disnakertransjabarda
 
Ad

10.Inheritance.ppt for oops programinggg

  • 2. 10-2 What is Inheritance? Generalization vs. Specialization • Real-life objects are typically specialized versions of other more general objects. • The term “insect” describes a very general type of creature with numerous characteristics. • Grasshoppers and bumblebees are insects – They share the general characteristics of an insect. – However, they have special characteristics of their own. • grasshoppers have a jumping ability, and • bumblebees have a stinger. • Grasshoppers and bumblebees are specialized versions of an insect.
  • 4. 10-4 The “is a” Relationship • The relationship between a superclass and an inherited class is called an “is a” relationship. – A grasshopper “is a” insect. – A poodle “is a” dog. – A car “is a” vehicle. • A specialized object has: – all of the characteristics of the general object, plus – additional characteristics that make it special. • In object-oriented programming, inheritance is used to create an “is a” relationship among classes.
  • 5. 10-5 The “is a” Relationship • We can extend the capabilities of a class. • Inheritance involves a superclass and a subclass. – The superclass is the general class and – the subclass is the specialized class. • The subclass is based on, or extended from, the superclass. – Superclasses are also called base classes, and – subclasses are also called derived classes. • The relationship of classes can be thought of as parent classes and child classes.
  • 6. 10-6 Inheritance, Fields and Methods • Members of the superclass that are marked private: – are not inherited by the subclass, – exist in memory when the object of the subclass is created – may only be accessed from the subclass by public methods of the superclass. • Members of the superclass that are marked public: – are inherited by the subclass, and – may be directly accessed from the subclass.
  • 7. Inheritance • It is the Process of creating a New class from an existing class. • The Existing class is called Base or Parent class. • The New class is called as Child or Derived Class base subclass1 subclass2
  • 8. Derived class Base Class Methods and Properties Base class methods + Additional methods Inheritance is the property that allows the reuse of an existing class to build a new class
  • 9. Advantages  It permits code reusability. So, save time and increase the program reliability.  Improve Program Reliability  It Permits code sharing
  • 10. Syntax for Inheritance class <derived classname > : visibility mode <base classname > { --- ---- };
  • 11. visibility mode • The visibility mode specifies whether the features are privately derived or publicly derived. • The default visibility mode is private. • Privately Inherited: public members of the base class become private members of the derived class, can be accessed only by the member functions of the derived class. • Publicly inherited: public members of the base class become public members of the derived class and can be accessed by the derived class objects. • Private members of the base class are not inherited in both the cases.
  • 13. Single Inheritance A class can be derived from a single base class is called single inheritance Person Employee
  • 14. • #include <iostream> • using namespace std; • // Base class • class Shape { • public: • void setWidth(int w) { • width = w; • } • void setHeight(int h) { • height = h; • } • protected: • int width; • int height; • }; • // Derived class • class Rectangle : public Shape { • public: • int getArea() { • return (width * height); • } • };
  • 15. • int main(void) { • Rectangle Rect; • Rect.setWidth(5); • Rect.setHeight(7); • // Print the area of the object. • cout << "Total area: " << Rect.getArea() << endl; • return 0; • }
  • 20. Access control to the members of a class
  • 23. Multilevel Inheritance Vehicle Car Racing car Person Employee Part time Employee A class be can derived from a derived class which is known as multilevel inheritance.
  • 26. It is the process of creating new class from more than one base classes. Syntax : class <derived class >:<access specifier> base_class1,<access specifier> base_class2... { private : // members; protected : // members; public : //memebers; }; Multiple Inheritance Person Employee Teacher
  • 29. Same Data Member Name in Base and Derived Class Single level inheretance
  • 30. • #include <iostream> • using namespace std; • class ClassA • { • protected : • int width, height; • public : • void set_values(int x, int y) • { • width = x; • height = y; • } • }; • class ClassC:public ClassA • { • protected: • int width, height; • • public : • int area() • { • return (width *height); • } • };
  • 31. Same Data Member Name in Base and Derived Class • int main() • { • ClassC Obj; • Obj.set_values(10, 20); • cout << Obj.area() << endl; • return 0; • } • Output: 0
  • 32. • #include <iostream> • using namespace std; • class ClassA • { • protected : • int width, height; • public : • void set_values(int x, int y) • { • width = x; • height = y; • } • }; • class ClassC:public ClassA • { • protected: • int width, height; • • public : • int area() • { • return (ClassA::width *ClassA::height); • } • };
  • 33. Same Data Member Name in Base and Derived Class • int main() • { • ClassC Obj; • Obj.set_values(10, 20); • cout << Obj.area() << endl; • return 0; • } • Output: 200
  • 34. Same Data Member Name in Base and Derived Class Multiple inheretance
  • 35. • #include <iostream> • using namespace std; • class person • { • protected: • int regno; • string name; • }; • class student • { • protected: • string name; • int regno; • }; • class studentgrade:public person,public student • { • private: • string grade; • public: • void details() • { • cin>>name; • cin>>regno; • } • void display() • { • cout<<name; • cout<<regno; • } • };
  • 36. Same Data Member Name in Base and Derived Class • int main() • { • studentgrade a; • • a.details(); • a.display(); • } • error: reference to 'name' is ambiguous 25 | cin>>name; | ^~~~/tmp/TQ6cOvUM4 U.cpp:13:15: note: candidates are: 'std::string student::name' 13 | string name;
  • 37. • #include <iostream> • using namespace std; • class person • { • protected: • int regno; • string name; • }; • class student • { • protected: • string name; • int regno; • }; • class studentgrade:public person,public student • { • private: • string grade; • public: • void details() • { • cin>>person::name; • cin>>person::regno; • } • void display() • { • cout<<person::name; • cout<<person::regno;
  • 38. Same Data Member Name in Base and Derived Class • int main() • { • studentgrade a; • • a.details(); • a.display(); • } • saira • 23 • saira23
  • 39. Hierarchical Inheritance Employee Permanent Employee Temporary Employee If more than one class is inherited from a base class, it's known as hierarchical inheritance. In general, all features that are common in child classes are included in base class in hierarchical inheritance. Employee: Data members: id and name Permanent Employee: designation and department, getdetails() and displaydetails() Temporary employee: no.of working day and contrctor name, getdetails and displaydetails
  • 42. Hybrid Inheritance The process of combining more than one type of Inheritance together while deriving subclasses in a program is called a Hybrid Inheritance.
  • 43. Person: name and age[protected], get() and print() Student: rollno and marks[private], get() and print() Gate Score: gatescore[private], get() and print() PG-Student :dept_name[private]. get() and print()
  翻译: