Inline Functions: Disadv: Code - More Memory
Inline Functions: Disadv: Code - More Memory
int main()
{
myclass o1,o2;
o1.seti(10);
cout<<o1.geti()<<“\n”;
cout<<o2.geti()<<“\n”;
}
*p=1000;
cout<<“Integer at p:”<< *p << “\n” ;
delete p; //release memory
}
Member vs non-member
Operator functions can be member or non-member functions
When overloading ( ), [ ], -> or any of the assignment
operators, you must use a member function
Operator functions as member functions
Leftmost operand must be an object (or reference to an
object) of the class
If left operand of a different type, operator function must be a
non-member function
Classic example << operator
Operator functions as non-member functions
Must be friends if needs to access private or protected
members
Enable the operator to be commutative
Example: Overloading the Stream-insertion (<<)
and Stream-extraction
Faculty of(>>)
IT, AFTC Operators
Operator Overloading
Overloading an operator does not
change:
the operator precedence,
the associativity of the operator,
the meaning of how the operator works on
objects of built-in types.
An Example
4 #include <iostream>
5
6 using std::cout;
7 using std::cin;
8 using std::endl;
9 using std::ostream;
10 using std::istream;
11
12 #include <iomanip>
Notice function prototypes for
13
overloaded operators >> and <<
14 using std::setw; They must be friend functions.
15
16 class PhoneNumber {
17 friend ostream &operator<<( ostream&, const PhoneNumber & );
18 friend istream &operator>>( istream&, PhoneNumber & );
19
20 private:
21 char areaCode[ 4 ]; // 3-digit area code and null
22 char exchange[ 4 ]; // 3-digit exchange and null
23 char line[ 5 ]; // 4-digit line and null
24 };
25
26 // Overloaded stream-insertion operator (cannot be
27 // a member function if we would like to invoke it with
28 // cout << somePhoneNumber;).
29 ostream &operator<<( ostream &output, Faculty of IT, AFTC &num )
const PhoneNumber
30 {
31 output << "(" << num.areaCode << ") "
32 << num.exchange << "-" << num.line;
33 return output; // enables cout << a << b << c;
34 }
35
36
37
istream &operator>>( istream &input, PhoneNumber &num )
{
Example
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 The
(-) function call
43 input >> setw( 5 ) >> num.line; // input line
44 return input; // enables cin >> a >> b >> c;
cin >> phone;
45 } interpreted as
46
47 int main() operator>>(cin, phone);
48 {
input is an alias for cin, and num
49 PhoneNumber phone; // create object phone
50
is an alias for phone.
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; Faculty of IT, AFTC
61 }
Overloading Unary Operators
Overloading unary operators
Can be overloaded with no arguments or one
argument
Should usually be implemented as member
functions
Avoid friend functions and classes because they
violate the encapsulation of a class
Example declaration as a member function:
class String {
public:
bool operator!() const;
...
};
Example declaration as a non-member function
class String {
friend bool operator!( const String & )
Faculty of IT, AFTC
...
Overloading Binary Operators
Overloaded Binary operators
Non-static member function, one argument
Example:
class String {
public:
const String &operator+=(
const String & );
...
};
y += z is equivalent to y.operator+=( z )
Should it be const?
// but preferred
sum.setReal( getReal( ) + rhs.getReal ( ) );
return sum; 40
Using operator+
We can now write (cascading operators)
C4 = C3 + C2 + C1;
To overload op as in
A op B
we define a function of the form
operator op(B) (member function)
Or
operator op(A, B) (non-member)
where op can be +,-,/, %, =, !=, [ ], +=,
etc Faculty of IT, AFTC
Overloading Binary Operators
Overloading +=
y += z translate to operator+=(y,z)
where
class String {
public:
friend const String &operator+=(String &, const
String &);
…
};
Faculty of IT, AFTC
Operator Overloading
Chapter 8
Faculty of IT, AFTC