Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 18: Inheritance in C++ Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Previously …

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs new and delete int main () { Queue myQueue2(500); // declare a pointer only Queue *myQueue; // reserve memory for an object myQueue = new Queue(500); // use the object myQueue->enqueue(23); myQueue2.enqueue(8); // delete will call ~Queue(); delete myQueue; ... delete myQueue2;//no needed }

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs new and delete with constructors and destructors #include using namespace std; class Queue { private: int queue_size; protected: int *buffer; int front; int rear; public: Queue(); Queue(int n); ~Queue(); }; Queue::Queue() { // constructor cout << "constructor(void)"<

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs Summary • If an object is on the stack, instead of on the heap, destructor will be called when the object goes out of scope. No delete operation is necessary. • All heap objects must be explicitly deleted before leaving the function, if they are no longer needed. • The function delete will implicitly call the destructor of the class, so that an object linked to a variable in the to-be-deleted object can be de- allocated too, i.e., using delete for variables created in the class (normally in the constructor).

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs delete and Array of Objects #include using namespace std; #define size 4 class arrayObject { public: int x; double y; arrayObject() { cout << "arrayObject's constructor called" << endl; } ~arrayObject() { cout << "arrayObject's destructor called" << endl; } }; int main() { arrayObject *p, *q; // declare two pointers p = new arrayObject[size]; // Create an array of objects for (q = p; q < p + size; q++) { // Initialize the objects q->x = 10; q->y = 1.5; } for (q = p; q < p + size; q++) { cout << "Element address " << q << " Element x value: " << q->x << endl; cout << "Element address " << q << " Element y value: " << q->y << endl; } delete[] p; return 0; }

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs Summary § How do we delete an array of objects? We can use a loop to delete each element, § However, the language provides a library function to delete all the elements one by one without the user to explicitly use a loop: delete[] array;

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 8 jgs Summary Java § Primitive variables (int, float, boolean) use value type. § All other variables (string, array, user defined classes) use reference type. (Java uses automatic garbage collection). C++ § Both value and reference types exist: § if value semantics is used then memory will be allocated on stack. Memory de- allocation is done automatically. § if reference semantics is used (e.g. variable is a pointer to an object), then memory must be allocated explicitly using new and explicitly de-allocated using delete

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 9 jgs Summary // in C++ Report r; // an object is allocated to r Report *rp1, *rp2; // two pointers declared rp1 = &r; // rp1 points to object r rp2 = new Report(); // an object is created, linked to rp2 // .. delete rp2; // in Java Report r; // an reference is allocated r = new Report (); // an object is created and linked to r

Slide 10

Slide 10 text

jgs Inheritance

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 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

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs Example #include using namespace std; class Base { public: Base(int n) { cout << "Base constructor"<

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs Example #include using namespace std; class Base { public: Base(int n) { cout << "Base constructor"<

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 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; }

Slide 15

Slide 15 text

jgs One more thing about Inheritance

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 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

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 jgs Example #include 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; }

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 jgs Polymorphism A pointer to a derived class is type-compatible with a pointer to its base class. // pointers to base class #include 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; }

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 jgs Polymorphism A virtual member is a member function for which dynamic dispatch is facilitated. // pointers to base class #include 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; }

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 20 jgs Questions

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 21 jgs Questions

Slide 22

Slide 22 text

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.