SlideShare a Scribd company logo
1 of 41
1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition
Chapter 13
Overloading and Templates
2
Objectives (1 of 2)
• In this chapter, you will:
• Learn about overloading
• Become familiar with the restrictions on operator overloading
• Examine the pointer this
• Learn about friend functions
• Learn how to overload operators as members and nonmembers of a 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
3
Objectives (2 of 2)
• Discover how to overload various operators
• Become familiar with the requirements for classes with pointer member variables
• Learn about templates
• Explore how to construct function templates and class templates
• Become aware of C++11 random number generators
© 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
• Templates enable you to write generic code for related functions and classes
• Function templates simplify function overloading
© 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
Why Operator Overloading Is Needed (1 of 2)
• Consider the following statements:
clockType myClock(8, 23, 34);
clockType yourClock(4, 5, 30);
• Which version of C++ statements would you prefer?
myClock.printTime();
myClock.incrementSeconds();
if (myClock.equalTime(yourClock))
.
.
.
cout << myClock;
myClock++;
if (myClock == yourClock)
.
.
.
© 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
Why Operator Overloading Is Needed (2 of 2)
• Assignment and member selection are the only built-in operations on classes
• Other operators cannot be applied directly to class objects
• Operator overloading extends the definition of an operator to work with a user-
defined data type
• C++ allows you to extend the definitions of most of the operators to work with 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
7
Operator Overloading
• Most existing C++ operators can be overloaded to manipulate class objects
• New operators cannot be created
• An operator function is a function that overloads an operator
• Use reserved word operator followed by the operator as the function name
© 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
Syntax for Operator Functions
• Syntax of an operator function heading:
• It is a value-returning function
• operator is a reserved word
• To overload an operator for a class:
• Include the operator function declaration in the class definition
• Write the definition of the operator function
© 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
Overloading an Operator: Some Restrictions
• Cannot change the precedence of an operator
• Associativity cannot be changed
• Default parameters cannot be used
• Cannot change number of parameters
• Cannot create new operators
• Cannot overload: . .* :: ?: sizeof
• How the operator works with built-in types remains the same
• Can overload for user-defined objects or for a combination of user-defined and
built-in objects
© 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
Pointer this
• Every object of a class maintains a (hidden) pointer to itself called this
• When an object invokes a member function
• this is referenced by the member function
© 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
Friend Functions of Classes
• A friend function (of a class) is a nonmember function of the class that has
access to all the members of the class
• Use the reserved word friend in the function prototype in the class definition
• Friendship is always given by the class
class classIllusFriend
{
friend void two(/*parameters*/);
.
.
.
};
© 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
Definition of a friend Function
• friend does not appear in the heading of the function’s definition
• When writing the friend function’s definition
• The name of the class and the scope resolution operator are not used
void two(/*parameters*/)
{
.
.
.
}
© 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
Operator Functions as Member and Nonmember Functions
• To overload (), [], ->, or = for a class, the function must be a member of the
class
• Suppose op is overloaded for opOverClass:
• If the leftmost operand of op is an object of a different type, the overloading function
must be a nonmember (friend) of the class
• If the overloading function for op is a member of opOverClass, then when
applying op on objects of type opOverClass, the leftmost operand must be of type
opOverClass
© 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
Overloading Binary Operators
• If # represents a binary operator (e.g., + or ==) that is to be overloaded for
rectangleType
• It can be overloaded as either a member function of the class or as a friend function
© 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
Overloading the Binary Operators as Member Functions
• Function prototype (included in the class definition):
• Function 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
16
Overloading the Binary Operators (Arithmetic or Relational) as
Nonmember Functions
• Function prototype (included in class definition):
• Function 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
17
Overloading the Stream Insertion (<<) and Extraction (>>) Operators
• Consider the expression:
cout << myRectangle;
• Leftmost operand is an ostream object, not a rectangleType object
• Thus, the operator function that overloads << for rectangleType must be a
nonmember function of the class
• The same applies to the function that overloads >>
© 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
Overloading the Stream Insertion Operator (<<)
• Function prototype:
• Function 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
19
Overloading the Stream Extraction Operator (>>)
• Function prototype:
• Function 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
20
Overloading the Assignment Operator (=)
• Function prototype:
• Function 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
21
Overloading Unary Operators
• To overload a unary operator for a class:
• If the operator function is a member of the class, it has no parameters
• If the operator function is a nonmember (i.e., a friend function), it has one
parameter
© 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
Overloading the Increment (++) and Decrement (--) Operators (1 of 4)
• General syntax to overload the pre-increment operator ++ as a member
function
• Function prototype:
• Function 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
23
Overloading the Increment (++) and Decrement (--) Operators (2 of 4)
• General syntax to overload the pre-increment operator ++ as a nonmember
function
• Function prototype:
• Function 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
24
Overloading the Increment (++) and Decrement (--) Operators (3 of 4)
• General syntax to overload the post-increment operator ++ as a member
function:
• Function prototype:
• Function 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
25
Overloading the Increment (++) and Decrement (--) Operators (4 of 4)
• General syntax to overload the post-increment operator ++ as a nonmember
function:
• Function prototype:
• Function 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
26
Operator Overloading: Member versus Nonmember (1 of 2)
• Some operators must be overloaded as member functions and some must be
overloaded as nonmember (friend) functions
• Binary arithmetic operator + can be overloaded either way
• As a member function, operator + has direct access to data members of one of the
objects
• Need to pass only one object as a parameter
© 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
Operator Overloading: Member versus Nonmember (2 of 2)
• Overload + as a nonmember function
• Must pass both objects as parameters
• Code may be somewhat clearer this way
© 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
Classes and Pointer Member Variables (Revisited)
• Recall that the assignment operator copies member variables from one object
to another of the same type
• Does not work well with pointer member variables
• Classes with pointer member variables must:
• Explicitly overload the assignment operator
• Include the copy constructor
• Include the destructor
© 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
Operator Overloading: One Final Word
• If an operator op is overloaded for a class, e.g., rectangleType
• When you use op on objects of type rectangleType, the body of the function
that overloads the operator op for the class rectangleType executes
• Therefore, whatever code you put in the body of the function executes
© 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
Overloading the Array Index (Subscript) Operator ([])
• Syntax to declare operator[] as a member of a class for nonconstant arrays:
• Syntax to declare operator[] as a member of a class for constant arrays:
© 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
Function Overloading
• Overloading a function refers to having several functions with the same name,
but different parameters
• The parameter list determines which function will execute
• Must provide the definition of each function
© 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
Templates (1 of 2)
• Template: a single code body for a set of related functions (function template)
and related classes (class template)
• Syntax:
• Type is the data type
• declaration is either a function declaration or a class declaration
© 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
Templates (2 of 2)
• class in the heading refers to any user-defined type or built-in type
• Type is a formal parameter to the template
• Just as variables are parameters to functions, data types are parameters to
templates
© 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
Function Templates
• Syntax of the function template:
• Type is a formal parameter of the template used to:
• Specify type of parameters to the function
• Specify return type of the function
• Declare variables within the function
© 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
Class Templates
• Class template: a single code segment for a set of related classes
• Called parameterized types
• Syntax:
• A template instantiation can be created with either a built-in or user-defined
type
• The function members of a class template are considered to be function
templates
© 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
Header File and Implementation File of a Class Template (1 of 2)
• Passing a parameter to a function takes effect at run time
• Passing a parameter to a class template takes effect at compile time
• Cannot compile the implementation file independently of the client code
• Can put class definition and definitions of the function templates directly in the client
code
• Can put class definition and the definitions of the function templates in the same
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
37
Header File and Implementation File of a Class Template (2 of 2)
• Another alternative is to put class definition and function definitions in
separate files
• Include directive to the implementation file at the end of the header file
• In either case, function definitions and client code are compiled together
© 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
C++11 Random Number Generator
• To use C++ 11 random number generator functions we use an engine and a
distributor
• An engine returns unpredictable (random) bits
• A distribution returns random numbers whose likelihoods correspond to a specific
shape such as a uniform or normal distribution
• The C++11 standard library provides 25 distribution types in five categories
• uniform_int_distribution and uniform_real_distribution fall in
the category of uniform distributions
© 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
39
Quick Review (1 of 3)
• An operator that has different meanings with different data types is said to be
overloaded
• Operator function: a function that overloads an operator
• operator is a reserved word
• Operator functions are value-returning
• Operator overloading provides the same concise notation for user-defined data
types as for built-in data types
© 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
40
Quick Review (2 of 3)
• Only existing operators can be overloaded
• The pointer this refers to the object
• A friend function is a nonmember of a class
• If an operator function is a member of a class
• The leftmost operand of the operator must be a class object (or a reference to a class
object) of that operator’s 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
41
Quick Review (3 of 3)
• Classes with pointer variables must overload the assignment operator, and
include both the copy constructor and the destructor
• In C++, template is a reserved word
• Function template: a single code segment for a set of related functions
• Class template: a single code segment for a set of related classes
- Are called parameterized types
• C++11 provides many functions to implement random number generator.
© 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
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop conceptsRushiBShah
 
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철민 권
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Raffi Khatchadourian
 
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
 
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
 

