logo CodeStepByStep logo

polymorphismMystery11

Language/Type: C++ inheritance polymorphism

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

class Becca : public Arie {
public:
    virtual void m2() {
        cout << "B2 ";
    }

    virtual void m3() {
        cout << "B3 ";
        Arie::m2();
    }
};

class Arie {
public:
    virtual void m1() {
        cout << "A1 ";
        m2();
    }

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

class JuanPablo :
           public Lauren {
public:
    virtual void m2() {
        cout << "JP2 ";
        Becca::m2();
    }
};

class Lauren : public Becca {
public:
    virtual void m1() {
        cout << "L1 ";
        Arie::m1();
    }

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

Now assume that the following variables are defined:

Arie*  var1 = new Becca();
Becca* var2 = new Lauren();
Arie*  var3 = new JuanPablo();

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();
var3->m2();
var3->m4();
((Becca*) var1)->m3();
((Lauren*) var1)->m4();
((Becca*) var1)->m4();
((Arie*) var2)->m3();
((Lauren*) var2)->m4();
((Lauren*) var3)->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.