SlideShare a Scribd company logo
1 of 25
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
OUTLINE
11.1 The this Pointer and
Constant Member
Functions
11.2 Static Members
11.3 Friends of Classes
11.4 Memberwise
Assignment
11.5 Copy Constructors
11.6 Operator Overloading
11.7 Rvalue References and
Move Operations
11-1
11.8 Type Conversion
Operators
11.9 Convert Constructors
11.10 Aggregation and
Composition
11.11 Inheritance
11.12 Protected Members and
Class Access
11.13 Constructors,
Destructors, and Inheritance
11.14 Overriding Base Class
Functions
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
KEY CONCEPT
1-2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.6 Operator Overloading
•Operators such as =, +, and others can be
redefined for use with objects of a class
•The name of the function for the overloaded
operator is operator followed by the operator
symbol, e.g.,
operator+ is the overloaded + operator and
operator= is the overloaded = operator
11-3
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Operator Overloading
•Operators can be overloaded as
- instance member functions, or as
- friend functions
•The overloaded operator must have the same
number of parameters as the standard version.
For example, operator= must have two
parameters, since the standard = operator
takes two parameters.
11-4
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
SPECIAL NOTE
1-5
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
SPECIAL NOTE
1-6
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloading Operators as Instance
Members 1 of 2
A binary operator that is overloaded as an instance
member needs only one parameter, which
represents the operand on the right:
class OpClass
{
private:
int x;
public:
OpClass operator+(OpClass right);
};
11-7
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloading Operators as Instance
Members 2 of 2
•The left operand of the overloaded binary operator
is the calling object
•The implicit left parameter is accessed through the
this pointer
OpClass OpClass::operator+(OpClass r)
{ OpClass sum;
sum.x = this->x + r.x;
return sum;
}
11-8
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Invoking an Overloaded Operator
•An overloaded operator can be invoked as a
member function:
OpClass a, b, s;
s = a.operator+(b);
•It can also be invoked in the more conventional
manner:
OpClass a, b, s;
s = a + b;
11-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloading Assignment 1 of 3
•Overloading the assignment operator solves
problems with object assignment when an object
contains a pointer to dynamic memory.
•The assignment operator is most naturally
overloaded as an instance member function
•It needs to return a value of the assigned object to
allow cascaded assignments such as
a = b = c;
11-10
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloading Assignment 2 of 3
Assignment overloaded as a member function:
class CpClass
{
int *p;
public:
CpClass(int v=0)
{ p = new int; *p = v;
~CpClass(){delete p;}
CpClass operator=(CpClass);
};
11-11
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloading Assignment 3 of 3
The implementation returns a value:
CpClass CpClass::operator=(CpClass r)
{
*p = *r.p;
return *this;
};
Invoking the assignment operator:
CpClass a, x(45);
a.operator=(x); // either of these
a = x; // lines can be used
11-12
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Notes on Overloaded Operators
•Overloading can change the entire meaning of an
operator
•Most operators can be overloaded
•You cannot change the number of operands of
the operator
•Cannot overload the following operators:
?: . .* sizeof
11-13
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloaded Operator – Where Does It Go?
•Overloaded operator as a member function of a
class
–Access to private members
–Use of the this pointer
•Overloaded operator as a separate function
outside of a class
–Must declare the operator function as a friend
of the class to have access to private
members
11-14
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloading Types of Operators
•++, -- operators overloaded differently for
prefix vs. postfix notation
•Overloaded relational operators should return
a bool value
•Overloaded stream operators >>, << must
return istream, ostream objects and take
istream, ostream objects as parameters.
These are best overloaded as standalone
operator functions.
11-15
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Overloaded [] Operator
•Can be used to create classes that behave
like arrays
•Can provide bounds-checking on subscripts
•Overloaded [] returns a reference to an
object, not the object itself
11-16
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
OUTLINE
11.1 The this Pointer and
Constant Member
Functions
11.2 Static Members
11.3 Friends of Classes
11.4 Memberwise
Assignment
11.5 Copy Constructors
11.6 Operator Overloading
11.7 Rvalue References and
Move Operations
11-17
11.8 Type Conversion
Operators
11.9 Convert Constructors
11.10 Aggregation and
Composition
11.11 Inheritance
11.12 Protected Members and
Class Access
11.13 Constructors,
Destructors, and Inheritance
11.14 Overriding Base Class
Functions
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
KEY CONCEPT
1-
18
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
11.7 Rvalue References and Move
Operations
• Introduced in C++ 11
• An rvalue is a temporary value that is unnamed.
ex: double x; x = pow(3.0, 2);
pow(3.0, 2) is an rvalue. It is used for the
assignment statement, then the memory is
deallocated and is inaccessible.
11-19
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Rvalue Reference
• Introduced in C++ 11
• An rvalue reference is a reference variable that
refers to an rvalue. It is declared with the &&
operator:
double && powRef = pow(3.0, 2);
cout << powRef << endl;
•Declaring the rvalue reference assigns a name to
the temporary location holding the value of the
expression, making it no longer an rvalue
11-20
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
SPECIAL NOTE
1-
21
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Move Assignment, Move Constructor
• Introduced in C++ 11:
• Copy assignments and copy constructors are used
when objects contain dynamic memory.
Deallocating memory in the target object, then
allocating memory for the copy, then destroying the
temporary object, is resource-intensive.
• Move assignment and move constructors, which
use rvalue references, are much more efficient.
11-22
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Move Assignment/Constructor Details
•Move assignment (overloaded = operator) and
move constructor use an rvalue reference for the
parameter
•The dynamic memory locations can be “taken”
from the parameter and assigned to the members
in the object invoking the assignment.
•Set dynamic fields in the parameter to nullptr
before the function ends and the parameter’s
destructor executes.
11-23
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Moving is Not New
•Though introduced in C++ 11, move operations
have already been used by the compiler:
–when a non-void function returns a value
–when the right side of an assignment
statement is an rvalue
–on object initialization from a temporary object
11-24
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Default Class Operations
•Managing the details of a class implementation is
tedious and potentially error-prone.
•The C++ compiler can generate a default
constructor, copy constructor, copy assignment
operator, move constructor, and destructor.
•If you provide your own implementation of any of
these functions, you should provide your own
implementation for all of them.
11-25

More Related Content

Similar to C++ Chapter 11 OOP - Part 4

C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3
DanWooster1
 
C++ Chapter 11 OOP - Part 2
C++ Chapter 11 OOP - Part 2C++ Chapter 11 OOP - Part 2
C++ Chapter 11 OOP - Part 2
DanWooster1
 
C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7
DanWooster1
 
C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6
DanWooster1
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
farhan amjad
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kumar
 
Accelerating query processing with materialized views in Apache Hive
Accelerating query processing with materialized views in Apache HiveAccelerating query processing with materialized views in Apache Hive
Accelerating query processing with materialized views in Apache Hive
DataWorks Summit
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
DanWooster1
 

Similar to C++ Chapter 11 OOP - Part 4 (20)

C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 3
 
C++ Chapter 11 OOP - Part 2
C++ Chapter 11 OOP - Part 2C++ Chapter 11 OOP - Part 2
C++ Chapter 11 OOP - Part 2
 
C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 7
 
C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6C++ Chapter 11 OOP - Part 6
C++ Chapter 11 OOP - Part 6
 
Unit3_OOP-converted.pdf
Unit3_OOP-converted.pdfUnit3_OOP-converted.pdf
Unit3_OOP-converted.pdf
 
Oops
OopsOops
Oops
 
Apache Ambari - What's New in 1.7.0
Apache Ambari - What's New in 1.7.0Apache Ambari - What's New in 1.7.0
Apache Ambari - What's New in 1.7.0
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Streamline Hadoop DevOps with Apache Ambari
Streamline Hadoop DevOps with Apache AmbariStreamline Hadoop DevOps with Apache Ambari
Streamline Hadoop DevOps with Apache Ambari
 
Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
Accelerating query processing with materialized views in Apache Hive
Accelerating query processing with materialized views in Apache HiveAccelerating query processing with materialized views in Apache Hive
Accelerating query processing with materialized views in Apache Hive
 
Ch7Structs.pptx
Ch7Structs.pptxCh7Structs.pptx
Ch7Structs.pptx
 
XOOPS 2.6.0 Service Manager
XOOPS 2.6.0  Service Manager XOOPS 2.6.0  Service Manager
XOOPS 2.6.0 Service Manager
 
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
 
Accelerating query processing
Accelerating query processingAccelerating query processing
Accelerating query processing
 
Apache Hadoop YARN: Past, Present and Future
Apache Hadoop YARN: Past, Present and FutureApache Hadoop YARN: Past, Present and Future
Apache Hadoop YARN: Past, Present and Future
 
CSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - ClassesCSCI 238 Chapter 07 - Classes
CSCI 238 Chapter 07 - Classes
 

More from DanWooster1

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
DanWooster1
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
DanWooster1
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
DanWooster1
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
DanWooster1
 

More from DanWooster1 (20)

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
 
Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1
 
Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3
 
Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2
 
Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1
 
Upstate CSCI 200 Java Chapter 8 - Arrays
Upstate CSCI 200 Java Chapter 8 - ArraysUpstate CSCI 200 Java Chapter 8 - Arrays
Upstate CSCI 200 Java Chapter 8 - Arrays
 
Upstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPUpstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOP
 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using Classes
 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & Expressions
 
CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

C++ Chapter 11 OOP - Part 4

