SlideShare a Scribd company logo
1 of 38
An Introduction To Software
Development Using C++
Class #22:
Inheritance, Part 1
Classes Creating New Classes
• Classes can create new classes from existing classes.
• This is a powerful feature because it encourages code
reuse.
• In C++ you can relate two or more classes in more than one
way.
• Two common ways to relate classes in a meaningful way
are:
– Inheritance (“is-a” relationship)
– Composition (aggregation)
(“has-a” relationship)
Image Credit: www.svendirect.com
Inheritance
• Let’s create a class called partTimeEmployee
so that we can implement and process the
characteristics of a part time employee.
• Main features of a part-time employee:
– Name
– Pay rate
– # hours worked
Image Credit: www.mccainortho.com
Inheritance
• Let us assume that we’ve already created a
class to be able to remember a person’s name.
• Rather than design the class
partTimeEmployee from scratch we want to
be able to extend the definition of the class
personType by adding additional data and / or
methods.
Image Credit: www.wikihow.com
Inheritance
• Somebody else may be using personType so we don’t want
to make the necessary changes to the class personType
directly (e.g. edit personType and edit / delete parts of it).
• We want to create the class partTimeEmployee without
making any physical changes to the class personType by
adding only the data and methods that are necessary.
• As an example of this, the class personType already has
methods to store the first name and the last name. We
won’t have to create such methods for the class
partTimeEmployee. The reason that we won’t
is because we’ll inherit these from personType.
Image Credit: en.hdyo.org
Inheritance – Another Example
• Assume that we’ve created a class called clockType that we’ve used to
implement the time of day in a program.
• The class clockType has three member variables to store the hours,
minutes, and seconds.
• Our new application, in addition to hours, minutes and seconds, needs to
know the time zone.
• This means that we’d like to extend the definition of clockType to now
include this new information by creating a new class extClockType.
• What this means is that we want to derive
class extClockType by adding a variable,
timeZone, and the necessary methods to
manipulate the time.
Image Credit: www.amazon.com
Inheritance
• In both the partTimeEmployee and the
extClockTime cases, C++ allows us to extend the
definition of an existing class to create a new
class.
• What allows us to accomplish this is called the
principal of inheritance.
• Inheritance is a “is-a” relationship.
Example: every employee is a
person.
Image Credit: www.cunningbailey.com
Inheritance
• Inheritance allows us to create new classes from existing classes.
• The new classes that we create from existing classes are called the
derived classes.
• The existing classes are called the base classes.
• The derived classes inherit the properties of the base classes.
• Rather than create completely new classes
from scratch, we can now take advantage
of inheritance and reduce software
development complexity.
Image Credit: gailbrenner.com
Inheritance
• Each derived class can, in turn, then become a base
class for a future derived class.
• Inheritance can either be a single inheritance or a
multiple inheritance.
• In single inheritance, the derived class is derived from
a single base class.
• In multiple inheritance, the
derived class is derived from
multiple base classes.
Image Credit: www.swiftshift.com
Inheritance
• Inheritance can be viewed as being a treelike or hierarchical
structure. Here’s an example:
• Shape is the base class. The classes circle and rectangle are
derived from shape, and the class square is derived from
the class rectangle.
• Every circle and every rectangle is a shape.
Every square is a rectangle.
Image Credit: en.wikipedia.org
Inheritance
• General syntax of a derived class is:
• memberAccessSpecifier will be public,
protected, or private. If it is not specified,
then it is assumed to be a private inheritance.
Image Credit: www.canstockphoto.com
Public / Private Inheritance
• Assume that we have a class called shape. These statements specify that the class
circle is derived from the class shape and it is a public inheritance:
• On the other hand, consider the following definition of class circle:
• This is a private inheritance. In this definition, the public members of shape
become private members of the class circle.
1 Image Credit: www.forensicmag.com
Public / Private Inheritance
• The previous definition of circle is equivalent to:
• What this means is that if we don’t use either the
memberAccessSpecifier public or private, the
public members of a base class are inherited as
private members by default.
Image Credit: www.huffingtonpost.ca
Facts About Base & Derived Classes
1. The private members of a base class remain private to the
base class; hence, the members of the derived class
cannot directly access them. In other words, when you
write the definitions of the member functions of the
derived class, even though the private members of the
base class are members of the derived class, the derived
class cannot directly access them.
2. The public members of the base class can be inherited as
either public members or private members by the derived
class. This means that what were public members in the
base class can now become either public or private
members in the derived class.
Image Credit: www.123rf.com
Facts About Base & Derived Classes
3. The derived class can contain additional data and or
functions.
4. The derived class can redefine the public member functions
of the base class. That is, in the derived class you can have a
member function with the same name, number and type of
parameters as a function in the base class, but with different
code in the function body. This redefinition will only apply to
objects of the derived class, not to objects of the base class.
Image Credit: www.featurepics.com
Facts About Base & Derived Classes
5. All member variables of the base class are
also member variables of the derived class.
Similarly, the member functions of the base
class are also members of the derived class.
Image Credit: www.liveboxcloud.com
Redefining / Overriding Member
Functions Of the Base Class
• Let us assume that the class derivedClass was
derived from the class baseClass.
• Assume that both derivedClass and
baseClass have some member variables.
• It follows that the member variables of the class
derivedClass are its own member variables,
together with the member variables of baseClass.
Image Credit: nicodewet.com
Redefining / Overriding Member
Functions Of the Base Class
• Suppose that baseClass contains a method print
that prints the values of the variables of
baseClass.
• Now derivedClass contains more variables
than just those inherited from baseClass.
• Suppose that you want to include a method in
derivedClass that will print the variables in
derivedClass.
Image Credit: en.soft-xpansion.eu
Redefining / Overriding Member
Functions Of the Base Class
• You can give any name to this new method.
• However, in the class derivedClass you can
also call this new method print – the same
name used by baseClass.
• This is called redefining or
overriding the method of
the base class.
Image Credit: inkondapaper.com
Redefining / Overriding Member
Functions Of the Base Class
• Note: To redefine a public method of a base
class in the derived class, the corresponding
class in the derived class must have the same
name, number and types of parameters.
Image Credit: www.abc.net.au
Example: rectangleType & boxType
• Class boxType is derived from class
rectangleType and it is a public inheritance.
• All public members of class rectangleType are
public members of class boxType.
• The class boxType also overrides / redefines
the functions print and area.
1 Image Credit: globe-views.com
How Can A Derived Class Call A Public
Method In The Base Class?
• If the derived class overrides a public method of the
base class, then to specify a call to that public member
function of the base class , you use the name of the
base class, followed by the scope resolution operator ,
::, followed by the function name with the appropriate
parameter list. Example: to call the area method of the
class rectangleType the statement is:
rectangleType::area()
• If the derived class does not override a public method
of the base class, you may specify a call to that public
method by using the name of the function and the
appropriate parameter list
Image Credit: thornleyfallis.com
Creating The Method Print Of
The Class boxType
• The class boxType has three variables: length, width, and height.
The purpose of the print method is to print the current value of
these 3 variables.
• We need to realize the following:
– The variables length and width are private members of class
rectangleType. This means that they cannot be directly accessed in
class boxType. Therefore in the boxType version of print we cannot
access these variables directly.
– The variables length and width of class rectangleType are accessible
by the class boxType only through the public methods of the class
rectangleType. This means that the print method of boxType will have
to call the print method of rectangleType to print the values of length
and width. We then print height.
Image Credit: www.nuttall-packaging.co.uk
Creating The Method Print Of The
Class boxType
• From the class boxType, if we want to call the
print method associated with rectangleType,
we will need to use the following statement:
rectangleType::print()
• This statement will ensure that we call the
print method associated with the class
rectangleType and not the one associated
with the class boxType.
Image Credit: www.fedex.com
Creating The Method Print Of The
Class boxType
• The definition of the method print for the
class boxType is:
void print ()
{
rectangleType::print();
cout << “Height = “ << height;
}
Image Credit: www.iconarchive.com
Creating The Method area Of
The Class boxType
• The method area of boxType calculates the surface area of a box.
• To calculate the surface area of a box we need access to the length
and width which are declared as private in class rectangleType.
• This means that we’ll have to use methods getLength and getWidth
in class rectangleType to get these values.
• Because class boxType does not have any
methods called getLenght and getWidth,
we can call these methods directly without
having to specify the base class.
Image Credit: calibermag.org
Creating The Method area Of
The Class boxType
• The definition of the boxType function area is:
double area();
{
return 2 * (getLength() * getWidth()
+ getLength() * height
+ getWidth() * height);
}
Image Credit: www.kauerguitars.com
Creating The Method volume Of
The Class boxType
• The method volume of boxType determines the volume of a box.
• One way to determine volume is to multiple the area of the base of
a box by its height.
• Use the method of the base class
rectangleType to calculate the area
of the base.
• Because the class boxType overrides the method area, to specify a
call to the method area of class rectangleType we use the name of
the base class and the scope resolution operator.
Image Credit: www.esupplystore.com
Creating The Method area Of
The Class boxType
• The definition of the boxType function
volume is:
double volume()
{
return rectangleType::area() * height;
}
Image Credit: movingboxestoronto.net
Constructors of Derived And
Base Classes
• A derived class can have it’s own private variables. This
means that a derived class needs to include its own
constructors in order to initialize them.
• When we declare a derived class object, this object
inherits the variables from the base class.
• The derived class cannot directly access the private
variables of the base class. Methods of the derived
class also cannot access these private variables.
Image Credit: www.pronto1.com
Constructors of Derived And
Base Classes
• What this means is that the constructors of a
derived class can directly initialize only the public
variables inherited from the base class.
• This means that when a derived object is created,
it will need to trigger the constructor of the base
class.
• The triggering of the base class’ constructor is
specified in the heading of the definition of a
derived class constructor.
Image Credit: www.itcuties.com
boxType Constructor
• Definition of the default constructor for
boxType:
boxType()
{
height = 0.0;
}
Image Credit: www.dreamstime.com
boxType Constructor With Parameters
• We first write the boxType constructor heading
including all of the parameters needed for both the
base class and the derived class constructors (i.e. all
parameters needed for rectangleType and boxType).
• In order to trigger the execution of the base class
constructor with the parameters, we add a “:” to the
heading followed by the name of the constructor of
the base class with its parameters in the heading of the
definition of the constructor of the derived class.
Image Credit: www.dreamstime.com
Definition of the Constructor With
Parameters of the Class boxType
boxType (double l, double, w, double h) : rectangleType(l,w)
{
if (h>=0)
height = h;
else
height = 0;
}
• In this definition we specify the constructor of rectangleType with
two parameters. When this constructor of boxType executes, it
triggers the execution of constructor of class rectangleType with
two parameters of type double.
Image Credit: www.dreamstime.com
Using Constructors
• Consider the following two statements:
rectangleType myRectangle (5.0,3.0);
boxType myBox (6.0, 5.0, 4.0);
• The first statement creates the rectangleType object myRectangle.
• The object myRectangle has two variables length and width.
• The second statement creates the boxType object myBox
• The object myBox has three variables length, width, and height.
Image Credit: www.jvkconstructors.com
2-Part In Class Programming Challenge
• Bob’s lawn care store specializes in putting up fences around lawns
and fertilizing the lawns. Assume that yards are rectangular. In
order to put up the fence, the program needs to know the
parameter and to fertilize it needs to know the area. Write a
program that uses the class rectangleType to store the dimension
of a yard, the cost per foot to put up the fence, and the cost per
square foot to fertilize the yard. The program then outputs the cost
of putting up the fence and fertilizing the yard.
• Sue’s gift store wraps small packages. Assume that each gift is in a
box that has a length, width, and height. Write a program that uses
the class boxType to store the dimensions of a package. The user
inputs the dimensions of the package and the cost per square foot
to wrap the package. The program will then output the cost to wrap
the package. Minimum cost of wrapping a package is $1.00.
Assume: Length = 70, Width = 50, Cost of fence = $10, Cost of fertilizer = 0.25
Pkg Length = 3, Pkg width = 2, Pkg height = 0.25, Cost of wrapping = 0.25
Image Credit: www.dreamstime.com
The Answer!
What’s In Your C++ Toolbox?
cout / cin #include if/else/
Switch
Math Class String getline While
For do…While Break /
Continue
Arrays Functions

