SlideShare a Scribd company logo
1 of 38
1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition
Chapter 11
Inheritance and Composition
2
Objectives (1 of 2)
• In this chapter, you will:
• Learn about inheritance
• Learn about derived and base classes
• Explore how to redefine the member functions of a base class
• Examine how the constructors of base and derived classes work
• Learn how the destructors of base and derived classes work
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
3
Objectives (2 of 2)
• Learn how to construct the header file of a derived class
• Become aware of stream classes hierarchy
• Explore three types of inheritance: public, protected, and private
• Learn about composition (aggregation)
• Become familiar with the three basic principles of object-oriented design
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
4
Introduction
• Two common ways to relate two classes in a meaningful way are:
• Inheritance (“is-a” relationship)
• Composition or aggregation: (“has-a” relationship)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
5
Inheritance (1 of 5)
• Inheritance is an “is-a” relationship
• Example: “every employee is a person”
• Inheritance allows creation of new classes from existing classes
• Derived classes: new classes created from the existing classes
• Base class: the original class
• A derived class inherits the properties of its base classes
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
6
Inheritance (2 of 5)
• Inheritance helps reduce software development complexity
• Single inheritance: derived class has a single base class
• Multiple inheritance: derived class has more than one base class
• Public inheritance: all public members of base class are inherited as public
members by derived class
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
7
Inheritance (3 of 5)
• Inheritance can be viewed as a tree-like, or hierarchical, structure between the
base class and its derived classes
FIGURE 11-1 Inheritance hierarchy
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
8
Inheritance (4 of 5)
• Syntax of a derived class:
• memberAccessSpecifier is public, protected, or private
(default)
• private members of a base class are private to the base class
• Derived class cannot directly access them
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
9
Inheritance (5 of 5)
• public members of the base class can be inherited as public or private
members
• The derived class can include additional members (data and/or functions)
• The derived class can redefine public member functions of the base class
• Applies only to the objects of the derived class
• All member variables of the base class are also member variables of the derived
class
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
10
Redefining (Overriding) Member Functions of the Base Class (1 of 3)
• To redefine a public member function:
• The corresponding function in the derived class must have the same name, number,
and types of parameters
• If the derived class overrides a public member function of the base class, then
to call the base class function, specify the:
• Name of the base class
• Scope resolution operator (::)
• Function name with appropriate parameter list
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
11
Redefining (Overriding) Member Functions of the Base Class (2 of 3)
FIGURE 11-2 UML class diagram of the class rectangleType
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
12
Redefining (Overriding) Member Functions of the Base Class (3 of 3)
• boxType is derived from rectangleType, and it is a public inheritance
• Also overrides the functions print and area
FIGURE 11-3 UML class diagram of the class boxType and the inheritance hierarchy
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
13
Constructors of Derived and Base Classes
• A derived class constructor cannot directly access private members of the
base class
• Can directly initialize only public member variables of the base class
• When a derived object is declared, it must execute one of the base class
constructors
• A call to the base class constructor is specified in the heading of the derived
class constructor definition
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
14
Destructors in a Derived Class
• Destructors deallocate dynamic memory allocated by the objects of a class
• When a derived class object goes out of scope
• Automatically invokes its destructor
• When the destructor of the derived class executes
• Automatically invokes the destructor of the base class
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
15
Header File of a Derived Class
• To define new classes, create new header files
• To create new derived classes, include commands that specify where the base
class definitions can be found
• Definitions of the member functions can be placed in a separate file
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
16
Multiple Inclusions of a Header File
• Use the preprocessor command (#include) to include a header file in a
program
• The preprocessor processes the program before it is compiled
• To avoid multiple inclusions of a file in a program, use certain preprocessor
commands in the header file
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
17
C++ Stream Classes (1 of 2)
• ios is the base class for all stream classes
• Contains formatting flags and member functions to access/modify the flag settings
FIGURE 11-6 C11 stream classes hierarchy
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
18
C++ Stream Classes (2 of 2)
• istream and ostream provide operations for data transfer between memory and devices
• istream defines the extraction operator (>>) and functions get and ignore
• ostream defines the insertion operator (<<) which is used by cout
• ifstream and ofstream objects are for file I/O
• Header file fstream contains the definitions for these
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
19
Protected Members of a Class
• A derived class cannot directly access private members of it base class
• To give it direct access, declare that member as protected
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
20
Inheritance as public, protected, or private (1 of 3)
• Assume class B is derived from class A with
• class B: memberAccessSpecifier A
• If memberAccessSpecifier is public:
• public members of A are public in B and can be directly accessed in class B
• protected members of A are protected in B and can be directly accessed by
member functions (and friend functions) of B
• private members of A are hidden in B and can be accessed only through public
or protected members of A
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
21
Inheritance as public, protected, or private (2 of 3)
• If memberAccessSpecifier is protected:
• public members of A are protected members of B and can be accessed by the
member functions (and friend functions) of B
• protected members of A are protected members of B and can be accessed by
the member functions (and friend functions) of B
• private members of A are hidden in B and can be accessed only through public
or protected members of A
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
22
Inheritance as public, protected, or private (3 of 3)
• If memberAccessSpecifier is private:
• public members of A are private members of B and can be accessed by member
functions of B
• protected members of A are private members of B and can be accessed by
member functions (and friend functions) of B
• private members of A are hidden in B and can be accessed only through public
or protected members of A
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
23
Composition (Aggregation) (1 of 2)
• In composition, one or more member(s) of a class are objects of another class
type
• Composition (aggregation) is a “has-a” relation
• Arguments to the constructor of a member-object are specified in the heading
part of the definition of the constructor
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
24
Composition (Aggregation) (2 of 2)
• Member-objects of a class are constructed in the order they are declared
• Not in the order listed in the constructor’s member initialization list
• They are constructed before the containing class objects are constructed
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
25
Object-Oriented Design (OOD) and Object-Oriented Programming
(OOP) (1 of 5)
• The fundamental principles of object-oriented design (OOD) are:
• Encapsulation: combines data and operations on data in a single unit
• Inheritance: creates new objects (classes) from existing objects (classes)
• Polymorphism: the ability to use the same expression to denote different operations
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
26
OOD and OOP (2 of 5)
• In OOD:
• Object is a fundamental entity
• Debug at the class level
• A program is a collection of interacting objects
• OOD encourages code reuse
• Object-oriented programming (OOP) implements OOD
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
27
OOD and OOP (3 of 5)
• C++ supports OOP through the use of classes
• A function name and operators can be overloaded
• A polymorphic function or operator has many forms
• Example: division with floating point and division with integer operands
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
28
OOD and OOP (4 of 5)
• Templates provide parametric polymorphism
• C++ provides virtual functions to implement polymorphism in an inheritance
hierarchy
• Allows run-time selection of appropriate member functions
• Objects are created when class variables are declared
• Objects interact with each other via function calls
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
29
OOD and OOP (5 of 5)
• Every object has an internal state and an external state
• Private members form the internal state
• Public members form the external state
• Only the object can manipulate its internal state
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
30
Identifying Classes, Objects, and Operations (1 of 5)
• To find classes, begin with a problem description and identify all nouns and
verbs
• From the list of nouns choose the classes
• From the list of verbs choose the operations
• Suppose we want to write a program that calculates and prints the volume and
surface area of a cylinder
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
31
Identifying Classes, Objects, and Operations (2 of 5)
• State this problem as follows:
• Write a program to input the dimensions of a cylinder and calculate and print the
surface area and volume
• Nouns are bold and verbs are italic
• From the list of nouns, one can visualize a cylinder as a class (cylinderType) from
which we can create many cylinder objects of various dimensions
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
32
Identifying Classes, Objects, and Operations (3 of 5)
• These nouns are characteristics of a cylinder, so they will not be classes:
• Dimensions
• Surface area
• Volume
• Next, determine three pieces of information about this class:
• Operations that an object can perform
• Operations that can be performed on an object
• Information that an object must maintain
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
33
Identifying Classes, Objects, and Operations (4 of 5)
• From the verbs, list possible operations that an object of that class can perform,
or have performed, on itself
• For the cylinderType class:
- Input
- Calculate
- Print
• Dimensions of the cylinder represent the class’s data
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
34
Identifying Classes, Objects, and Operations (5 of 5)
• Identifying classes via nouns and verbs from problem descriptions is not the
only technique possible
• There are several other OOD techniques in the literature
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
35
Quick Review (1 of 4)
• Inheritance and composition are meaningful ways to relate two or more classes
• Inheritance is an “is-a” relation
• Single inheritance: a derived class is derived from one class, called the base class
• Multiple inheritance: a derived class is derived from more than one base class
• Composition is a “has-a” relation
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
36
Quick Review (2 of 4)
• private members of a base class are private to the base class
• public members of a base class can be inherited either as public or
private
• A derived class can redefine function members of a base class
• Redefinition applies only to objects of derived class
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
37
Quick Review (3 of 4)
• A call to a base class constructor (with parameters) is specified in the heading
of the definition of the derived class constructor
• When initializing object of a derived class, the base class constructor is
executed first
• In composition (aggregation):
• A class member is an object of another class
• A call to constructor of member objects is specified in heading of the definition of
class’s constructor
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom
38
Quick Review (4 of 4)
• Three basic principles of OOD:
• Encapsulation
• Inheritance
• Polymorphism
• To find classes:
• Describe the problem
• Choose classes from the list of nouns
• Choose operations from the list of verbs
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service
or otherwise on a password-protected website for classroom

More Related Content

What's hot

9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04Terry Yoast
 
Spring AOP
Spring AOPSpring AOP
Spring AOPAnushaNaidu
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop conceptsRushiBShah
 
50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questionsSynergisticMedia
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Hitesh-Java
 
Understanding GBM and XGBoost in Scikit-Learn
Understanding GBM and XGBoost in Scikit-LearnUnderstanding GBM and XGBoost in Scikit-Learn
Understanding GBM and XGBoost in Scikit-Learn철민 권
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers Hitesh-Java
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)stanbridge
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETDevLabs Alliance
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSergey Aganezov
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationGregory Solovey
 
