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)
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
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; }
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 = ▵ 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; }
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 = ▵ 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; }
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 }
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?
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; }
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; }
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. } };
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.
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 }
[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.