View
214
Download
0
Embed Size (px)
Citation preview
C++ Operator OverloadingC++ Operator Overloading
Gordon College
CPS212
Gordon College
CPS212
Function overloadingFunction overloading
Function overloading enables writing more thenone 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);
Function overloadingFunction 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();
Operator overloadingOperator 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 + 63.4 + 5.6 “Cows “ + “can jump”
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 + 63.4 + 5.6 “Cows “ + “can jump”
Operator overloadingOperator overloading Operator overloading doesn’t 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”, shouldn’t it possible to add, substract, compare lists and display using intuitive operators?listA == listBlistA + JimlistA - Marvincout << listA
Operator overloading doesn’t 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”, shouldn’t it possible to add, substract, compare lists and display using intuitive operators?listA == listBlistA + JimlistA - Marvincout << listA
Operator overloadingOperator overloading
The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function).
The only operators that cannot be overloaded are :: (scope resolution), . (member selection), and .* (member selection through pointer to function).
Operator overloadingOperator overloading FRIEND (free – yet is associated with class –
inline or external function)friend const Point operator+ (const Point&, const int&);
friend ostream & operator<<( ostream&, const Point &);
CLASSconst Point& operator= (const Point&);
FREEconst Point operator- (const Point&, const Point&);
FRIEND (free – yet is associated with class – inline or external function)
friend const Point operator+ (const Point&, const int&);
friend ostream & operator<<( ostream&, const Point &);
CLASSconst Point& operator= (const Point&);
FREEconst Point operator- (const Point&, const Point&);
Within Class Definition
Operator overloadingOperator 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& operator<<( ostream& o, const Point &p ) { o << "(" << p.x() << "," << p.y() << ")"; return o; }
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& operator<<( ostream& o, const Point &p ) { o << "(" << p.x() << "," << p.y() << ")"; return o; }
Within Source File (not scoped for class)
Operator overloadingOperator overloading
CLASS (use when necessary or more convenient)const Point& Point::operator= (const Point& rhs) { this->_x = rhs.x(); this->_y = rhs.y(); return *this; }
CLASS (use when necessary or more convenient)const Point& Point::operator= (const Point& rhs) { this->_x = rhs.x(); this->_y = rhs.y(); return *this; }
Within Source File (scoped for class)
Operator overloadingOperator 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()); }
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)
ConstConst
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 can’t change)
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 can’t change)
ConstConst 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.
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.
ConstConst For class usage of a member function - the function can
not change the values of any class variables
void displayBrands() const;
For class usage of a member function - the function can not change the values of any class variables
void displayBrands() const;
ConstConst 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 std::vector<int>& GetValues() const { return mV; } private: std::vector<int> 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.
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 std::vector<int>& GetValues() const { return mV; } private: std::vector<int> 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.