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

CSE240 Lecture 19

CSE240 Lecture 19

Introduction to Programming Languages
Operator Overloading
(202011)

Javier Gonzalez-Sanchez

January 19, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    Overloading Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 2 jgs

    Announcements § Homework 4. (C++ Programming) § Quiz 4 (C++ Programming) – 45 minutes. Some open questions. A lot of constructors/destructors and pointers (use of new/delete)
  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 4 jgs

    Multiple Inheritance § A class can inherit members from more than one class; § The semantics of multiple inheritances is complex and error prone. It must be used with caution. § The diamond problem name and age needed only once
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 5 jgs

    Example #include <iostream> using namespace std; class A { public: A() { cout << "A's constructor called" << endl; } }; class B { public: B() { cout << "B's constructor called" << endl; } }; class C: public B, public A { // Note the order public: C() { cout << "C's constructor called" << endl; } }; int main() { C c; return 0; }
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 6 jgs

    Polymorphism A pointer to a derived class is type-compatible with a pointer to its base class. // pointers to base class #include <iostream> using namespace std; class Figure { protected: int width, height; public: void set_values (int a, int b) {width=a; height=b;} int area () { return 0; } }; class Rectangle: public Figure { public: int area() { return width*height;} }; class Triangle: public Figure { public: int area() { return width*height/2; } }; int main () { Rectangle rectangle; Triangle triangle; Figure * f1 = &rectangle; Figure * f2 = &triangle; f1->set_values (10,20); f2->set_values (30,40); cout << rectangle.area() << "\n"; cout << triangle.area() << "\n"; cout << f1->area() << '\n'; cout << f2->area() << '\n'; return 0; }
  6. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 7 jgs

    Polymorphism A virtual member is a member function for which dynamic dispatch is facilitated. // pointers to base class #include <iostream> using namespace std; class Figure { protected: int width, height; public: void set_values (int a, int b) {width=a; height=b;} virtual int area () { return 0; } }; class Rectangle: public Figure { public: int area() { return width*height;} }; class Triangle: public Figure { public: int area() { return width*height/2; } }; int main () { Rectangle rectangle; Triangle triangle; Figure * f1 = &rectangle; Figure * f2 = &triangle; f1->set_values (10,20); f2->set_values (30,40); cout << rectangle.area() << "\n"; cout << triangle.area() << "\n"; cout << f1->area() << '\n'; cout << f2->area() << '\n'; return 0; }
  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 9 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 }
  8. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 10 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?
  9. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 11 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; }
  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 12 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
  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 13 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; } };
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 14 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; }
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 15 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; } }
  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 16 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; }
  15. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 17 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. } };
  16. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 18 jgs

    Operator Overloading § can be overloaded § Cannot be overloaded + - * / % ^ & | ~ ! , = < > <= >= ++ -- << >> == != && || += -= /= %= ^= &= |= *= <<= >>= [] () -> ->* new new [] delete delete [] :: .* . ?:
  17. Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 20 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 | 21 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 | 22 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, Ph.D.

    [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.