logo CodeStepByStep logo

polymorphismMystery10

Language/Type: C++ inheritance polymorphism

Consider the following classes; assume that each is defined in its own file.

class Jesse : public Tulip {
public:
    virtual void m1() {
        cout << "Js1  ";
        Cassidy::m1();
    }

    virtual void m4() {
        cout << "Js4  ";
        m2();
    }
};

class Cassidy {
public:
    virtual void m1() {
        cout << "Ca1  ";
        m2();
    }

    virtual void m2() {
        cout << "Ca2  ";
    }
};

class Tulip : public Cassidy {
public:
    virtual void m2() {
        cout << "Tu2  ";
        Cassidy::m2();
    }

    virtual void m3() {
        cout << "Tu3  ";
        m1();
    }
};

Now assume that the following variables are defined:

Cassidy*  var1 = new Tulip();
Tulip*    var2 = new Jesse();

In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement produces more than one line of output, indicate the line breaks with slashes as in "x / y / z" to indicate three lines of output with "x" followed by "y" followed by "z". If the statement does not compile, write "COMPILER ERROR". If a statement would crash at runtime or cause unpredictable behavior, write "CRASH".

var1->m1();
var1->m3();
var2->m2();
var2->m3();
var2->m4();
((Tulip*) var1)->m3();
((Tulip*) var1)->m4();
((Jesse*) var1)->m4();
((Cassidy*) var2)->m3();
((Jesse*) var2)->m4();

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.