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

CSE240 Lecture 18

CSE240 Lecture 18

Introduction to Programming Languages
Inheritance in C++
(202010)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 18, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 18: Inheritance

    in C++ Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs

    Inheritance § Can be used for defining new classes by extending existing classes § Java: parent (super) class and child (sub) class § C++: base class and derived class § New class inherits existing members (variables and functions) and may add members or redefine members
  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs

    Example #include <iostream> using namespace std; class Base { public: Base(int n) { cout << "Base constructor"<<endl; } void function() { cout << "fuction"<<endl; } ~Base() { cout << "Base destructor"<<endl; } }; class Derived : public Base { public: // constructor calls constructor base Derived(int n) : Base(n) { cout << "Derived constructor"<<endl; } ~Derived() { cout << "Derived destructor"<<endl; } };
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs

    Example #include <iostream> using namespace std; class Base { public: Base(int n) { cout << "Base constructor"<<endl; } void function() { cout << "fuction"<<endl; } ~Base() { cout << "Base destructor"<<endl; } }; class Derived : public Base { public: // constructor calls constructor base Derived(int n) : Base(n) { cout << "Derived constructor"<<endl; } ~Derived() { cout << "Derived destructor"<<endl; } };
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs

    Example int main() { Derived myPQ1(50); myPQ1.function(); // inherited myPQ1.function(); // inherited Derived *myPQ2; myPQ2 = new Derived(50); myPQ2->function(); // inherited myPQ2->function(); // inherited delete myPQ2; }
  6. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 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
  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 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; }
  8. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 8 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; }
  9. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 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; }
  10. jgs One last thing: Operator Overloading

  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 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?
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 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; }
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 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
  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 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; } };
  15. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 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; }
  16. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 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; } }
  17. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 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; }
  18. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 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. } };
  19. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 jgs

    Operator Overloading § can be overloaded § Cannot be overloaded + - * / % ^ & | ~ ! , = < > <= >= ++ -- << >> == != && || += -= /= %= ^= &= |= *= <<= >>= [] () -> ->* new new [] delete delete [] :: .* . ?:
  20. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    Spring 2018 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.