SlideShare a Scribd company logo
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

PPTX
C++ Chapter 11 OOP - Part 5
PPT
Subconsultas
PPTX
Mule transformers
PPTX
Functional Programming With Lambdas and Streams in JDK8
PPTX
Spring integration
PPTX
Java- Updates in java8-Mazenet solution
KEY
S2GX 2012 - Introduction to Spring Integration and Spring Batch
PPTX
Muletransformers
C++ Chapter 11 OOP - Part 5
Subconsultas
Mule transformers
Functional Programming With Lambdas and Streams in JDK8
Spring integration
Java- Updates in java8-Mazenet solution
S2GX 2012 - Introduction to Spring Integration and Spring Batch
Muletransformers

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

PPTX
C++ Chapter 11 OOP - Part 3
PPTX
C++ Chapter 11 OOP - Part 2
PPTX
C++ Chapter 11 OOP - Part 7
PPTX
C++ Chapter 11 OOP - Part 6
PDF
Unit3_OOP-converted.pdf
PPTX
PPTX
Apache Ambari - What's New in 1.7.0
PPT
Operator overloading
PPTX
Streamline Hadoop DevOps with Apache Ambari
PDF
Operator Overloading in C++
PPTX
Operator overloading
PPTX
3. Polymorphism.pptx
PDF
Ch-4-Operator Overloading.pdf
PPTX
Accelerating query processing with materialized views in Apache Hive
PPTX
Ch7Structs.pptx
PPT
C++ CH4 in funtion using c++ in all ab.ppt
PDF
XOOPS 2.6.0 Service Manager
PDF
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
PPTX
Accelerating query processing
PPTX
Apache Hadoop YARN: Past, Present and Future
C++ Chapter 11 OOP - Part 3
C++ Chapter 11 OOP - Part 2
C++ Chapter 11 OOP - Part 7
C++ Chapter 11 OOP - Part 6
Unit3_OOP-converted.pdf
Apache Ambari - What's New in 1.7.0
Operator overloading
Streamline Hadoop DevOps with Apache Ambari
Operator Overloading in C++
Operator overloading
3. Polymorphism.pptx
Ch-4-Operator Overloading.pdf
Accelerating query processing with materialized views in Apache Hive
Ch7Structs.pptx
C++ CH4 in funtion using c++ in all ab.ppt
XOOPS 2.6.0 Service Manager
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Accelerating query processing
Apache Hadoop YARN: Past, Present and Future
Ad

More from DanWooster1 (20)

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

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Lesson notes of climatology university.
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Types and Its function , kingdom of life
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Presentation on HIE in infants and its manifestations
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
VCE English Exam - Section C Student Revision Booklet
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Lesson notes of climatology university.
Institutional Correction lecture only . . .
Cell Types and Its function , kingdom of life
GDM (1) (1).pptx small presentation for students
Presentation on HIE in infants and its manifestations
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
RMMM.pdf make it easy to upload and study
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
O5-L3 Freight Transport Ops (International) V1.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
2.FourierTransform-ShortQuestionswithAnswers.pdf

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