Reflection in java
Reflection in javaReflection in java
Reflection in javaupen.rockin
 

What's hot (20)

9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
 
L05 Frameworks
L05 FrameworksL05 Frameworks
L05 Frameworks
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
L07 Frameworks
L07 FrameworksL07 Frameworks
L07 Frameworks
 
L09 Frameworks
L09 FrameworksL09 Frameworks
L09 Frameworks
 
JavaMicroBenchmarkpptm
JavaMicroBenchmarkpptmJavaMicroBenchmarkpptm
JavaMicroBenchmarkpptm
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
 
50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questions
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews
 
Understanding GBM and XGBoost in Scikit-Learn
Understanding GBM and XGBoost in Scikit-LearnUnderstanding GBM and XGBoost in Scikit-Learn
Understanding GBM and XGBoost in Scikit-Learn
 
Java scjp-part1
Java scjp-part1Java scjp-part1
Java scjp-part1
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDET
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 
Testware Hierarchy for Test Automation
Testware Hierarchy for Test AutomationTestware Hierarchy for Test Automation
Testware Hierarchy for Test Automation
 
1z0-808-certification-questions-sample
1z0-808-certification-questions-sample1z0-808-certification-questions-sample
1z0-808-certification-questions-sample
 
Reflection in java
Reflection in javaReflection in java
Reflection in java
 