What's hot (12)

9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
L05 Frameworks
L05 FrameworksL05 Frameworks
L05 Frameworks
 
L07 Frameworks
L07 FrameworksL07 Frameworks
L07 Frameworks
 
L09 Frameworks
L09 FrameworksL09 Frameworks
L09 Frameworks
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
 
JavaMicroBenchmarkpptm
JavaMicroBenchmarkpptmJavaMicroBenchmarkpptm
JavaMicroBenchmarkpptm
 
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
 
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...Defaultification Refactoring: A Tool for Automatically Converting Java Method...
Defaultification Refactoring: A Tool for Automatically Converting Java Method...
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews
 
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
 

Similar to 9781337102087 ppt ch13

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressionspullaravikumar
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
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
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsAmitaSuri
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business ContinuityDr. Ahmed Al Zaidy
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdfTanayKashid
 
APEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep DiveAPEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep DiveJohnSnyders
 
Oracle APEX Application Features
Oracle APEX Application FeaturesOracle APEX Application Features
Oracle APEX Application FeaturesChristina Cho
 
JavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsJavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsLearnNowOnline
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentationdgdotson
 
principles of economics the cost of production.ppt
principles of economics the cost of production.pptprinciples of economics the cost of production.ppt
principles of economics the cost of production.pptSubhanAli78
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choicetoddbr
 
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
 