  • 1. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved OUTLINE 11.1 The this Pointer and Constant Member Functions 11.2 Static Members 11.3 Friends of Classes 11.4 Memberwise Assignment 11.5 Copy Constructors 11.6 Operator Overloading 11.7 Rvalue References and Move Operations 11-1 11.8 Type Conversion Operators 11.9 Convert Constructors 11.10 Aggregation and Composition 11.11 Inheritance 11.12 Protected Members and Class Access 11.13 Constructors, Destructors, and Inheritance 11.14 Overriding Base Class Functions
  • 2. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1-2
  • 3. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.6 Operator Overloading •Operators such as =, +, and others can be redefined for use with objects of a class •The name of the function for the overloaded operator is operator followed by the operator symbol, e.g., operator+ is the overloaded + operator and operator= is the overloaded = operator 11-3
  • 4. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Operator Overloading •Operators can be overloaded as - instance member functions, or as - friend functions •The overloaded operator must have the same number of parameters as the standard version. For example, operator= must have two parameters, since the standard = operator takes two parameters. 11-4
  • 5. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved SPECIAL NOTE 1-5
  • 6. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved SPECIAL NOTE 1-6
  • 7. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloading Operators as Instance Members 1 of 2 A binary operator that is overloaded as an instance member needs only one parameter, which represents the operand on the right: class OpClass { private: int x; public: OpClass operator+(OpClass right); }; 11-7
  • 8. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloading Operators as Instance Members 2 of 2 •The left operand of the overloaded binary operator is the calling object •The implicit left parameter is accessed through the this pointer OpClass OpClass::operator+(OpClass r) { OpClass sum; sum.x = this->x + r.x; return sum; } 11-8
  • 9. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Invoking an Overloaded Operator •An overloaded operator can be invoked as a member function: OpClass a, b, s; s = a.operator+(b); •It can also be invoked in the more conventional manner: OpClass a, b, s; s = a + b; 11-9
  • 10. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloading Assignment 1 of 3 •Overloading the assignment operator solves problems with object assignment when an object contains a pointer to dynamic memory. •The assignment operator is most naturally overloaded as an instance member function •It needs to return a value of the assigned object to allow cascaded assignments such as a = b = c; 11-10
  • 11. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloading Assignment 2 of 3 Assignment overloaded as a member function: class CpClass { int *p; public: CpClass(int v=0) { p = new int; *p = v; ~CpClass(){delete p;} CpClass operator=(CpClass); }; 11-11
  • 12. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloading Assignment 3 of 3 The implementation returns a value: CpClass CpClass::operator=(CpClass r) { *p = *r.p; return *this; }; Invoking the assignment operator: CpClass a, x(45); a.operator=(x); // either of these a = x; // lines can be used 11-12
  • 13. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Notes on Overloaded Operators •Overloading can change the entire meaning of an operator •Most operators can be overloaded •You cannot change the number of operands of the operator •Cannot overload the following operators: ?: . .* sizeof 11-13
  • 14. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloaded Operator – Where Does It Go? •Overloaded operator as a member function of a class –Access to private members –Use of the this pointer •Overloaded operator as a separate function outside of a class –Must declare the operator function as a friend of the class to have access to private members 11-14
  • 15. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloading Types of Operators •++, -- operators overloaded differently for prefix vs. postfix notation •Overloaded relational operators should return a bool value •Overloaded stream operators >>, << must return istream, ostream objects and take istream, ostream objects as parameters. These are best overloaded as standalone operator functions. 11-15
  • 16. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Overloaded [] Operator •Can be used to create classes that behave like arrays •Can provide bounds-checking on subscripts •Overloaded [] returns a reference to an object, not the object itself 11-16
  • 17. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved OUTLINE 11.1 The this Pointer and Constant Member Functions 11.2 Static Members 11.3 Friends of Classes 11.4 Memberwise Assignment 11.5 Copy Constructors 11.6 Operator Overloading 11.7 Rvalue References and Move Operations 11-17 11.8 Type Conversion Operators 11.9 Convert Constructors 11.10 Aggregation and Composition 11.11 Inheritance 11.12 Protected Members and Class Access 11.13 Constructors, Destructors, and Inheritance 11.14 Overriding Base Class Functions
  • 18. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved KEY CONCEPT 1- 18
  • 19. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved 11.7 Rvalue References and Move Operations • Introduced in C++ 11 • An rvalue is a temporary value that is unnamed. ex: double x; x = pow(3.0, 2); pow(3.0, 2) is an rvalue. It is used for the assignment statement, then the memory is deallocated and is inaccessible. 11-19
  • 20. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Rvalue Reference • Introduced in C++ 11 • An rvalue reference is a reference variable that refers to an rvalue. It is declared with the && operator: double && powRef = pow(3.0, 2); cout << powRef << endl; •Declaring the rvalue reference assigns a name to the temporary location holding the value of the expression, making it no longer an rvalue 11-20
  • 21. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved SPECIAL NOTE 1- 21
  • 22. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Move Assignment, Move Constructor • Introduced in C++ 11: • Copy assignments and copy constructors are used when objects contain dynamic memory. Deallocating memory in the target object, then allocating memory for the copy, then destroying the temporary object, is resource-intensive. • Move assignment and move constructors, which use rvalue references, are much more efficient. 11-22
  • 23. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Move Assignment/Constructor Details •Move assignment (overloaded = operator) and move constructor use an rvalue reference for the parameter •The dynamic memory locations can be “taken” from the parameter and assigned to the members in the object invoking the assignment. •Set dynamic fields in the parameter to nullptr before the function ends and the parameter’s destructor executes. 11-23
  • 24. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Moving is Not New •Though introduced in C++ 11, move operations have already been used by the compiler: –when a non-void function returns a value –when the right side of an assignment statement is an rvalue –on object initialization from a temporary object 11-24
  • 25. Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved Default Class Operations •Managing the details of a class implementation is tedious and potentially error-prone. •The C++ compiler can generate a default constructor, copy constructor, copy assignment operator, move constructor, and destructor. •If you provide your own implementation of any of these functions, you should provide your own implementation for all of them. 11-25