$30 off During Our Annual Pro Sale. View Details »

CSE240 Lecture 18

CSE240 Lecture 18

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

Javier Gonzalez-Sanchez
PRO

January 18, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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

    View Slide

  2. jgs
    Previously …

    View Slide

  3. 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
    }

    View Slide

  4. 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)"<// code...
    buffer = NULL;
    }
    Queue::Queue(int n) { // constructor overload
    cout << "constructor (int)"<// code...
    buffer = new int[queue_size];
    }
    Queue::~Queue(void) {
    cout << "destructor"<// code...
    delete [] buffer;
    }
    int main () {
    Queue myQueue1(500);
    Queue myQueue2;
    Queue *myQueue3 = new Queue(100);
    // more code...
    delete myQueue3;
    return 0;
    }

    View Slide

  5. 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).

    View Slide

  6. 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;
    }

    View Slide

  7. 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;

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. jgs
    Inheritance

    View Slide

  11. 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

    View Slide

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

    View Slide

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

    View Slide

  14. 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;
    }

    View Slide

  15. jgs
    One more thing about Inheritance

    View Slide

  16. 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

    View Slide

  17. 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;
    }

    View Slide

  18. 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;
    }

    View Slide

  19. 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;
    }

    View Slide

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

    View Slide

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

    View Slide

  22. 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.

    View Slide