Title: C Operator Overloading
1C Operator Overloading
2Function overloading
Function overloading enables writing more
then one function with the same name but
different signature.
int my_func(int a, int b) return(a b)
int my_func(int a, int b, int c) return( a b
c) my_func(2,4) my_func(2,4,6)
3Function overloading
int Add(int nX, int nY) return nX nY
double Add(double dX, double dY) return dX
dY int Add(int nX, int nY, int nZ)
return nX nY nZ ERROR (signature must
be unique in some way) int GetRandomValue()
double GetRandomValue()
4Operator overloading
- Some languages allow only function (method)
overloading - such as Java. - However, on a primitive level all languages use
operator overloading to some degree. - Consider
- 4 6
- 3.4 5.6
- Cows can jump
5Operator overloading
- Operator overloading doesnt add power to code -
it essentially enhances the coding experience
(efficiency for the programmer) - Adds logical depth to ADTs
- If there is a new type called BirthdayList,
shouldnt it possible to add, substract, compare
lists and display using intuitive operators? - listA listB
- listA Jim
- listA - Marvin
- cout ltlt listA
6Operator overloading
- The only operators that cannot be overloaded are
(scope resolution), . (member selection), and
. (member selection through pointer to
function).
7Operator overloading
- FRIEND (free yet is associated with class
inline or external function) - friend const Point operator (const Point, const
int) - friend ostream operatorltlt( ostream, const
Point ) - CLASS
- const Point operator (const Point)
- FREE
- const Point operator- (const Point, const
Point)
Within Class Definition
8Operator overloading
- FRIEND (free that is allowed access to private
variables of class Friend not used here) - const Point operator (const Point lhs, const
int v) -
- return Point(lhs._x v, lhs._y v)
-
- ostream operatorltlt( ostream o, const Point p )
-
- o ltlt "(" ltlt p.x() ltlt "," ltlt p.y() ltlt ")"
- return o
-
-
Within Source File (not scoped for class)
9Operator overloading
- CLASS (use when necessary or more convenient)
- const Point Pointoperator (const Point rhs)
-
- this-gt_x rhs.x()
- this-gt_y rhs.y()
- return this
-
-
Within Source File (scoped for class)
10Operator overloading
- FREE (efficiency issues must use accessor and
mutator functions) - const Point operator- (const Point lhs, const
Point rhs) -
- return Point(lhs.x() - rhs.x(), lhs.y() -
rhs.y()) -
-
Within Source File (not scoped for class)
11Const
- The five types of const prevent the following
from modification - const variable the variable (local and global
variables) - const argument the argument
- const return type this only only applied to
references to members of a class. Then, const can
prevent the original member from being modified. - const method all non-mutable class members
- const member the member (class member - once
object is constructed - this value cant change)
12Const
- For parameters to member function the parameter
value can not be changed. - Particularly useful when using the reference
qualifier - void addCow(const Cow heifer)
- list_ list_heifer
-
- Otherwise - the function would have the ability
to change the value of the argument being passed
in.
13Const
- For class usage of a member function - the
function can not change the values of any class
variables - void displayBrands() const
-
14Const
- The value of a return type that is declared const
cannot be changed. This is especially useful when
giving a reference to a class's internals. - struct Values
-
- const stdvectorltintgt GetValues() const
return mV - private
- stdvectorltintgt mV
-
- C Structure and C class are exactly same
except default access specifier of their members
- in C Structure all members are public by
default while in Class all are private. - NOTE Use const whenever possible.