logo CodeStepByStep logo

polymorphismMystery6

Language/Type: C++ inheritance polymorphism

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

class Amazon : public Facebook {
public:
    virtual void b() {
        cout << "AM B" << endl;
        a();
    }

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

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

class Microsoft : public Google {
public:
    virtual void b() {
        cout << "MS B" << endl;
        a();
    }

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

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

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

class Facebook : public Google {
public:
    virtual void a() {
        cout << "FB A" << endl;
    }

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

Now assume that the following variables are defined:

Google*   var1 = new Facebook();
Facebook* var2 = new Amazon();
Google*   var3 = new Amazon();
Google*   var4 = new Microsoft();

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();
((Facebook*) var1)->a();
((Facebook*) var1)->d();
((Amazon*) var2)->d();
((Microsoft*) var3)->b();
((Facebook*) var4)->a();
((Facebook*) var4)->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.