INTRODUCTION TO PROGRAM DESIGN
Lecture 10: Operator Overloading &
Class string
Yun-Shan Hsieh (謝昀珊),
Department of Computer Science and Information Engineering, NCKU
Acknowledgement: Slides template by https://slidesgo.com
Standard Library string
2
Standard Library string (Cont.)
3
Standard Library string (Cont.)
4
Standard Library string (Cont.)
5
Standard Library string (Cont.)
6
Standard Library string (Cont.)
7
Operators Not Required or Allowed for
Overloading
• Operators you need not overload
• The assignment operator (=) performs memberwise copy by default, but
should be overloaded for classes with pointer members.
• The address (&) operator returns a pointer to the object; this operator
also can be overloaded.
• The comma operator evaluates both expressions left to right and returns
the right one; it can also be overloaded.
• Operators you cannot overload
8
Rules and Restrictions on Operator Overloading
• Operator precedence cannot be changed; use parentheses to control
evaluation order.
• Operator associativity is fixed and cannot be changed by overloading.
• Operator arity (unary/binary) is fixed; ternary ?: cannot be overloaded.
• Only existing operators can be overloaded; new ones cannot be
created.
• Operators can't be overloaded for built-in types
(e.g., changing int + int).
• Related operators (e.g., + and +=) must be overloaded separately.
• (), [], ->, and assignment operators must be overloaded as class
members; others can be member or non-member functions. 9
Overloading Binary Operators
• Binary overloaded operators as member functions
y.operator<(z);
class String {
public:
bool operator<(const String&) const;
...
};
• Binary overloaded operators as non-member functions
operator<(y, z);
bool operator<(const String&, const String&);
10
Overloading the Binary Stream Insertion and
Stream Extraction Operators
11
Overloading the Binary Stream Insertion and
Stream Extraction Operators (Cont.)
12
Overloading the Binary Stream Insertion and
Stream Extraction Operators (Cont.)
13
Overloading the Binary Stream Insertion and
Stream Extraction Operators (Cont.)
14
Overloading the Binary Stream Insertion and
Stream Extraction Operators (Cont.)
15
Overloading Unary Operators
• Unary overloaded operators as member functions
s.operator!();
class String {
public:
bool operator!() const;
...
};
• Unary overloaded operators as non-member functions
operator!(s);
bool operator!(const String&);
16
Overloading the Increment and Decrement
Operators
• Overloading the prefix • Overloading the postfix
increment operator increment operator
• Member-function call • Member-function call
d1.operator++() d1.operator++(0)
Date& operator++(); Date operator++(int);
• Non-member function • Non-member function
operator++(d1) operator++(d1, 0)
Date& operator++(Date&); Date operator++(Date&, int);
17
Case Study: A Date Class
18
Case Study: A Date Class (Cont.)
19
Case Study: A Date Class (Cont.)
20
Case Study: A Date Class (Cont.)
21
Case Study: A Date Class (Cont.)
22
Case Study: A Date Class (Cont.)
23
Case Study: A Date Class (Cont.)
24
Case Study: A Date Class (Cont.)
25
Case Study: A Date Class (Cont.)
26
Case Study: A Date Class (Cont.)
27
Case Study: A Date Class (Cont.)
28
Dynamic Memory Management
• Obtaining dynamic memory with new
Time* timePtr{new Time};
• Releasing dynamic memory with delete
delete timePtr;
• Initializing dynamic memory
double* ptr{new double{3.14159}};
Time* timePtr{new Time{12, 45, 0}};
• Dynamically allocating built-in arrays with new[]
int* gradesArray{new int[10]{}};
• Releasing dynamically allocated built-in arrays with delete[]
delete[] gradesArray; 29
Problems of Built-in Array
• C++ doesn’t check array bounds, so out-of-range access is possible.
• Array indices must be 0 to n - 1; custom index ranges aren’t
allowed.
• Entire arrays can’t be input/output at once; each element must be
handled individually.
• Arrays can’t be compared using == or relational operators—they
compare addresses, not contents.
• When passing arrays to functions, their sizes must be passed
separately.
• One array can’t be assigned to another using the assignment
operator.
30
Case Study: Array Class
31
Case Study: Array Class (Cont.)
32
Case Study: Array Class (Cont.)
33
Case Study: Array Class (Cont.)
34
Case Study: Array Class (Cont.)
35
Case Study: Array Class (Cont.)
36
Case Study: Array Class (Cont.)
37
Case Study: Array Class (Cont.)
38
Case Study: Array Class (Cont.)
39
Case Study: Array Class (Cont.)
40
Case Study: Array Class (Cont.)
41
Case Study: Array Class (Cont.)
42
Case Study: Array Class (Cont.)
43
Converting Between Types
• Conversion constructor
MyClass(int value);
• Conversion operators
MyClass::operator string() const;
s.operator string()
• Overloaded cast operator functions
MyClass::operator int() const;
MyClass::operator OtherClass() const;
• Implicit calls to cast operators and conversion constructors
display(s); // argument expected to be a string object
44
Single-argument Constructors and Implicit
Conversions
45
Single-argument Constructors and Implicit
Conversions (Cont.)
46
Demonstrating an explicit Constructor
47
References
• https://en.cppreference.com/w/cpp/header/cmath
• https://en.cppreference.com/w/cpp/chrono/c/time
48
Thank You Very Much!
Questions & Answers?
49