Similar to 9781337102087 ppt ch13 (20)

Software Development, Data Types, and Expressions
Software Development, Data Types, and ExpressionsSoftware Development, Data Types, and Expressions
Software Development, Data Types, and Expressions
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
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
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
 
Sql9e ppt ch08
Sql9e ppt ch08Sql9e ppt ch08
Sql9e ppt ch08
 
Chapter 14 Business Continuity
Chapter 14 Business ContinuityChapter 14 Business Continuity
Chapter 14 Business Continuity
 
Python Fundamentals
Python FundamentalsPython Fundamentals
Python Fundamentals
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
C for beginners.pdf
C for beginners.pdfC for beginners.pdf
C for beginners.pdf
 
APEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep DiveAPEX Office Hours Interactive Grid Deep Dive
APEX Office Hours Interactive Grid Deep Dive
 
Oracle APEX Application Features
Oracle APEX Application FeaturesOracle APEX Application Features
Oracle APEX Application Features
 
Advanced angular
Advanced angularAdvanced angular
Advanced angular
 
JavaScript: Operators and Expressions
JavaScript: Operators and ExpressionsJavaScript: Operators and Expressions
JavaScript: Operators and Expressions
 
Chapter 12 Access Management
Chapter 12 Access ManagementChapter 12 Access Management
Chapter 12 Access Management
 
Sql9e ppt ch02
Sql9e ppt ch02 Sql9e ppt ch02
Sql9e ppt ch02
 
Excel module 1 ppt presentation
Excel module 1 ppt presentationExcel module 1 ppt presentation
Excel module 1 ppt presentation
 
principles of economics the cost of production.ppt
principles of economics the cost of production.pptprinciples of economics the cost of production.ppt
principles of economics the cost of production.ppt
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choice
 
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
 

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

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
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
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
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
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
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
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
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
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
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
 

