1 / 14

C++ Operator Overloading

C++ Operator Overloading. Gordon College CPS212. 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); }

carissa
Download Presentation

C++ Operator Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Operator Overloading 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”

  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

  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) friendconstPointoperator+ (constPoint&, constint&); friendostream & operator<<( ostream&, constPoint &); • CLASS constPoint& operator= (constPoint&); • FREE constPointoperator- (constPoint&, constPoint&); Within Class Definition

  8. Operator overloading • FRIEND (free that is allowed access to private variables of class – Friend not used here) constPointoperator+ (constPoint& lhs, constint& v) { return Point(lhs._x + v, lhs._y + v); } ostream& operator<<( ostream& o, constPoint &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) constPoint& Point::operator= (constPoint& 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) constPointoperator- (constPoint& lhs, constPoint& 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)

  12. Const • For parameters to member function: the parameter value can not be changed. • Particularly useful when using the reference qualifier: voidaddCow(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 voiddisplayBrands() 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<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.

More Related