Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator overloading.

Similar presentations


Presentation on theme: "Operator overloading."— Presentation transcript:

1 Operator overloading

2 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

3 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

4 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

5 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... };

6 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!=’

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

8 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

9 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... };

10 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};

11 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};

12 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};

13 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... };

14 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;

15 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;

16 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

17 References [1] No references?

18 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 for more information.

19 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.


Download ppt "Operator overloading."

Similar presentations


Ads by Google