Question: Polymorphism C++ Your friend made a program for a robot, which makes different drinks, including tea and coffee. The code defines a Drink class, and

Polymorphism

C++

Your friend made a program for a robot, which makes different drinks, including tea and coffee. The code defines a Drink class, and separate Tea and Coffee classes. It tries to create a Tea and Coffee object, set their prices with the corresponding method and call their make() method. However, the code does not work, as it has multiple errors. Find the errors in the code and fix them, so that the program runs as expected.

#include

using namespace std;

class Drink {

protected:

int price;

public:

void setPrice(int a){

price = a;

}

};

class Coffee {

public:

void make() {

cout << "Coffee: "<

}

};

class Tea {

public:

void make() {

cout << "Tea: "<

}

};

int main() {

Coffee c;

Tea t;

Drink *e1 = &c;

Drink *e2 = &t;

c->setPrice(5);

t->setPrice(6);

c->make();

t.make();

}

Remember, to call a method with a pointer, you need to use the -> operator, whereas on an object you need to use the dot operator.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!