OOP-chapter 4
OOP-chapter 4
Operator Overloading
Overloading
1
Operator Overloading
Operator
Example:
• arithmetic operator: + , −, ∗, /
• logical operator: && and ||
• pointer operator: & and ∗
• memory management operator: new, delete[ ]
2
Operator Overloading
Operator overloading
Example:
3
Operator Overloading
4
Operator Overloading
5
Operator Overloading
Example:
class C{
public:
C(): x(0), y(0) {}
C(int xx, int yy): x(xx), y(yy) {}
private:
int x, y;
};
int main(){
C c1, c2(5,6);
C *ptr;
c1 = c2;
ptr = &c2;
}
6
Operator Overloading
7
Operator Overloading
xobj @ yobj
8
Operator Overloading
Overloading operator +
class C {
public:
C operator+( const C& ) const;
...
};
9
Operator Overloading
C a, b, c;
a = b.operator+( c );
a = b + c;
10
Operator Overloading
Example:
#include <iostream>
using namespace std;
class C{
public:
void print();
C operator+( const C& ) const;
C() : x(0), y(0) {}
C(int xx, int yy) : x(xx), y(yy) {}
private:
int x, y;
};
int main(){
C c1( 2, 3 );
C c2( 3, 4 );
C result = c1 + c2;
result.print();
}
11
Operator Overloading
12
Operator Overloading
#include <iostream>
using namespace std;
class Complex{
public:
Complex();
Complex( double );
Complex( double, double );
void print() const;
Complex operator+( const Complex& ) const;
Complex operator-( const Complex& ) const;
Complex operator*( const Complex& ) const;
Complex operator/( const Complex& ) const;
bool operator==( const Complex& ) const;
bool operator!=( const Complex& ) const;
private:
double real;
double imag;
};
Complex::Complex() {
real = imag = 0.0;
}
Complex::Complex( double re ) {
real = re;
imag = 0.0;
}
Complex::Complex( double re, double im ) {
real = re;
imag = im;
}
void Complex::print() const {
cout << real << " + " << imag << "i\n";
}
13
Operator Overloading
14
Operator Overloading
c3.print();
c4.print();
if ( c3 == c4 )
cout << "No way.";
else
cout << "Sure they are not equal.";
}
15
Operator Overloading
Overloading operator =
16
Operator Overloading
Example:
class Vector{
public:
Vector():size(0), ptr(0){cout << "default constructor" << endl; }
Vector(int);
Vector(const Vector&);
Vector& operator=( const Vector& );
// ...
private:
int size;
int* ptr;
};
Vector::Vector(int n){
size = n;
ptr = new int[size];
for ( int i =0; i<size; i++)
ptr[i] = 0;
cout << "constructor Vector(n)" << endl;
}
Vector::Vector(const Vector& rhs){
if( rhs.ptr != 0 ){
size = rhs.size;
ptr = new int[size];
for (int i=0; i<size; i++)
ptr[i] = rhs.ptr[i];
}
else{
ptr = 0;
size = 0;
}
cout << "copy constructor" << endl;
}
17
Operator Overloading
Example:
// overload = for class Vector
Vector& Vector::operator=( const Vector& rhs ){
if (this != &rhs){
if ( rhs.ptr != 0 ){
size = rhs.size;
delete [] ptr;
ptr = new int[size];
for ( int i=0; i<size; i++ )
ptr[i] = rhs.ptr[i];
}
else{
size = 0;
delete [] ptr;
ptr = 0;
}
}
cout << "assignment =" << endl;
return *this;
}
int main(){
Vector v1(5);
Vector v2;
v2 = v1;
Vector v3 = v2;
}
18
Operator Overloading
Note:
19
Operator Overloading
#include <iostream>
using namespace std;
class C{
public:
void print() const;
C operator!(); // unary operator; takes no argument
C() : x(0), y(0) {}
C(int xx, int yy) : x(xx), y(yy) {}
private:
int x;
int y;
};
C C::operator!(){
C tmp( -x, -y );
return tmp;
}
int main(){
C c1, c2( 2, 3 );
c1 = !c2;
c1.print();
c2.print();
}
20
Operator Overloading
int x = 6;
++x; // preincrement
x++; // postincrement
--x; // predecrement
x--; // postdecrement
21
Operator Overloading
Example
#include <iostream>
using namespace std;
class C{
public:
void print() const;
C operator++( );
C operator++(int);
C() : x(0), y(0) {}
C(int xx, int yy) : x(xx), y(yy) {}
private:
int x;
int y;
};
C C::operator++(){ // preincrement
x++;
y++;
return *this;
}
22
Operator Overloading
c = a++;
a.print();
c.print(); // x 1 y 1
c = ++b;
b.print();
c.print(); // x 2 y 2
}
23
Operator Overloading
To use @ as
x @ y
we can overload operator@ as a stand alone
function which takes two parameters: one of
type X and one of type Y.
operator@ ( X, Y )
24
Operator Overloading
Example:
25
Operator Overloading
C a, b, c;
a = operator+( b , c );
a = b + c;
26
Operator Overloading
27
Operator Overloading
Solution 1:
#include <iostream>
using namespace std;
class C{
public:
int getX() const{ return x; }
int getY() const{ return y; }
void print() const;
C() : x(0), y(0) {}
C(int xx, int yy) : x(xx), y(yy) {}
private:
int x;
int y;
};
void C::print() const {
cout << "x " << x << "y " << y << "\n";
}
int main(){
C c1( 2, 3 );
C c2( 3, 4 );
C result;
result = c1 + c2;
result.print();
return 0;
}
28
Operator Overloading
class C{
public:
C() : x(0), y(0) {}
C(int xx, int yy) : x(xx), y(yy) {}
void print() const;
friend C operator+( const C&, const C& );
private:
int x;
int y;
};
void C::print() const {
cout << "x " << x << "y " << y << "\n";
}
// as stand-alone friend
C operator+( const C& c1, const C& c2 ){
C tmp( c1.x + c2.x,
c1.y + c2.y );
return tmp;
}
int main(){
C c1( 2, 3 );
C c2( 3, 4 );
C result;
result = c1 + c2;
result.print();
return 0;
}
29
Operator Overloading
Operator functions:
As class member v.s. As stand-alone
a = operator+( b , c );
30
Operator Overloading
31
Operator Overloading
can be interpreted as
cout.operator<<( i );
cout.operator<<( s );
32
Operator Overloading
Example:
is now equivalent to
operator>>( cin, c );
which is evaluated as
cin >> c.x >> c.y;
33
Operator Overloading
class Complex{
public:
Complex();
Complex( double );
Complex( double, double );
friend Complex operator+( const Complex&, const Complex& );
friend Complex operator-( const Complex&, const Complex& );
friend Complex operator*( const Complex&, const Complex& );
friend Complex operator/( const Complex&, const Complex& );
friend bool operator==( const Complex&, const Complex& );
friend bool operator!=( const Complex&, const Complex& );
friend istream& operator>>( istream&, Complex& );
friend ostream& operator<<( ostream&, const Complex& );
private:
double real;
double imag;
};
Complex::Complex() {
real = imag = 0.0;
}
Complex::Complex( double re ) {
real = re;
imag = 0.0;
}
Complex::Complex( double re, double im ) {
real = re;
imag = im;
}
34
Operator Overloading
35
Operator Overloading
int main(){
Complex c1, c2;
return 0;
}
36
Operator Overloading
Note
Example:
class C{
C operator+(); // error! + is a binary operator
// ...
};
37