You are on page 1of 36

11.

Operator
Overloading - 02
Oritented Object Programming C++: Chapter 06

 Time: 120 Minutes


 Instructor: Thang, Nguyen Chien
 Email: chienthangplus@gmail.com
 Phone: 0349 688 571
Previous lessons

1. Introduction C++
 C++ vs C
 Compilers, IDEs
2. C++ Language Basics
 Types: int, float, double
 Control Structures: if, for, while, switch-case…
3. Functions, Arrays, Strings and Parameter Passing
 void func(int); void func(int, int = 1);
 int arr[10], brr[2][3];
 #include <cstring>; char str[10];
 #include <string>; string s;

Duy Tan University


2
Previous lessons (cont.)

4. Pointers and Reference Variables


 int &ref = a;
 int *p = &a; cout << *p;
 void iSwap(int* x, int& y);
 int* pD = new int; delete pD;
 int* pArr = new int; delete pArr[];
 Heap, Stack
5. Classes: Declaring and Using Objects
 public, private data members, member functions
 Constructors, destructors
 this pointer, static member, friends
Duy Tan University
3
Previous lessons (cont.)

6. Operator Overloading – 01
 rtype operator#(arguments) {...}
Binary operator overloading:
<left_operand> <binary_operator> <right_operand>
 Fraction fa(1, 2), fb(2, 3);
 fa = fa + fb;
 fa = fa – fb;

Duy Tan University


4
Contents

IV. cout, cin and input/output operators


 What are cin, out?
 How to use cin, cout, << and >>?
V. Overloading copy operator: =
 What is the special?
 How to use the copy operator?
VI. Overloading unary operators
 Overloading prefix unary operators
 Overloading postfix unary operators
 How operators ++, -- work?
Duy Tan University
5
IV. cout, cin and input/output operators

 Example: Output
int main() { Enter a number: 12
12
int number;
cout << "Enter a number: ";
cin >> number;
cout << number;
return 0;
}
 What are cout and cin?

Duy Tan University


6
IV. cout, cin and input/output operators (cont)

 cout is a global instance of ostream class


 cin is a global instance of istream class
 The classes defined binary operators >> and << for
input and output, example for illustration:
class ostream {
public:
ostream& operator<<(int _Val) {...};
};
class istream {
public:
istream& operator>>(int& _Val) {...};
};
Duy Tan University
7
Discussion

 How to the define operators <<, >> Fraction to do:


class Fraction {
private:
long nu, de; // numerator, denominator;
public:
Fraction(long n = 0, long d = 1) {...}
};
int main() {
Fraction fa(1, 2), fb;
cout << fa << endl; // 1/2
cin >> fb; // Enter fb’s numerator, denominator;
cout << fb << endl; // display fb
return 0;
} 8 Duy Tan University
Discussion (cont.)
Can I define the operators
as non-static member
 Solutions: functions?
class Fraction {
...
friend ostream& operator<<(ostream&,const Fraction&);
friend istream& operator>>(istream&,Fraction&);
};
ostream& operator<<(ostream& os, const Fraction& f){
os << f.nu << "/" << f.de;
return os;
}
istream& operator>>(istream& is, Fraction& f) {
is >> f.nu >> f.de;
return is;
} 9 Duy Tan University
Contents

IV. cout, cin and input/output operators


 What are cin, out?
 How to use cin, cout, << and >>?
V. Overloading copy operator: =
 What is the special?
 How to use the copy operator?
VI. Overloading unary operators
 Overloading prefix unary operators
 Overloading postfix unary operators
 How operators ++, -- work?
Duy Tan University
10
V. Overloading copy operator

 Example:
int main() {
Fraction f1(1, 2);
Fraction f2 = f1; The copy constructor
f1 = 1;
f1 += 1;
f1 = f2; The copy operator
f1 += f2;
}
 What is the copy operator?
 How to declare the operator for Fraction class?
Duy Tan University
11
V. Overloading copy operator (cont.)

 Copy operator is an assignment operator


It is used to copy data from an object to another
object of the same type.
It needs to be an non-static member function:
class T { // “T” is the class name
T& operator#(const T& right) {…}
};
If not defined, the compiler will generate a default
one
• Which copies the data members from the right operand

Duy Tan University


12
Example

 Define copy operator = to do this:


class Fraction {
...
friend ostream& operator<<(ostream&,const Fraction&);
friend istream& operator>>(istream&,Fraction&);
};
int main() {
Fraction f1, f2(3, 4);
f1 = f2;
cout << f1;
return 0;
}

Duy Tan University


13
Example (cont.)

 The solution:
class Fraction {
...
Fraction& operator=(const Fraction& f) {
nu = f.nu;
de = f.de;
return *this;
}
};

Duy Tan University


14
Discussion

 When/Why we need to define copy operator?


When we have pointers
or when we have any run time allocation of
resource like file handle, a network connection..etc
 Example:
class FractionList {
Fraction* list;
int size;
public:
FractionList() {...}
friend ostream& operator<<(...) {...}
friend istream& operator>>(...) {...}
};
Duy Tan University
15
Contents

IV. cout, cin and input/output operators


 What are cin, out?
 How to use cin, cout, << and >>?
V. Overloading copy operator: =
 What is the special?
 How to use the copy operator?
VI. Overloading unary operators
 Overloading prefix unary operators
 Overloading postfix unary operators
 How operators ++, -- work?
Duy Tan University
16
VI. Overloading unary operators

 General unary operators form:


Prefix operator
• <unary_operator><operand>
Postfix operator
• <operand><unary_operator>
 Examples:
 int a = 10;
 cout << a++ << endl; // postfix increment
 cout << ++a << endl; // prefix increment
 cout << -a << endl; // unary minus
 cout << &a << endl; // Address of
 cout << !a << endl; // NOT
Duy Tan University
17
VI.1. Overloading prefix unary operators

 A prefix unary operator can be overloaded:


as a non-member function with one argument:
rtype operator#(type operand){…}
• It often declared as friend of the class of the operand
for performance reasons
Or as a non-static member function without
arguments
class type{
rtype operator#() {…}
};

Duy Tan University


18
Example

 Give class Fraction:


class Fraction {
private:
long nu, de; // numerator, denominator;
public:
Fraction(long n = 0, long d = 1) {...}
friend ostream& operator<<(...) {...}
friend istream& operator>>(...) {...}
};

Duy Tan University


19
Example 01

 Write prefix unary operator - to do:


int main() { Output
Fraction f1(1, 2), f2;
-f1: -1/2
cout << "-f1: " << -f1 << endl;
f1: 1/2
cout << "f1: " << f1 << endl; f2: -1/2
f2 = -f1;
cout << "f2: " << f2;
return 0;
}
 As a non-static member function?
 As a non-member function?
Duy Tan University
20
Example 01 (cont.)

 Solution 01: as a non-static member function


class Fraction {
...
friend Fraction operator-(const Fraction& f);
};
Fraction operator-(const Fraction& f) {
return Fraction(-f.nu, f.de);
}
 Solution 02: as a non-static member function
class Fraction {
...
Fraction operator-(){ return Fraction(-nu, de);}
};
Duy Tan University
21
Example 02

 Write prefix unary operator ++ to do:


Output
int main() {
Fraction f1(1, 2); f1: 1/2
cout << "f1: " << f1 << endl; ++f1: 3/2
cout << "++f1: " << ++f1 << endl; f1: 3/2
cout << "f1: " << f1 << endl; ++(++f1): 7/2
cout << "++(++f1): " << ++(++f1) << endl;
return 0;
}
 As a non-static member function?
 As a non-member function?

Duy Tan University


22
Example 02 (cont.)

 Solution 01: as a non-static member function


class Fraction { ...
friend Fraction& operator++(Fraction& f);
};
Fraction& operator++(Fraction& f) {
f.nu = f.nu + f.de;
return f;
}
 Solution 02: as a non-static member function
class Fraction { ...
Fraction& operator++()
{ nu = nu + de; return *this; }
};
Duy Tan University
23
Example 03

 Write prefix unary operator -- to do: Output


int main() {
f1: 1/2
Fraction f1(1, 2);
--f1: -1/2
cout << "f1: " << f1 << endl;
f1: -1/2
cout << "--f1: " << --f1 << endl; --(--f1): -5/2
cout << "f1: " << f1 << endl;
cout << "--(--f1): " << --(--f1) << endl;
return 0;
}
 As a non-static member function?
 As a non-member function?