Similar to 9781337102087 ppt ch11

9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
Oracle Enterprise Manager Security A Practitioners Guide
Oracle Enterprise Manager Security A Practitioners GuideOracle Enterprise Manager Security A Practitioners Guide
Oracle Enterprise Manager Security A Practitioners GuideCourtney Llamas
 
Chapter 12 Access Management
Chapter 12 Access ManagementChapter 12 Access Management
Chapter 12 Access ManagementDr. Ahmed Al Zaidy
 
Coronel_DatabaseSystems_13e_ch02.pptx
Coronel_DatabaseSystems_13e_ch02.pptxCoronel_DatabaseSystems_13e_ch02.pptx
Coronel_DatabaseSystems_13e_ch02.pptxrmzx1989
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Steve Guinan
 
Windows 10 Module 1.pdf
Windows 10 Module 1.pdfWindows 10 Module 1.pdf
Windows 10 Module 1.pdfssuser536c36
 
Windows 10 module 2 ppt presentation
Windows 10 module 2 ppt presentationWindows 10 module 2 ppt presentation
Windows 10 module 2 ppt presentationdgdotson
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressionspullaravikumar
 
Oracle Enterprise Manager Security: A Practitioners Guide
Oracle Enterprise Manager Security: A Practitioners GuideOracle Enterprise Manager Security: A Practitioners Guide
Oracle Enterprise Manager Security: A Practitioners GuideCourtney Llamas
 