9781337102087 ppt ch13

  • 1. 1 C++ Programming: From Problem Analysis to Program Design, Eighth Edition Chapter 13 Overloading and Templates
  • 2. 2 Objectives (1 of 2) • In this chapter, you will: • Learn about overloading • Become familiar with the restrictions on operator overloading • Examine the pointer this • Learn about friend functions • Learn how to overload operators as members and nonmembers of a 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
  • 3. 3 Objectives (2 of 2) • Discover how to overload various operators • Become familiar with the requirements for classes with pointer member variables • Learn about templates • Explore how to construct function templates and class templates • Become aware of C++11 random number generators © 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 • Templates enable you to write generic code for related functions and classes • Function templates simplify function overloading © 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 Why Operator Overloading Is Needed (1 of 2) • Consider the following statements: clockType myClock(8, 23, 34); clockType yourClock(4, 5, 30); • Which version of C++ statements would you prefer? myClock.printTime(); myClock.incrementSeconds(); if (myClock.equalTime(yourClock)) . . . cout << myClock; myClock++; if (myClock == yourClock) . . . © 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 Why Operator Overloading Is Needed (2 of 2) • Assignment and member selection are the only built-in operations on classes • Other operators cannot be applied directly to class objects • Operator overloading extends the definition of an operator to work with a user- defined data type • C++ allows you to extend the definitions of most of the operators to work with 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
  • 7. 7 Operator Overloading • Most existing C++ operators can be overloaded to manipulate class objects • New operators cannot be created • An operator function is a function that overloads an operator • Use reserved word operator followed by the operator as the function name © 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 Syntax for Operator Functions • Syntax of an operator function heading: • It is a value-returning function • operator is a reserved word • To overload an operator for a class: • Include the operator function declaration in the class definition • Write the definition of the operator function © 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 Overloading an Operator: Some Restrictions • Cannot change the precedence of an operator • Associativity cannot be changed • Default parameters cannot be used • Cannot change number of parameters • Cannot create new operators • Cannot overload: . .* :: ?: sizeof • How the operator works with built-in types remains the same • Can overload for user-defined objects or for a combination of user-defined and built-in objects © 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 Pointer this • Every object of a class maintains a (hidden) pointer to itself called this • When an object invokes a member function • this is referenced by the member function © 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 Friend Functions of Classes • A friend function (of a class) is a nonmember function of the class that has access to all the members of the class • Use the reserved word friend in the function prototype in the class definition • Friendship is always given by the class class classIllusFriend { friend void two(/*parameters*/); . . . }; © 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 Definition of a friend Function • friend does not appear in the heading of the function’s definition • When writing the friend function’s definition • The name of the class and the scope resolution operator are not used void two(/*parameters*/) { . . . } © 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 Operator Functions as Member and Nonmember Functions • To overload (), [], ->, or = for a class, the function must be a member of the class • Suppose op is overloaded for opOverClass: • If the leftmost operand of op is an object of a different type, the overloading function must be a nonmember (friend) of the class • If the overloading function for op is a member of opOverClass, then when applying op on objects of type opOverClass, the leftmost operand must be of type opOverClass © 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 Overloading Binary Operators • If # represents a binary operator (e.g., + or ==) that is to be overloaded for rectangleType • It can be overloaded as either a member function of the class or as a friend function © 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 Overloading the Binary Operators as Member Functions • Function prototype (included in the class definition): • Function 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
  • 16. 16 Overloading the Binary Operators (Arithmetic or Relational) as Nonmember Functions • Function prototype (included in class definition): • Function 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
  • 17. 17 Overloading the Stream Insertion (<<) and Extraction (>>) Operators • Consider the expression: cout << myRectangle; • Leftmost operand is an ostream object, not a rectangleType object • Thus, the operator function that overloads << for rectangleType must be a nonmember function of the class • The same applies to the function that overloads >> © 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 Overloading the Stream Insertion Operator (<<) • Function prototype: • Function 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
  • 19. 19 Overloading the Stream Extraction Operator (>>) • Function prototype: • Function 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
  • 20. 20 Overloading the Assignment Operator (=) • Function prototype: • Function 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
  • 21. 21 Overloading Unary Operators • To overload a unary operator for a class: • If the operator function is a member of the class, it has no parameters • If the operator function is a nonmember (i.e., a friend function), it has one parameter © 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 Overloading the Increment (++) and Decrement (--) Operators (1 of 4) • General syntax to overload the pre-increment operator ++ as a member function • Function prototype: • Function 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
  • 23. 23 Overloading the Increment (++) and Decrement (--) Operators (2 of 4) • General syntax to overload the pre-increment operator ++ as a nonmember function • Function prototype: • Function 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
  • 24. 24 Overloading the Increment (++) and Decrement (--) Operators (3 of 4) • General syntax to overload the post-increment operator ++ as a member function: • Function prototype: • Function 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
  • 25. 25 Overloading the Increment (++) and Decrement (--) Operators (4 of 4) • General syntax to overload the post-increment operator ++ as a nonmember function: • Function prototype: • Function 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
  • 26. 26 Operator Overloading: Member versus Nonmember (1 of 2) • Some operators must be overloaded as member functions and some must be overloaded as nonmember (friend) functions • Binary arithmetic operator + can be overloaded either way • As a member function, operator + has direct access to data members of one of the objects • Need to pass only one object as a parameter © 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 Operator Overloading: Member versus Nonmember (2 of 2) • Overload + as a nonmember function • Must pass both objects as parameters • Code may be somewhat clearer this way © 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 Classes and Pointer Member Variables (Revisited) • Recall that the assignment operator copies member variables from one object to another of the same type • Does not work well with pointer member variables • Classes with pointer member variables must: • Explicitly overload the assignment operator • Include the copy constructor • Include the destructor © 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 Operator Overloading: One Final Word • If an operator op is overloaded for a class, e.g., rectangleType • When you use op on objects of type rectangleType, the body of the function that overloads the operator op for the class rectangleType executes • Therefore, whatever code you put in the body of the function executes © 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 Overloading the Array Index (Subscript) Operator ([]) • Syntax to declare operator[] as a member of a class for nonconstant arrays: • Syntax to declare operator[] as a member of a class for constant arrays: © 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 Function Overloading • Overloading a function refers to having several functions with the same name, but different parameters • The parameter list determines which function will execute • Must provide the definition of each function © 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 Templates (1 of 2) • Template: a single code body for a set of related functions (function template) and related classes (class template) • Syntax: • Type is the data type • declaration is either a function declaration or a class declaration © 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 Templates (2 of 2) • class in the heading refers to any user-defined type or built-in type • Type is a formal parameter to the template • Just as variables are parameters to functions, data types are parameters to templates © 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 Function Templates • Syntax of the function template: • Type is a formal parameter of the template used to: • Specify type of parameters to the function • Specify return type of the function • Declare variables within the function © 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 Class Templates • Class template: a single code segment for a set of related classes • Called parameterized types • Syntax: • A template instantiation can be created with either a built-in or user-defined type • The function members of a class template are considered to be function templates © 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 Header File and Implementation File of a Class Template (1 of 2) • Passing a parameter to a function takes effect at run time • Passing a parameter to a class template takes effect at compile time • Cannot compile the implementation file independently of the client code • Can put class definition and definitions of the function templates directly in the client code • Can put class definition and the definitions of the function templates in the same 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
  • 37. 37 Header File and Implementation File of a Class Template (2 of 2) • Another alternative is to put class definition and function definitions in separate files • Include directive to the implementation file at the end of the header file • In either case, function definitions and client code are compiled together © 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 C++11 Random Number Generator • To use C++ 11 random number generator functions we use an engine and a distributor • An engine returns unpredictable (random) bits • A distribution returns random numbers whose likelihoods correspond to a specific shape such as a uniform or normal distribution • The C++11 standard library provides 25 distribution types in five categories • uniform_int_distribution and uniform_real_distribution fall in the category of uniform distributions © 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
  • 39. 39 Quick Review (1 of 3) • An operator that has different meanings with different data types is said to be overloaded • Operator function: a function that overloads an operator • operator is a reserved word • Operator functions are value-returning • Operator overloading provides the same concise notation for user-defined data types as for built-in data types © 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
  • 40. 40 Quick Review (2 of 3) • Only existing operators can be overloaded • The pointer this refers to the object • A friend function is a nonmember of a class • If an operator function is a member of a class • The leftmost operand of the operator must be a class object (or a reference to a class object) of that operator’s 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
  • 41. 41 Quick Review (3 of 3) • Classes with pointer variables must overload the assignment operator, and include both the copy constructor and the destructor • In C++, template is a reserved word • Function template: a single code segment for a set of related functions • Class template: a single code segment for a set of related classes - Are called parameterized types • C++11 provides many functions to implement random number generator. © 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