Professional Documents
Culture Documents
Lesson 11 - 06. Operator Overloading - 02
Lesson 11 - 06. Operator Overloading - 02
Operator
Overloading - 02
Oritented Object Programming C++: Chapter 06
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;
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;
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?
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.)
The solution:
class Fraction {
...
Fraction& operator=(const Fraction& f) {
nu = f.nu;
de = f.de;
return *this;
}
};