Operator overloading.

Slides:



Advertisements
Similar presentations
Throwing and catching exceptions
Advertisements

Recursive binary search
For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Reference variables, pass-by-reference and return-by-reference
Switch statement.
Binary search.
Command-line arguments
Throwing exceptions.
Console input.
Dangling pointers.
This.
Sorted arrays.
Dynamically allocating arrays within structures
Break statements.
Linked Lists.
Wild pointers.
Logical operators.
The comma as a separator and as an operator
Bucket sort.
The call stack and recursion and parameters revisited
The ternary conditional operator
Throwing exceptions.
Dynamically allocating structures
Bit-wise and bit-shift operators
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Templated Linked Lists
Polymorphism.
Repetitious operations
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Code-development strategies
Throwing exceptions.
Anatomy of a program.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Class variables and class functions
The std::string class.
Templates.
This.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
Arithmetic operators.
Data structures: class
An array class: constructor and destructor
Constructors.
This.
Recursive binary search
Presentation transcript:

Operator overloading

Outline In this lesson, we will: Describe issues with the primitive data types Introduce the 3-body problem and an attempt to solve it Introduce the struct keyword and member variables Create a 3-dimensional vector data structure Describe assigning to and using the member variables Describe passing instances of data structures as arguments We will create a library of functions for vectors Initialize instances of data structures Revisit our 3-body problem Determine if we have a solution to arrays

Limitations of primitive data types Given two vectors, wouldn’t it be nice if we could perform operations on them as if we were doing math? int main() { Vector_3d u{7.5, 2.1, 4.6}; Vector_3d v{1.3, 8.4, -3.9}; if ( u == v ) { // do something if they are equal } u += v; // Add the vector v onto the vector u std::cout

3-dimensional vectors Revisiting our Vector_3d class, class Vector_3d; class Vector_3d { public: // Member variables double x; double y; double z; }; two vectors are equal if all their components are equal

3-dimensional vectors We need to define a function that is called if the user calls if ( u == v ) { // do something } This is done through special member functions: class Vector_3d { public: // Member functions... // Overloaded operators bool operator==( Vector_3d const &v ) const; bool operator!=( Vector_3d const &v ) const; // Member variables... };

3-dimensional vectors The following two are equivalent in C++: if ( u == v ) { if ( u != v ) { if ( u.operator==( v ) ) { if ( u.operator!=( v ) ) { The first description is converted by the compiler into the second If you do not define these functions, the compiler will flag an error: example.cpp: In function ‘int main()’: example.cpp:27:10: error: no match for ‘operator==’ (operand types are ‘Vector_3d’ and ‘Vector_3d’) if ( (u == v) || (u != v) ) { ~~^~~~ example.cpp:27:22: error: no match for ‘operator!=’

3-dimensional vectors Here are the member function definitions: // Return true if all the member variables are the same bool Vector_3d::operator==( Vector_3d const &v ) const { return (x == v.x) && (y == v.y) && (z == v.z); } // Return true if any of the member variables differ bool Vector_3d::operator!=( Vector_3d const &v ) const { return (x != v.x) || (y != v.y) || (z != v.z);

3-dimensional vectors Other operators you may wish to implement: Adding two vectors Subtracting one vector from another Finding the additive inverse of a vector Multiplying a vector by a scalar Dividing a vector by a scalar Calculating the cross product of two vectors

3-dimensional vectors All of these can be done with member functions class Vector_3d { public: // Member functions... // Overloaded operators bool operator==( Vector_3d const &v ) const; bool operator!=( Vector_3d const &v ) const; Vector_3d operator+( Vector_3d const &v ) const; Vector_3d operator-( Vector_3d const &v ) const; Vector_3d operator+() const; Vector_3d operator-() const; Vector_3d operator*( double s ) const; Vector_3d operator/( double s ) const; Vector_3d operator*( Vector_3d const &v ) const; // Member variables... };

3-dimensional vectors Given vectors u and v, these overloaded operators add and subtract two vectors calculating u + v and u – v // Return a new vector of u + v Vector_3d Vector_3d::operator+( Vector_3d const &v ) const { return Vector_3d{x + v.x, y + v.y, z + v.z}; } // Return a new vector of u - v return Vector_3d{x - v.x, y - v.y, z - v.z};

3-dimensional vectors These overloaded operators return +u and –u // Return a new vector of +u Vector_3d Vector_3d::operator+() const { return Vector_3d{x, y, z}; } // Return a new vector of -u Vector_3d Vector_3d::operator-( Vector_3d const &v ) const { return Vector_3d{-x, -y, -z};

3-dimensional vectors Given a vector u and a scalar a, these overloaded operators calculating ua and u/a // Return a new vector of u*s Vector_3d Vector_3d::operator*( double s ) const { return Vector_3d{x*s, y*s, z*s}; } // Return a new vector of u - v Vector_3d Vector_3d::operator+( double s ) const { return Vector_3d{x/s, y/s, z/s};

3-dimensional vectors Recall that you can also assign: This is for free, as all member variables are just copied over What about other auto-assigned operators? class Vector_3d { public: // Member functions... // Overloaded operators // let u = u + v Vector_3d &operator+=( Vector_3d const &v ); Vector_3d &operator-=( Vector_3d const &v ); Vector_3d &operator*=( double s ); Vector_3d &operator/=( double s ); // Member variables... };

3-dimensional vectors These overloaded operators return u = u + v and –u // Update u <- u + v Vector_3d& Vector_3d::operator+=( Vector_3d const *v ) { x += v.x; y += v.y; z += v.z; return *this; } Vector_3d& Vector_3d::operator-=( Vector_3d const *v ) { x -= v.x; y -= v.y; z -= v.z;

3-dimensional vectors These overloaded operators return u = u + v and –u // Update u <- u + v Vector_3d& Vector_3d::operator*=( double s ) { x *= s; y *= s; z *= s; return *this; } Vector_3d& Vector_3d::operator/=( double s ) { x /= s; y /= s; z /= s;

Summary Following this lesson, you now Understand the struct keyword Know how to declare, access and manipulate the member variables Know how to pass instances of data structures to functions Understand how to initialize instances Understand how data structures can contain data structures Know that this simple solution cannot solve our issues with arrays

References [1] No references?

Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https://www.rbg.ca/ for more information.

Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.