Salesforce winter 16 release
Salesforce winter 16 releaseSalesforce winter 16 release
Salesforce winter 16 releaseJitendra Zaa
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentationdgdotson
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#Chandan Gupta Bhagat
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementDr. Ahmed Al Zaidy
 
SFDC Deployments
SFDC DeploymentsSFDC Deployments
SFDC DeploymentsSujit Kumar
 
Access 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentationAccess 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentationdgdotson
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdfSHASHIKANT346021
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesDigitalDsms
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentationdgdotson
 

Similar to 9781337102087 ppt ch11 (20)

9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
Oracle Enterprise Manager Security A Practitioners Guide
Oracle Enterprise Manager Security A Practitioners GuideOracle Enterprise Manager Security A Practitioners Guide
Oracle Enterprise Manager Security A Practitioners Guide
 
Chapter 12 Access Management
Chapter 12 Access ManagementChapter 12 Access Management
Chapter 12 Access Management
 
Coronel_DatabaseSystems_13e_ch02.pptx
Coronel_DatabaseSystems_13e_ch02.pptxCoronel_DatabaseSystems_13e_ch02.pptx
Coronel_DatabaseSystems_13e_ch02.pptx
 
Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7Intro to Web Design 6e Chapter 7
Intro to Web Design 6e Chapter 7
 
Windows 10 Module 1.pdf
Windows 10 Module 1.pdfWindows 10 Module 1.pdf
Windows 10 Module 1.pdf
 
Windows 10 module 2 ppt presentation
Windows 10 module 2 ppt presentationWindows 10 module 2 ppt presentation
Windows 10 module 2 ppt presentation
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressions
 
Oracle Enterprise Manager Security: A Practitioners Guide
Oracle Enterprise Manager Security: A Practitioners GuideOracle Enterprise Manager Security: A Practitioners Guide
Oracle Enterprise Manager Security: A Practitioners Guide
 
Salesforce winter 16 release
Salesforce winter 16 releaseSalesforce winter 16 release
Salesforce winter 16 release
 
Excel module 2 ppt presentation
Excel module 2 ppt presentationExcel module 2 ppt presentation
Excel module 2 ppt presentation
 
Better Understanding OOP using C#
Better Understanding OOP using C#Better Understanding OOP using C#
Better Understanding OOP using C#
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Chapter 11 Authentication and Account Management
Chapter 11 Authentication and Account ManagementChapter 11 Authentication and Account Management
Chapter 11 Authentication and Account Management
 
SFDC Deployments
SFDC DeploymentsSFDC Deployments
SFDC Deployments
 
Access 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentationAccess 2016 module 2 ppt presentation
Access 2016 module 2 ppt presentation
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentation
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07Terry Yoast
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 

More from Terry Yoast (17)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

