logo CodeStepByStep logo

polymorphismMystery5

Language/Type: C++ inheritance polymorphism

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

class Tuolumne {
public:
    virtual void m1() {
        cout << "T 1" << endl;
        m2();
    }

    virtual void m2() {
        cout << "T 2" << endl;
    }
};

class Glacier : public Muir {
public:
    virtual void m3() {
        cout << "G 3" << endl;
        m1();
    }

    virtual void m4() {
        cout << "G 4" << endl;
    }
};

class Muir : public Vernal {
public:
    virtual void m2() {
        cout << "M 2" << endl;
        Vernal::m2();
    }

    virtual void m4() {
        cout << "M 4" << endl;
    }
};

class Vernal : public Tuolumne {
public:
    virtual void m1() {
        Tuolumne::m1();
        cout << "V 1" << endl;
    }

    virtual void m3() {
        cout << "V 3" << endl;
    }
};

Now assume that the following variables are defined:

Vernal*   var1 = new Glacier();
Tuolumne* var2 = new Vernal();
Vernal*   var3 = new Muir();
Tuolumne* var4 = new Muir();
Tuolumne* var5 = new Tuolumne();

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

var2->m1();
var2->m2();
var2->m3();
var1->m1();
var1->m2();
var1->m3();
var1->m4();
var4->m1();
var4->m2();
var3->m2();
var3->m3();
var5->m4();
((Vernal*) var2)->m1();
((Vernal*) var2)->m3();
((Glacier*) var5)->m3();
((Tuolumne*) var3)->m3();
((Muir*) var1)->m4();
((Glacier*) var1)->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.