Duy Tan University


24
Example 03 (cont.)

 Solution 01: as a non-static member function


class Fraction { ...
friend Fraction& operator--(Fraction& f);
};
Fraction& operator--(Fraction& f) {
f.nu = f.nu - f.de;
return f;
}
 Solution 02: as a non-static member function
class Fraction { ...
Fraction& operator--()
{ nu = nu - de; return *this; }
};
Duy Tan University
25
VI.2. Overloading postfix unary operators

 A postfix unary operator can be overloaded:


as a non-member function with two arguments
rtype operator#(type operand, int){…}
• It often declared as friend of the class of the operand
for performance reasons
Or as a non-static member function a argument
class type{
rtype operator#(int) {…}
};
 Postfix unary operators can be overloaded: ++, --,
 int a = 10; a++; a--;

Duy Tan University


26
Example 01

 Write the postfix unary operator ++ to do:


int main() {
Fraction f(1, 2); Output
cout << "f: " << f << endl; f: 1/2
cout << "f++: " << f++ << endl; f++: 1/2
cout << "f: " << f << endl; f: 3/2
return 0;
}
 As a non-static member function?
 As a non-member function?

Duy Tan University


27
Example 01 (cont.)

 Solution 01: as a non-static member function


class Fraction { ...
friend Fraction operator++(Fraction& f, int);
};
Fraction operator++(Fraction& f, int) {
Fraction tmp(f.nu, f.de);
f.nu = f.nu + f.de;
return tmp;
}

Duy Tan University


28
Example 01 (cont.)

 Solution 02: as a non-static member function


class Fraction {
...
Fraction operator++(int) {
Fraction tmp(nu, de);
nu = nu + de;
return *this;
}
};

Duy Tan University


29
Example 02

 Write the postfix unary operator -- to do:


int main() {
Fraction f(1, 2); Output
cout << "f: " << f << endl; f: 1/2
cout << "f--: " << f-- << endl; f--: 1/2
cout << "f: " << f << endl; f: -1/2
return 0;
}
 As a non-static member function?
 As a non-member function?

Duy Tan University


30
Example 02 (cont.)

 Solution 01: as a non-static member function


class Fraction { ...
friend Fraction operator--(Fraction& f, int);
};
Fraction operator--(Fraction& f, int) {
Fraction tmp(f.nu, f.de);
f.nu = f.nu - f.de;
return tmp;
}

Duy Tan University


31
Example 02 (cont.)

 Solution 02: as a non-static member function


class Fraction { ...
Fraction operator--(int) {
Fraction tmp(nu, de);
nu = nu - de;
return *this;
}
};

Duy Tan University


32
Contents

IV. cout, cin and input/output operators


 What are cin, out?
 How to use cin, cout, << and >>?
V. Overloading copy operator: =
 What is the special?
 How to use the copy operator?
VI. Overloading unary operators
 Overloading prefix unary operators
 Overloading postfix unary operators
 How operators ++, -- work?
Duy Tan University
33
Summary

 cout is a global instance of ostream class


 cin is a global instance of istream class
 The classes defined operators >> and << for input and
output
 friend ostream& operator<<(ostream&,const Fraction&);
 friend istream& operator>>(istream&, Fraction&);
 Copy operator is an assignment operator
 It is used to copy data from an object to another object of the
same type
 It needs to be an non-static member function
class T { T& operator#(const T& right) { … } };

Duy Tan University


34
Summary (cont.)

 General unary operators form:


 Prefix operator: <unary_operator><operand>
 Postfix operator: <operand><unary_operator>
 Overloading prefix unary operators
 rtype operator#(type operand){…}
 class type { rtype operator#() { … } };
 Overloading postfix unary operators
 rtype operator#(type operand, int){…}
 class type { rtype operator#(int) { … } };

Duy Tan University


35
Problems

1. Define all operators needed for Vector class: add,


substract, multiply with a number
2. Define operators needed for Fraction class: >>, <<,
*, /, ++, --, -
3. Define operators needed for iString: <<, >>, + (with
a iString, a string, a character, a number), =, []
(element at)
4. Write class FractionList manipulate dynamic arrays
of Fractions: << (add an element, add elements,
merge to list, display the list), =, >>, []

Duy Tan University


36

You might also like