More Related Content

What's hot (17)

OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
B.sc CSIT 2nd semester C++ Unit5
B.sc CSIT  2nd semester C++ Unit5B.sc CSIT  2nd semester C++ Unit5
B.sc CSIT 2nd semester C++ Unit5
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
java tr.docx
java tr.docxjava tr.docx
java tr.docx
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
How would you implement multiple inheritance in java
How would you implement multiple inheritance in javaHow would you implement multiple inheritance in java
How would you implement multiple inheritance in java
 
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
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Ch2
Ch2Ch2
Ch2
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 

Similar to Intro To C++ - Class #22: Inheritance, Part 1

Similar to Intro To C++ - Class #22: Inheritance, Part 1 (20)

Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
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
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
full defination of final opp.pptx
full defination of final opp.pptxfull defination of final opp.pptx
full defination of final opp.pptx
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Unit 7 inheritance
Unit 7 inheritanceUnit 7 inheritance
Unit 7 inheritance
 
Classes2
Classes2Classes2
Classes2
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
C++Presentation 2.PPT
C++Presentation 2.PPTC++Presentation 2.PPT
C++Presentation 2.PPT
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
OOPs Lecture 2
OOPs Lecture 2OOPs Lecture 2
OOPs Lecture 2
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
OOP
OOPOOP
OOP
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Oops
OopsOops
Oops
 

