More Related Content
Similar to C++ Chapter 11 OOP - Part 4 (20)
More from DanWooster1 (20)
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