Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 19

CSE240 Lecture 19

Introduction to Programming Languages
Operator Overloading
(202010)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 19, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 19: Operator

    Overloading Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 2 jgs

    Announcements § Homework 4. (C++ Programming) § Quiz 3 (C++ Programming)
  3. jgs Previously…

  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 4 jgs

    Function Overloading • Multiple functions can have the same name, but different parameter list in type or in number. • Destructor cannot be overloaded, because it does not have parameter void function(int v) { // code } void function(double v) { // code }
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 5 jgs

    Operator Overloading in C++ Like function overloading, C++ allows user to define operator (built-in function) overloading. Why do we need operator overloading? • string1 = string2; instead of using strcpy(string1, string2); • string1 >= string2; instead of using strcmp(string1, string2); • rectangleArea(3, 5) < rectangleArea(2, 6) • time1(3, 23) + time2(5, 56), resulting in: time3(9, 19) • Increament a Date(year, month, day) object, what is the next date?
  6. jgs Example 1

  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 7 jgs

    Example 1 int main() { Cylinder cylinder1, cylinder2, cylinder3; double volume = 0.0; // cylinder1 and cylinder2 initialization cylinder1.setRadius(5.0); cylinder1.setHeight(5.0); cylinder2.setRadius(4.0); cylinder2.setHeight(10.0); // get and print volumes of cylinder1 and cylinder2 volume = cylinder1.getVolume(); cout << "Volume of cylinder1 : " << volume << endl; volume = cylinder2.getVolume(); cout << "Volume of cylinder2 : " << volume << endl; // Add two objects using overloaded operator +, and get and print volume cylinder3 = cylinder1 + cylinder2; volume = cylinder3.getVolume(); cout << "Volume of cylinder3 : " << volume << endl; // Subtract two object as follows: cylinder3 = cylinder1 - cylinder2; // get and print volume of cylinder 3 volume = cylinder3.getVolume(); cout << "Volume of cylinder3 : " << volume << endl; if (cylinder1 > cylinder2) // using overloaded operator > cout << "cylinder1 volume is greater than cylinder2 volume" << endl; else cout << "cylinder1 volume is not greater than cylinder2 volume" << endl; return 0; }
  8. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 8 jgs

    Example 1 #include <iostream> #include <cmath> using namespace std; class Cylinder { private: double radius, height; public: double getVolume(void) { // M_PI defined in <cmath> return M_PI * radius * radius * height; } void setRadius(double r) { radius = r; } void setHeight(int h) { height = h; } radius height
  9. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 9 jgs

    Example 1 // Overload + operator to add two Cylinder objects. Cylinder operator+(Cylinder &c) { Cylinder cylinder; cylinder.radius = this->radius + c.radius; cylinder.height = this->height + c.height; return cylinder; } // Overload - operator to subtract two Cylinder objects. Cylinder operator-(Cylinder &c) { Cylinder cylinder; cylinder.radius = this->radius - c.radius; cylinder.height = this->height - c.height; return cylinder; } // Overload - operator > (greater than of two Cylinder objects). bool operator>(Cylinder &c) { Cylinder cylinder; double vol0, vol1; vol0 = this->getVolume(); vol1 = c.getVolume(); if (vol0 > vol1) return true; else return false; } };
  10. jgs Example 1

  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 11 jgs

    Example 2 int main() { Days day1(Mon), day2, day3; day2.setDay(Sat); day3.setDay(Sun); cout << "The days before ++ operations" << endl; day1.display(); day2.display(); day3.display(); ++day1; ++day2; ++day3; cout << "The days after prefix ++ operations" << endl; day1.display(); day2.display(); day3.display(); day1++; day2++; day3++; cout << "The days after postfix ++ operations" << endl; day1.display(); day2.display(); day3.display(); return 0; }
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 12 jgs

    Example 2 #include <iostream> using namespace std; typedef enum { Sun = 0, Mon, Tue, Wed, Thu, Fri, Sat } DayType; class Days { private: DayType day; public: Days() { day = Sun; } // constructor without parameter Days(DayType d) { day = d; } // constructor with a parameter DayType getDay(void) { return day; } void setDay(DayType d) { if (d >= Sun && d <= Sat) this->day = d;} void display() { switch (day) { case Sun: cout << "Sun" << endl; break; case Mon: cout << "Mon" << endl; break; case Tue: cout << "Tue" << endl; break; case Wed: cout << "Wed" << endl; break; case Thu: cout << "Thu" << endl; break; case Fri: cout << "Fri" << endl; break; case Sat: cout << "Sat" << endl; break; default: cout << "Incorrect day" << endl; } }
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 13 jgs

    Example 2 // Overload prefix ++ operator to add one to Days object: ++days. Days operator++() { Days days(day); // Save the original value switch (this->day) { case Sun: this->day = Mon; break; case Mon: this->day = Tue; break; case Tue: this->day = Wed; break; case Wed: this->day = Thu; break; case Thu: this->day = Fri; break; case Fri: this->day = Sat; break; case Sat: this->day = Sun; break; default: cout << "Incorrect day" << endl; } days.day = this->day; return days; }
  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 14 jgs

    Example 2 // Overload postfix ++ operator to add one to Days object: days++. Days operator++(int) { // This parameter indicates ++ follows a parameter Days days(day); // Save the original value switch (this->day) { case Sun: this->day = Mon; break; case Mon: this->day = Tue; break; case Tue: this->day = Wed; break; case Wed: this->day = Thu; break; case Thu: this->day = Fri; break; case Fri: this->day = Sat; break; case Sat: this->day = Sun; break; default: cout << "Incorrect day" << endl; } // The value in the this object has been changed. // days.day = this->day; return days; // return the value before the changes. } };
  15. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 15 jgs

    Operator Overloading § can be overloaded § Cannot be overloaded + - * / % ^ & | ~ ! , = < > <= >= ++ -- << >> == != && || += -= /= %= ^= &= |= *= <<= >>= [] () -> ->* new new [] delete delete [] :: .* . ?:
  16. jgs Test Yourselves

  17. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 17 jgs

    Overloaded Functions vs. Virtual Functions • Multiple functions can have the same name, but different parameter list in type or in number. • Overloading can be applied to constructors and normal functions. • Overloading does not allow functions to have the same parameter list but different return type. • Virtual functions in the base class and derived class must have the same parameter list and return type.
  18. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 18 jgs

    Overloaded Functions vs. Virtual Functions • Multiple functions can have the same name, but different parameter list in type or in number. • Overloading can be applied to constructors and normal functions. • Overloading does not allow functions to have the same parameter list but different return type. • Virtual functions in the base class and derived class must have the same parameter list and return type. void enqueue(int v) { if (rear < queue_size) buffer[rear++] = v; else if (compact()) buffer[rear++] = v; } void enqueue(double v) { if (rear < queue_size) buffer[rear++] = v; else if (compact()) buffer[rear++] = v; } virtual void display() { // different implementations in different classes }
  19. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 19 jgs

    Quiz § Class Box (width, height, depth) § Merge Boxes (Box + Box) § Increase size (Box + 5) // add to all § Increase size ++Box or Box++ § Divide (Box / Box) // return an integer
  20. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.