9781337102087 ppt ch11

  • 1. 1 C++ Programming: From Problem Analysis to Program Design, Eighth Edition Chapter 11 Inheritance and Composition
  • 2. 2 Objectives (1 of 2) • In this chapter, you will: • Learn about inheritance • Learn about derived and base classes • Explore how to redefine the member functions of a base class • Examine how the constructors of base and derived classes work • Learn how the destructors of base and derived classes work © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 3. 3 Objectives (2 of 2) • Learn how to construct the header file of a derived class • Become aware of stream classes hierarchy • Explore three types of inheritance: public, protected, and private • Learn about composition (aggregation) • Become familiar with the three basic principles of object-oriented design © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 4. 4 Introduction • Two common ways to relate two classes in a meaningful way are: • Inheritance (“is-a” relationship) • Composition or aggregation: (“has-a” relationship) © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 5. 5 Inheritance (1 of 5) • Inheritance is an “is-a” relationship • Example: “every employee is a person” • Inheritance allows creation of new classes from existing classes • Derived classes: new classes created from the existing classes • Base class: the original class • A derived class inherits the properties of its base classes © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 6. 6 Inheritance (2 of 5) • Inheritance helps reduce software development complexity • Single inheritance: derived class has a single base class • Multiple inheritance: derived class has more than one base class • Public inheritance: all public members of base class are inherited as public members by derived class © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 7. 7 Inheritance (3 of 5) • Inheritance can be viewed as a tree-like, or hierarchical, structure between the base class and its derived classes FIGURE 11-1 Inheritance hierarchy © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 8. 8 Inheritance (4 of 5) • Syntax of a derived class: • memberAccessSpecifier is public, protected, or private (default) • private members of a base class are private to the base class • Derived class cannot directly access them © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 9. 9 Inheritance (5 of 5) • public members of the base class can be inherited as public or private members • The derived class can include additional members (data and/or functions) • The derived class can redefine public member functions of the base class • Applies only to the objects of the derived class • All member variables of the base class are also member variables of the derived class © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 10. 10 Redefining (Overriding) Member Functions of the Base Class (1 of 3) • To redefine a public member function: • The corresponding function in the derived class must have the same name, number, and types of parameters • If the derived class overrides a public member function of the base class, then to call the base class function, specify the: • Name of the base class • Scope resolution operator (::) • Function name with appropriate parameter list © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 11. 11 Redefining (Overriding) Member Functions of the Base Class (2 of 3) FIGURE 11-2 UML class diagram of the class rectangleType © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 12. 12 Redefining (Overriding) Member Functions of the Base Class (3 of 3) • boxType is derived from rectangleType, and it is a public inheritance • Also overrides the functions print and area FIGURE 11-3 UML class diagram of the class boxType and the inheritance hierarchy © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 13. 13 Constructors of Derived and Base Classes • A derived class constructor cannot directly access private members of the base class • Can directly initialize only public member variables of the base class • When a derived object is declared, it must execute one of the base class constructors • A call to the base class constructor is specified in the heading of the derived class constructor definition © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 14. 14 Destructors in a Derived Class • Destructors deallocate dynamic memory allocated by the objects of a class • When a derived class object goes out of scope • Automatically invokes its destructor • When the destructor of the derived class executes • Automatically invokes the destructor of the base class © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 15. 15 Header File of a Derived Class • To define new classes, create new header files • To create new derived classes, include commands that specify where the base class definitions can be found • Definitions of the member functions can be placed in a separate file © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 16. 16 Multiple Inclusions of a Header File • Use the preprocessor command (#include) to include a header file in a program • The preprocessor processes the program before it is compiled • To avoid multiple inclusions of a file in a program, use certain preprocessor commands in the header file © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 17. 17 C++ Stream Classes (1 of 2) • ios is the base class for all stream classes • Contains formatting flags and member functions to access/modify the flag settings FIGURE 11-6 C11 stream classes hierarchy © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 18. 18 C++ Stream Classes (2 of 2) • istream and ostream provide operations for data transfer between memory and devices • istream defines the extraction operator (>>) and functions get and ignore • ostream defines the insertion operator (<<) which is used by cout • ifstream and ofstream objects are for file I/O • Header file fstream contains the definitions for these © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 19. 19 Protected Members of a Class • A derived class cannot directly access private members of it base class • To give it direct access, declare that member as protected © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 20. 20 Inheritance as public, protected, or private (1 of 3) • Assume class B is derived from class A with • class B: memberAccessSpecifier A • If memberAccessSpecifier is public: • public members of A are public in B and can be directly accessed in class B • protected members of A are protected in B and can be directly accessed by member functions (and friend functions) of B • private members of A are hidden in B and can be accessed only through public or protected members of A © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 21. 21 Inheritance as public, protected, or private (2 of 3) • If memberAccessSpecifier is protected: • public members of A are protected members of B and can be accessed by the member functions (and friend functions) of B • protected members of A are protected members of B and can be accessed by the member functions (and friend functions) of B • private members of A are hidden in B and can be accessed only through public or protected members of A © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 22. 22 Inheritance as public, protected, or private (3 of 3) • If memberAccessSpecifier is private: • public members of A are private members of B and can be accessed by member functions of B • protected members of A are private members of B and can be accessed by member functions (and friend functions) of B • private members of A are hidden in B and can be accessed only through public or protected members of A © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 23. 23 Composition (Aggregation) (1 of 2) • In composition, one or more member(s) of a class are objects of another class type • Composition (aggregation) is a “has-a” relation • Arguments to the constructor of a member-object are specified in the heading part of the definition of the constructor © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 24. 24 Composition (Aggregation) (2 of 2) • Member-objects of a class are constructed in the order they are declared • Not in the order listed in the constructor’s member initialization list • They are constructed before the containing class objects are constructed © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 25. 25 Object-Oriented Design (OOD) and Object-Oriented Programming (OOP) (1 of 5) • The fundamental principles of object-oriented design (OOD) are: • Encapsulation: combines data and operations on data in a single unit • Inheritance: creates new objects (classes) from existing objects (classes) • Polymorphism: the ability to use the same expression to denote different operations © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 26. 26 OOD and OOP (2 of 5) • In OOD: • Object is a fundamental entity • Debug at the class level • A program is a collection of interacting objects • OOD encourages code reuse • Object-oriented programming (OOP) implements OOD © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 27. 27 OOD and OOP (3 of 5) • C++ supports OOP through the use of classes • A function name and operators can be overloaded • A polymorphic function or operator has many forms • Example: division with floating point and division with integer operands © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 28. 28 OOD and OOP (4 of 5) • Templates provide parametric polymorphism • C++ provides virtual functions to implement polymorphism in an inheritance hierarchy • Allows run-time selection of appropriate member functions • Objects are created when class variables are declared • Objects interact with each other via function calls © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 29. 29 OOD and OOP (5 of 5) • Every object has an internal state and an external state • Private members form the internal state • Public members form the external state • Only the object can manipulate its internal state © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 30. 30 Identifying Classes, Objects, and Operations (1 of 5) • To find classes, begin with a problem description and identify all nouns and verbs • From the list of nouns choose the classes • From the list of verbs choose the operations • Suppose we want to write a program that calculates and prints the volume and surface area of a cylinder © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 31. 31 Identifying Classes, Objects, and Operations (2 of 5) • State this problem as follows: • Write a program to input the dimensions of a cylinder and calculate and print the surface area and volume • Nouns are bold and verbs are italic • From the list of nouns, one can visualize a cylinder as a class (cylinderType) from which we can create many cylinder objects of various dimensions © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 32. 32 Identifying Classes, Objects, and Operations (3 of 5) • These nouns are characteristics of a cylinder, so they will not be classes: • Dimensions • Surface area • Volume • Next, determine three pieces of information about this class: • Operations that an object can perform • Operations that can be performed on an object • Information that an object must maintain © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 33. 33 Identifying Classes, Objects, and Operations (4 of 5) • From the verbs, list possible operations that an object of that class can perform, or have performed, on itself • For the cylinderType class: - Input - Calculate - Print • Dimensions of the cylinder represent the class’s data © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 34. 34 Identifying Classes, Objects, and Operations (5 of 5) • Identifying classes via nouns and verbs from problem descriptions is not the only technique possible • There are several other OOD techniques in the literature © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 35. 35 Quick Review (1 of 4) • Inheritance and composition are meaningful ways to relate two or more classes • Inheritance is an “is-a” relation • Single inheritance: a derived class is derived from one class, called the base class • Multiple inheritance: a derived class is derived from more than one base class • Composition is a “has-a” relation © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 36. 36 Quick Review (2 of 4) • private members of a base class are private to the base class • public members of a base class can be inherited either as public or private • A derived class can redefine function members of a base class • Redefinition applies only to objects of derived class © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 37. 37 Quick Review (3 of 4) • A call to a base class constructor (with parameters) is specified in the heading of the definition of the derived class constructor • When initializing object of a derived class, the base class constructor is executed first • In composition (aggregation): • A class member is an object of another class • A call to constructor of member objects is specified in heading of the definition of class’s constructor © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom
  • 38. 38 Quick Review (4 of 4) • Three basic principles of OOD: • Encapsulation • Inheritance • Polymorphism • To find classes: • Describe the problem • Choose classes from the list of nouns • Choose operations from the list of verbs © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom