SlideShare a Scribd company logo
Chapter 13:
Overloading and Templates
Objectives
• 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
2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Objectives (cont’d.)
• In this chapter, you will (cont’d.)
– 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
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Introduction
• Templates: enable you to write generic code
for related functions and classes
• Function templates: used to simplify function
overloading
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Why Operator Overloading Is Needed
• Consider the following statements:
• Which of the following would you prefer?
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Why Operator Overloading Is Needed
(cont’d.)
• Assignment and member selection are the
only built-in operations on classes
• Other operators cannot be applied directly to
class objects
• Operator overloading: extends 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
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Operator Overloading
• Most existing C++ operators can be
overloaded to manipulate class objects
• Cannot create new operators
• Operator function: overloads an operator
– Use reserved word operator as the function
name
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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 operator function declaration in the class
definition
– Write the definition of the operator function
8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading an Operator: Some
Restrictions
• Cannot change precedence or associativity
• 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
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Friend Functions of Classes
• Friend function (of a class): 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
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Definition of a friend Function
• "friend" doesn’t appear in function
definition
• When writing the friend function definition
– The name of the class and the scope resolution
operator are not used
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Operator Functions as Member and
Nonmember Functions
• To overload (), [], ->, or = for a class, the
function must be a member of the class
• If 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
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Binary Operators as
Member Functions
• Function prototype (included in the class
definition):
• Function definition:
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Arithmetic or Relational
Operators as Nonmember Functions
• Function prototype (included in class
definition):
• Function definition:
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
– Same applies to the function that overloads >>
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the
Stream Insertion Operator (<<)
• Function prototype:
• Function definition:
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Stream Extraction
Operator (>>)
• Function prototype:
• Function definition:
19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Assignment Operator
(=)
• Function prototype:
• Function definition:
20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Increment (++) and
Decrement (--) Operators
• General syntax to overload the pre-increment
operator ++ as a member function
– Function prototype:
– Function definition:
22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Increment (++) and
Decrement (--) Operators (cont’d.)
• General syntax to overload the pre-increment
operator ++ as a nonmember function:
– Function prototype:
– Function definition:
23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Increment (++) and
Decrement (--) Operators (cont’d.)
• General syntax to overload the post-
increment operator ++ as a member function:
– Function prototype:
– Function definition:
24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Overloading the Increment (++) and
Decrement (--) Operators (cont’d.)
• Syntax to overload the post-increment
operator ++ as a nonmember function:
– Function prototype:
– Function definition:
25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Operator Overloading:
Member Versus Nonmember
• 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
26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Operator Overloading:
Member Versus Nonmember (cont’d.)
• Overload + as a nonmember function
– Must pass both objects as parameters
– Code may be somewhat clearer this way
27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Classes and Pointer Member
Variables (Revisited)
• Recall that 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
28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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:
30C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Function Overloading
• Overloading a function: several functions with
the same name, but different parameters
– Parameter list determines which function will
execute
– Must provide the definition of each function
31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Templates
• 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
32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Templates (cont’d.)
• class in the heading refers to any user-
defined type or built-in type
• Type: a formal parameter to the template
• Just as variables are parameters to functions,
data types are parameters to templates
33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
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
35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Header File and Implementation File
of a Class Template
• 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
36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Header File and Implementation File
of a Class Template (cont’d.)
• Another alternative: put class definition and
function definitions in separate files
– Include directive to implementation file at the end
of header file
• In either case, function definitions and client
code are compiled together
37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary
• 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
38C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• 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
39C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Summary (cont’d.)
• Classes with pointer variables must overload
the assignment operator, and include both a
copy constructor and deconstructor
• Templates:
– 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
40C++ Programming: From Problem Analysis to Program Design, Seventh Edition

More Related Content

What's hot

9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
Terry Yoast
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
Terry Yoast
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
Terry Yoast
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
Terry Yoast
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
Terry Yoast
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
Burhan Fakhar
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
MLG College of Learning, Inc
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
ViswanathanS21
 
Chapter 02 - Problem Solving
Chapter 02 - Problem SolvingChapter 02 - Problem Solving
Chapter 02 - Problem Solving
Luis Daniel Serrano
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
Unfold UI
 
Csc153 chapter 05
Csc153 chapter 05Csc153 chapter 05
Csc153 chapter 05PCC
 
Ladc presentation
Ladc presentationLadc presentation
Ladc presentation
erikamicrosoft
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05Terry Yoast
 
10. sub program
10. sub program10. sub program
20100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture0220100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture02Computer Science Club
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05Terry Yoast
 
Matlab brochure
Matlab  brochureMatlab  brochure
Matlab brochure
Zabeel Institute
 
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02PCC
 
Yacc
YaccYacc
Csc153 chapter 07
Csc153 chapter 07Csc153 chapter 07
Csc153 chapter 07PCC
 

What's hot (20)

9781285852744 ppt ch09
9781285852744 ppt ch099781285852744 ppt ch09
9781285852744 ppt ch09
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
9781285852744 ppt ch16
9781285852744 ppt ch169781285852744 ppt ch16
9781285852744 ppt ch16
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Programming in c (importance of c)
Programming in c (importance of c)Programming in c (importance of c)
Programming in c (importance of c)
 
Chapter 02 - Problem Solving
Chapter 02 - Problem SolvingChapter 02 - Problem Solving
Chapter 02 - Problem Solving
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
Csc153 chapter 05
Csc153 chapter 05Csc153 chapter 05
Csc153 chapter 05
 
Ladc presentation
Ladc presentationLadc presentation
Ladc presentation
 
9781439035665 ppt ch05
9781439035665 ppt ch059781439035665 ppt ch05
9781439035665 ppt ch05
 
10. sub program
10. sub program10. sub program
10. sub program
 
20100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture0220100522 software verification_sharygina_lecture02
20100522 software verification_sharygina_lecture02
 
9781111530532 ppt ch05
9781111530532 ppt ch059781111530532 ppt ch05
9781111530532 ppt ch05
 
Matlab brochure
Matlab  brochureMatlab  brochure
Matlab brochure
 
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02
 
Yacc
YaccYacc
Yacc
 
Csc153 chapter 07
Csc153 chapter 07Csc153 chapter 07
Csc153 chapter 07
 

Viewers also liked

Software development
Software developmentSoftware development
Software development
Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
Computer science education week or csed week
Computer science education week or csed weekComputer science education week or csed week
Computer science education week or csed week
Terry Yoast
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04Terry Yoast
 
Algorithm
AlgorithmAlgorithm
Algorithm
eShikshak
 
9780840024220 ppt ch06
9780840024220 ppt ch069780840024220 ppt ch06
9780840024220 ppt ch06
Kristin Harrison
 
JavaYDL14
JavaYDL14JavaYDL14
JavaYDL14
Terry Yoast
 
Computer basics
Computer basicsComputer basics
Computer basics
mohammed-alshukur
 
raid technology
raid technologyraid technology
raid technology
Mangukiya Maulik
 
Computer operating systems
Computer operating systemsComputer operating systems
Computer operating systems
mohammed-alshukur
 
Lesson 1 introduction
Lesson 1 introductionLesson 1 introduction
Lesson 1 introductionVhe Cagande
 

Viewers also liked (15)

Software development
Software developmentSoftware development
Software development
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
 
Savitch ch 05
Savitch ch 05Savitch ch 05
Savitch ch 05
 
Optbook
OptbookOptbook
Optbook
 
Computer science education week or csed week
Computer science education week or csed weekComputer science education week or csed week
Computer science education week or csed week
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
9780840024220 ppt ch06
9780840024220 ppt ch069780840024220 ppt ch06
9780840024220 ppt ch06
 
JavaYDL14
JavaYDL14JavaYDL14
JavaYDL14
 
Computer basics
Computer basicsComputer basics
Computer basics
 
raid technology
raid technologyraid technology
raid technology
 
Computer operating systems
Computer operating systemsComputer operating systems
Computer operating systems
 
Lesson 1 introduction
Lesson 1 introductionLesson 1 introduction
Lesson 1 introduction
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 

Similar to 9781285852744 ppt ch13

9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
LokeshK66
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
masadjie
 
C++.ppt
C++.pptC++.ppt
C++.ppt
shweta210
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
Rithiga6
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
Jay Patel
 
Practical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlibPractical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlib
Databricks
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Juginder Pal Singh
 
Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3ahmed22dg
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
LokeshK66
 
ProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPSProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPS
sunmitraeducation
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
Manivannan837728
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
Amogh Kalyanshetti
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
svishalsingh01
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 
Machine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - DatabricksMachine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - DatabricksSpark Summit
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Intro To C++ - Class #19: Functions
Intro To C++ - Class #19: FunctionsIntro To C++ - Class #19: Functions
Intro To C++ - Class #19: Functions
Blue Elephant Consulting
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
Hashni T
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
Prashant odhavani
 

Similar to 9781285852744 ppt ch13 (20)

9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Practical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlibPractical Machine Learning Pipelines with MLlib
Practical Machine Learning Pipelines with MLlib
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
 
ProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPSProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPS
 
U19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).pptU19CS101 - PPS Unit 4 PPT (1).ppt
U19CS101 - PPS Unit 4 PPT (1).ppt
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Machine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - DatabricksMachine Learning Pipelines - Joseph Bradley - Databricks
Machine Learning Pipelines - Joseph Bradley - Databricks
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Intro To C++ - Class #19: Functions
Intro To C++ - Class #19: FunctionsIntro To C++ - Class #19: Functions
Intro To C++ - Class #19: Functions
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 

More from Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
Terry Yoast
 

More from Terry Yoast (20)

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
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Recently uploaded

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
ShivajiThube2
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 

Recently uploaded (20)

Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
JEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questionsJEE1_This_section_contains_FOUR_ questions
JEE1_This_section_contains_FOUR_ questions
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 

9781285852744 ppt ch13

  • 2. Objectives • 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 2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 3. Objectives (cont’d.) • In this chapter, you will (cont’d.) – 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 3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 4. Introduction • Templates: enable you to write generic code for related functions and classes • Function templates: used to simplify function overloading 4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 5. Why Operator Overloading Is Needed • Consider the following statements: • Which of the following would you prefer? 5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 6. Why Operator Overloading Is Needed (cont’d.) • Assignment and member selection are the only built-in operations on classes • Other operators cannot be applied directly to class objects • Operator overloading: extends 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 6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 7. Operator Overloading • Most existing C++ operators can be overloaded to manipulate class objects • Cannot create new operators • Operator function: overloads an operator – Use reserved word operator as the function name 7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 operator function declaration in the class definition – Write the definition of the operator function 8C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 9. Overloading an Operator: Some Restrictions • Cannot change precedence or associativity • 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 9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 11. Friend Functions of Classes • Friend function (of a class): 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 11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 12. Definition of a friend Function • "friend" doesn’t appear in function definition • When writing the friend function definition – The name of the class and the scope resolution operator are not used 12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 13. Operator Functions as Member and Nonmember Functions • To overload (), [], ->, or = for a class, the function must be a member of the class • If 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 13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 15. Overloading the Binary Operators as Member Functions • Function prototype (included in the class definition): • Function definition: 15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 16. Overloading the Arithmetic or Relational Operators as Nonmember Functions • Function prototype (included in class definition): • Function definition: 16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 – Same applies to the function that overloads >> 17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 18. Overloading the Stream Insertion Operator (<<) • Function prototype: • Function definition: 18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 19. Overloading the Stream Extraction Operator (>>) • Function prototype: • Function definition: 19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 20. Overloading the Assignment Operator (=) • Function prototype: • Function definition: 20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 22. Overloading the Increment (++) and Decrement (--) Operators • General syntax to overload the pre-increment operator ++ as a member function – Function prototype: – Function definition: 22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 23. Overloading the Increment (++) and Decrement (--) Operators (cont’d.) • General syntax to overload the pre-increment operator ++ as a nonmember function: – Function prototype: – Function definition: 23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 24. Overloading the Increment (++) and Decrement (--) Operators (cont’d.) • General syntax to overload the post- increment operator ++ as a member function: – Function prototype: – Function definition: 24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 25. Overloading the Increment (++) and Decrement (--) Operators (cont’d.) • Syntax to overload the post-increment operator ++ as a nonmember function: – Function prototype: – Function definition: 25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 26. Operator Overloading: Member Versus Nonmember • 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 26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 27. Operator Overloading: Member Versus Nonmember (cont’d.) • Overload + as a nonmember function – Must pass both objects as parameters – Code may be somewhat clearer this way 27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 28. Classes and Pointer Member Variables (Revisited) • Recall that 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 28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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: 30C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 31. Function Overloading • Overloading a function: several functions with the same name, but different parameters – Parameter list determines which function will execute – Must provide the definition of each function 31C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 32. Templates • 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 32C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 33. Templates (cont’d.) • class in the heading refers to any user- defined type or built-in type • Type: a formal parameter to the template • Just as variables are parameters to functions, data types are parameters to templates 33C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 34C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 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 35C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 36. Header File and Implementation File of a Class Template • 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 36C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 37. Header File and Implementation File of a Class Template (cont’d.) • Another alternative: put class definition and function definitions in separate files – Include directive to implementation file at the end of header file • In either case, function definitions and client code are compiled together 37C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 38. Summary • 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 38C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 39. Summary (cont’d.) • 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 39C++ Programming: From Problem Analysis to Program Design, Seventh Edition
  • 40. Summary (cont’d.) • Classes with pointer variables must overload the assignment operator, and include both a copy constructor and deconstructor • Templates: – 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 40C++ Programming: From Problem Analysis to Program Design, Seventh Edition