logo CodeStepByStep logo

polymorphismMystery1

Language/Type: C++ inheritance polymorphism

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

class Eddie : public Kurt {
public:
    virtual void b() {
        a();
        cout << "E B" << endl;
    }

    virtual void c() {
        cout << "E C" << endl;
    }
};

class Kurt {
public:
    virtual void a() {
        cout << "K A" << endl;
        c();
    }

    virtual void c() {
        cout << "K C" << endl;
    }
};

class Chris : public Jerry {
public:
    virtual void b() {
        a();
        cout << "C B" << endl;
    }

    virtual void c() {
        cout << "C C" << endl;
        Jerry::c();
    }

    virtual void d() {
        cout << "C D" << endl;
        c();
    }
};

class Jerry : public Kurt {
public:
    virtual void a() {
        cout << "J A" << endl;
    }

    virtual void c() {
        cout << "J C" << endl;
        Kurt::c();
    }
};

Now assume that the following variables are defined:

Kurt*  var1 = new Jerry();
Jerry* var2 = new Chris();
Kurt*  var3 = new Eddie();
Kurt*  var4 = new Chris();
Kurt*  var5 = new Kurt();

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->a();
var1->b();
var1->c();
var2->a();
var2->b();
var2->c();
var3->a();
var3->b();
var4->a();
var5->a();
((Jerry*) var1)->a();
((Jerry*) var1)->b();
((Chris*) var2)->d();
((Eddie*) var3)->b();
((Jerry*) var4)->a();
((Jerry*) var4)->b();
((Jerry*) var5)->b();

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.