More C++ Concepts: - Operator Overloading - Friend Function - This Operator - Inline Function
More C++ Concepts: - Operator Overloading - Friend Function - This Operator - Inline Function
• Operator overloading
• Friend Function
• This Operator
• Inline Function
1
Operator overloading
2
Why Operator Overloading
int i, j, k; // integers
float m, n, p; // floats The compiler overloads
the + operator for built-in
integer and float types by
k = i + j; default, producing integer
// integer addition and assignment addition with i+j, and
p = m + n; floating addition with m+n.
// floating addition and assignment
operator@(argument-list) operator-
operator*
operator/
--- operator is a function
--- @ is one of C++ operator symbols (+, -, =, etc..)
4
Example of Operator Overloading
class CStr
{
void CStr::cat(char *s)
char *pData;
{
int nLength; int n;
public: char *pTemp;
// … n=strlen(s);
void cat(char *s); if (n==0) return;
// …
CStr operator+(CStr str1, CStr str2); pTemp=new char[n+nLength+1];
CStr operator+(CStr str, char *s); if (pData)
CStr operator+(char *s, CStr str); strcpy(pTemp,pData);
//accessors strcat(pTemp,s);
char* get_Data(); pData=pTemp;
int get_Len(); nLength+=n;
}
};
5
The Addition (+) Operator
CStr CStr::operator+(CStr str1, CStr str2)
{
CStr new_string(str1);
//call the copy constructor to initialize an
//entirely new CStr object with the first
//operand
new_string.cat(str2.get_Data());
//concatenate the second operand onto the
//end of new_string
return new_string;
//call copy constructor to create a copy of
//the return value new_string
}
new_string
str1
strcat(str1,str2)
strlen(str1)
strlen(str1)+strlen(str2) 6
How does it work?
CStr first(“John”);
CStr last(“Johnson”);
CStr name(first+last);
7
Implementing Operator Overloading
• Two ways:
– Implemented as member functions
– Implemented as non-member or Friend functions
• the operator function may need to be declared as a
friend if it requires access to protected or private data
8
Implementing Operator Overloading
12
What is ‘Friend’?
• Friend declarations introduce extra coupling
between classes
– Once an object is declared as a friend, it has
access to all non-public members as if they
were public
• Access is unidirectional
– If B is designated as friend of A, B can access
A’s non-public members; A cannot access B’s
• A friend function of a class is defined
outside of that class's scope
13
More about ‘Friend’
• The major use of friends is
– to provide more efficient access to data members
than the function call
– to accommodate operator functions with easy
access to private data members
• Friends can have access to everything, which
defeats data hiding, so use them carefully
• Friends have permission to change the
internal state from outside the class. Always
recommend use member functions instead of
friends to change state
14
Assignment Operator
• Assignment between objects of the same type is always
supported
– the compiler supplies a hidden assignment function if you don’t
write your own one
– same problem as with the copy constructor - the member by
member copying
– Syntax:
CStr object
(*this) 17
Overloading stream-insertion and
stream-extraction operators
• In fact, cout<< or cin>> are operator overloading built in C++
standard lib of iostream.h, using operator "<<" and ">>"
• cout and cin are the objects of ostream and istream classes,
respectively
• We can add a friend function which overloads the operator <<
friend ostream& operator<< (ostream &os, const Date &d);
22
Take Home Message
• Operator overloading provides convenient
notations for object behaviors