Download Operator Overloading: Friend Functions, Special Forms and Global Overloading and more Slides Computer Science in PDF only on Docsity!
Operator Overloading
Friend Functions & Special Forms
Operator Overloading Using a Friend Function
- Number of Parameters Accepted by an Overloaded Friend Operator Function
Depend Upon the Operator Type -- One for Unary Operators and Two for Binary
Operators
class complex{
int re, im;
public:
complex(int ip1 = 0, int ip2 = 0)
:re(ip1), im(ip2){}
friend complex operator+(complex, complex);
//Friend Operator + Function
complex operator+(complex a, complex b)
{return complex(a.re+b.re, a.im+b.im);}
main(){
complex one(1,1), two(2,2), three;
three = one + two; //three = operator+(one, two)
} Is a friend function necessary in this case?
No because LH operand is an instance of the class.
• Similar toGlobal Operator Overloading friend Function Overloading, Except the Keyword friend is Omitted and Global Functions
CANNOT ACCESS private Members
class complex{ //All Public Members!
public:
int re, im;
complex(int ip1 = 0, int ip2 = 0)
:re(ip1), im(ip2){}
void operator!(complex a)
int temp = a.re; a.re = a.im; a.im = temp;
cout << "Real: " << a.re << endl;
cout << "Imaginary: " << a.im << endl;
main()
complex one(1,2);
!one;
Overloading of Operators Having a Variable Arity
- Operators Such as - Can be Unary or Binary
- Overloading of Such Operators Involves Creating a Unary Function (One Operand)
and a Binary Function (Two Operands)
- Only if Both the Forms are Used, They Need to be Implemented
class number{
int n;
public:
number(int x = 0):n(x){}
number operator-(){n = -n; return *this;}
number operator-(number ip)
{n = n – ip.n; return *this;}
main(){
number one(1), two(2), three;
one = -one; //unary operator
three = one - two; //three.n = -
Prefix Overloaded Function -- Example
class number{
int n;
public:
number(int x):n(x){}; //Constructor
//prefix operator -- unary
number operator++();
};
number number::operator++(){
n++; return *this;}
main(){
number one(10); //one.n = 10
++one; //one.n = 11
}
Postfix Overloaded Function -- Example
- Postfix Operator is Implemented as a Binary Operator with an int Argument
with a Default Value of 0. When specifying an overloaded operator for the postfix form of the increment or decrement operator, the additional argument
must be of type int; specifying any other type generates an error.
class number{
int n;
public:
number(int x):n(x){}; //Constructor
//postfix operator -- binary -- int argument
number operator++(int);
};
number number::operator++(int y)
{if (y != 0) n += y; else n++; return *this;}
Special Overloading Forms
A Few Operators Require Special Treatments
During Overloading
Conversion Operator
const Array Operator
Function Call -- Parenthesis Operator
Stream Insertion -- << Operator
Stream Extraction -- >> Operator
Pointer to Member -- -> Operator
Assignment Operator
new Operator
delete Operator
Overloading Stream-Insertion and Stream-Extraction Operators
- Overloaded << and >> operators
- Must have left operand of types ostream &,
istream & respectively
- It must be a non-member function (left operand
not an object of the class)
- It must be a friend function if it accesses
private data members
31 output << "(" << num.areaCode << ") "
32 << num.exchange << "-" << num.line;
33 return output; // enables cout << a << b << c;
34 }
35
36 istream &operator>>( istream &input, PhoneNumber &num )
37 {
38 input.ignore(); // skip (
39 input >> setw( 4 ) >> num.areaCode; // input area code
40 input.ignore( 2 ); // skip ) and space
41 input >> setw( 4 ) >> num.exchange; // input exchange
42 input.ignore(); // skip dash (-)
43 input >> setw( 5 ) >> num.line; // input line
44 return input; // enables cin >> a >> b >> c;
45 }
46
47 int main()
48 {
49 PhoneNumber phone; // create object phone
50
51 cout << "Enter phone number in the form (123) 456-7890:\n";
52
53 // cin >> phone invokes operator>> function by
54 // issuing the call operator>>( cin, phone ).
55 cin >> phone;
56
57 // cout << phone invokes operator<< function by
58 // issuing the call operator<<( cout, phone ).
59 cout << "The phone number entered was: " << phone << endl;
60 return 0;
61 }
Docsity.com
Enter phone number in the form (123) 456-7890: (800) 555- The phone number entered was: (800) 555-
Converting between Types (cont)
- The compiler can call these functions to create
temporary objects.
- If s is not of type char *
Calls A::operator char *() const; for
cout << s;
Special overloading forms - Example