Recently uploaded

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 

Recently uploaded (20)

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 

Intro To C++ - Class #22: Inheritance, Part 1

  • 1. An Introduction To Software Development Using C++ Class #22: Inheritance, Part 1
  • 2. Classes Creating New Classes • Classes can create new classes from existing classes. • This is a powerful feature because it encourages code reuse. • In C++ you can relate two or more classes in more than one way. • Two common ways to relate classes in a meaningful way are: – Inheritance (“is-a” relationship) – Composition (aggregation) (“has-a” relationship) Image Credit: www.svendirect.com
  • 3. Inheritance • Let’s create a class called partTimeEmployee so that we can implement and process the characteristics of a part time employee. • Main features of a part-time employee: – Name – Pay rate – # hours worked Image Credit: www.mccainortho.com
  • 4. Inheritance • Let us assume that we’ve already created a class to be able to remember a person’s name. • Rather than design the class partTimeEmployee from scratch we want to be able to extend the definition of the class personType by adding additional data and / or methods. Image Credit: www.wikihow.com
  • 5. Inheritance • Somebody else may be using personType so we don’t want to make the necessary changes to the class personType directly (e.g. edit personType and edit / delete parts of it). • We want to create the class partTimeEmployee without making any physical changes to the class personType by adding only the data and methods that are necessary. • As an example of this, the class personType already has methods to store the first name and the last name. We won’t have to create such methods for the class partTimeEmployee. The reason that we won’t is because we’ll inherit these from personType. Image Credit: en.hdyo.org
  • 6. Inheritance – Another Example • Assume that we’ve created a class called clockType that we’ve used to implement the time of day in a program. • The class clockType has three member variables to store the hours, minutes, and seconds. • Our new application, in addition to hours, minutes and seconds, needs to know the time zone. • This means that we’d like to extend the definition of clockType to now include this new information by creating a new class extClockType. • What this means is that we want to derive class extClockType by adding a variable, timeZone, and the necessary methods to manipulate the time. Image Credit: www.amazon.com
  • 7. Inheritance • In both the partTimeEmployee and the extClockTime cases, C++ allows us to extend the definition of an existing class to create a new class. • What allows us to accomplish this is called the principal of inheritance. • Inheritance is a “is-a” relationship. Example: every employee is a person. Image Credit: www.cunningbailey.com
  • 8. Inheritance • Inheritance allows us to create new classes from existing classes. • The new classes that we create from existing classes are called the derived classes. • The existing classes are called the base classes. • The derived classes inherit the properties of the base classes. • Rather than create completely new classes from scratch, we can now take advantage of inheritance and reduce software development complexity. Image Credit: gailbrenner.com
  • 9. Inheritance • Each derived class can, in turn, then become a base class for a future derived class. • Inheritance can either be a single inheritance or a multiple inheritance. • In single inheritance, the derived class is derived from a single base class. • In multiple inheritance, the derived class is derived from multiple base classes. Image Credit: www.swiftshift.com
  • 10. Inheritance • Inheritance can be viewed as being a treelike or hierarchical structure. Here’s an example: • Shape is the base class. The classes circle and rectangle are derived from shape, and the class square is derived from the class rectangle. • Every circle and every rectangle is a shape. Every square is a rectangle. Image Credit: en.wikipedia.org
  • 11. Inheritance • General syntax of a derived class is: • memberAccessSpecifier will be public, protected, or private. If it is not specified, then it is assumed to be a private inheritance. Image Credit: www.canstockphoto.com
  • 12. Public / Private Inheritance • Assume that we have a class called shape. These statements specify that the class circle is derived from the class shape and it is a public inheritance: • On the other hand, consider the following definition of class circle: • This is a private inheritance. In this definition, the public members of shape become private members of the class circle. 1 Image Credit: www.forensicmag.com
  • 13. Public / Private Inheritance • The previous definition of circle is equivalent to: • What this means is that if we don’t use either the memberAccessSpecifier public or private, the public members of a base class are inherited as private members by default. Image Credit: www.huffingtonpost.ca
  • 14. Facts About Base & Derived Classes 1. The private members of a base class remain private to the base class; hence, the members of the derived class cannot directly access them. In other words, when you write the definitions of the member functions of the derived class, even though the private members of the base class are members of the derived class, the derived class cannot directly access them. 2. The public members of the base class can be inherited as either public members or private members by the derived class. This means that what were public members in the base class can now become either public or private members in the derived class. Image Credit: www.123rf.com
  • 15. Facts About Base & Derived Classes 3. The derived class can contain additional data and or functions. 4. The derived class can redefine the public member functions of the base class. That is, in the derived class you can have a member function with the same name, number and type of parameters as a function in the base class, but with different code in the function body. This redefinition will only apply to objects of the derived class, not to objects of the base class. Image Credit: www.featurepics.com
  • 16. Facts About Base & Derived Classes 5. All member variables of the base class are also member variables of the derived class. Similarly, the member functions of the base class are also members of the derived class. Image Credit: www.liveboxcloud.com
  • 17. Redefining / Overriding Member Functions Of the Base Class • Let us assume that the class derivedClass was derived from the class baseClass. • Assume that both derivedClass and baseClass have some member variables. • It follows that the member variables of the class derivedClass are its own member variables, together with the member variables of baseClass. Image Credit: nicodewet.com
  • 18. Redefining / Overriding Member Functions Of the Base Class • Suppose that baseClass contains a method print that prints the values of the variables of baseClass. • Now derivedClass contains more variables than just those inherited from baseClass. • Suppose that you want to include a method in derivedClass that will print the variables in derivedClass. Image Credit: en.soft-xpansion.eu
  • 19. Redefining / Overriding Member Functions Of the Base Class • You can give any name to this new method. • However, in the class derivedClass you can also call this new method print – the same name used by baseClass. • This is called redefining or overriding the method of the base class. Image Credit: inkondapaper.com
  • 20. Redefining / Overriding Member Functions Of the Base Class • Note: To redefine a public method of a base class in the derived class, the corresponding class in the derived class must have the same name, number and types of parameters. Image Credit: www.abc.net.au
  • 21. Example: rectangleType & boxType • Class boxType is derived from class rectangleType and it is a public inheritance. • All public members of class rectangleType are public members of class boxType. • The class boxType also overrides / redefines the functions print and area. 1 Image Credit: globe-views.com
  • 22. How Can A Derived Class Call A Public Method In The Base Class? • If the derived class overrides a public method of the base class, then to specify a call to that public member function of the base class , you use the name of the base class, followed by the scope resolution operator , ::, followed by the function name with the appropriate parameter list. Example: to call the area method of the class rectangleType the statement is: rectangleType::area() • If the derived class does not override a public method of the base class, you may specify a call to that public method by using the name of the function and the appropriate parameter list Image Credit: thornleyfallis.com
  • 23. Creating The Method Print Of The Class boxType • The class boxType has three variables: length, width, and height. The purpose of the print method is to print the current value of these 3 variables. • We need to realize the following: – The variables length and width are private members of class rectangleType. This means that they cannot be directly accessed in class boxType. Therefore in the boxType version of print we cannot access these variables directly. – The variables length and width of class rectangleType are accessible by the class boxType only through the public methods of the class rectangleType. This means that the print method of boxType will have to call the print method of rectangleType to print the values of length and width. We then print height. Image Credit: www.nuttall-packaging.co.uk
  • 24. Creating The Method Print Of The Class boxType • From the class boxType, if we want to call the print method associated with rectangleType, we will need to use the following statement: rectangleType::print() • This statement will ensure that we call the print method associated with the class rectangleType and not the one associated with the class boxType. Image Credit: www.fedex.com
  • 25. Creating The Method Print Of The Class boxType • The definition of the method print for the class boxType is: void print () { rectangleType::print(); cout << “Height = “ << height; } Image Credit: www.iconarchive.com
  • 26. Creating The Method area Of The Class boxType • The method area of boxType calculates the surface area of a box. • To calculate the surface area of a box we need access to the length and width which are declared as private in class rectangleType. • This means that we’ll have to use methods getLength and getWidth in class rectangleType to get these values. • Because class boxType does not have any methods called getLenght and getWidth, we can call these methods directly without having to specify the base class. Image Credit: calibermag.org
  • 27. Creating The Method area Of The Class boxType • The definition of the boxType function area is: double area(); { return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height); } Image Credit: www.kauerguitars.com
  • 28. Creating The Method volume Of The Class boxType • The method volume of boxType determines the volume of a box. • One way to determine volume is to multiple the area of the base of a box by its height. • Use the method of the base class rectangleType to calculate the area of the base. • Because the class boxType overrides the method area, to specify a call to the method area of class rectangleType we use the name of the base class and the scope resolution operator. Image Credit: www.esupplystore.com
  • 29. Creating The Method area Of The Class boxType • The definition of the boxType function volume is: double volume() { return rectangleType::area() * height; } Image Credit: movingboxestoronto.net
  • 30. Constructors of Derived And Base Classes • A derived class can have it’s own private variables. This means that a derived class needs to include its own constructors in order to initialize them. • When we declare a derived class object, this object inherits the variables from the base class. • The derived class cannot directly access the private variables of the base class. Methods of the derived class also cannot access these private variables. Image Credit: www.pronto1.com
  • 31. Constructors of Derived And Base Classes • What this means is that the constructors of a derived class can directly initialize only the public variables inherited from the base class. • This means that when a derived object is created, it will need to trigger the constructor of the base class. • The triggering of the base class’ constructor is specified in the heading of the definition of a derived class constructor. Image Credit: www.itcuties.com
  • 32. boxType Constructor • Definition of the default constructor for boxType: boxType() { height = 0.0; } Image Credit: www.dreamstime.com
  • 33. boxType Constructor With Parameters • We first write the boxType constructor heading including all of the parameters needed for both the base class and the derived class constructors (i.e. all parameters needed for rectangleType and boxType). • In order to trigger the execution of the base class constructor with the parameters, we add a “:” to the heading followed by the name of the constructor of the base class with its parameters in the heading of the definition of the constructor of the derived class. Image Credit: www.dreamstime.com
  • 34. Definition of the Constructor With Parameters of the Class boxType boxType (double l, double, w, double h) : rectangleType(l,w) { if (h>=0) height = h; else height = 0; } • In this definition we specify the constructor of rectangleType with two parameters. When this constructor of boxType executes, it triggers the execution of constructor of class rectangleType with two parameters of type double. Image Credit: www.dreamstime.com
  • 35. Using Constructors • Consider the following two statements: rectangleType myRectangle (5.0,3.0); boxType myBox (6.0, 5.0, 4.0); • The first statement creates the rectangleType object myRectangle. • The object myRectangle has two variables length and width. • The second statement creates the boxType object myBox • The object myBox has three variables length, width, and height. Image Credit: www.jvkconstructors.com
  • 36. 2-Part In Class Programming Challenge • Bob’s lawn care store specializes in putting up fences around lawns and fertilizing the lawns. Assume that yards are rectangular. In order to put up the fence, the program needs to know the parameter and to fertilize it needs to know the area. Write a program that uses the class rectangleType to store the dimension of a yard, the cost per foot to put up the fence, and the cost per square foot to fertilize the yard. The program then outputs the cost of putting up the fence and fertilizing the yard. • Sue’s gift store wraps small packages. Assume that each gift is in a box that has a length, width, and height. Write a program that uses the class boxType to store the dimensions of a package. The user inputs the dimensions of the package and the cost per square foot to wrap the package. The program will then output the cost to wrap the package. Minimum cost of wrapping a package is $1.00. Assume: Length = 70, Width = 50, Cost of fence = $10, Cost of fertilizer = 0.25 Pkg Length = 3, Pkg width = 2, Pkg height = 0.25, Cost of wrapping = 0.25 Image Credit: www.dreamstime.com
  • 38. What’s In Your C++ Toolbox? cout / cin #include if/else/ Switch Math Class String getline While For do…While Break / Continue Arrays Functions

Editor's Notes

  1. New name for the class I know what this means Technical professionals are who get hired This means much more than just having a narrow vertical knowledge of some subject area. It means that you know how to produce an outcome that I value. I’m willing to pay you to do that.