Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Operator Overloading Gordon College CPS212 Gordon College CPS212.

Similar presentations


Presentation on theme: "C++ Operator Overloading Gordon College CPS212 Gordon College CPS212."— Presentation transcript:

1 C++ Operator Overloading Gordon College CPS212 Gordon College CPS212

2 Function 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);

3 Function 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();

4 Operator 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 ”  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 ”

5 Operator 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 == listB listA + Jim listA - Marvin cout << 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 == listB listA + Jim listA - Marvin cout << listA

6 Operator overloading  The only operators that cannot be overloaded are :: (scope resolution),. (member selection), and.* (member selection through pointer to function).

7 Operator 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 &);  CLASS const Point& operator= (const Point&);  FREE const 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 &);  CLASS const Point& operator= (const Point&);  FREE const Point operator- (const Point&, const Point&); Within Class Definition

8 Operator 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)

9 Operator 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)

10 Operator 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)

11 Const  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)

12 Const  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.

13 Const  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;

14 Const  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 & GetValues() const { return mV; } private: std::vector 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 & GetValues() const { return mV; } private: std::vector 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.


Download ppt "C++ Operator Overloading Gordon College CPS212 Gordon College CPS212."

Similar presentations


Ads by Google