+ 1

C++ Another Cup of Coffee

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. 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. #include <iostream> using namespace std; class Drink { protected: int price; public: void setPrice(int a){ price = a; } }; class Coffee: public Drink { public: void make() { cout << "Coffee: "<<price<<endl; } }; class Tea: public Drink { public: void make() { cout << "Tea: "<<price<<endl; } }; int main() { Coffee c; Tea t; Drink *e1 = &c; Drink *e2 = &t; c->setPrice(5); t->setPrice(6); c.make(); t.make(); } Not sure what I'm doing wrong if someone would advise!

20th Oct 2021, 6:41 PM
Alex Ramsey
4 odpowiedzi
+ 1
e1->setPrice(5); e2->setPrice(6);
20th Oct 2021, 7:27 PM
Coding Cat
Coding Cat - avatar
+ 1
C++ Another Cup of Coffee 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. 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. #include <iostream> using namespace std; class Drink { protected: int price; public: void setPrice(int a){ price = a; } }; class Coffee: public Drink { public: void make() { cout << "Coffee: "<<price<<endl; } }; class Tea: public Drink { public: void make() { cout << "Tea: "<<price<<endl; } }; int main() { Coffee c; Tea t; Drink *e1 = &c; Drink *e2 = &t; c.setPrice(5); t.setPrice(6); c.make(); t.make(); }
17th Oct 2022, 2:18 PM
Suhas S
Suhas S - avatar
+ 1
THIS IS THE PERFECT ANSWER
17th Oct 2022, 2:19 PM
Suhas S
Suhas S - avatar
+ 1
#include <iostream> using namespace std; class Drink { protected: int price; public: void setPrice(int a){ price = a; } }; class Coffee: public Drink { public: void make() { cout << "Coffee: "<<price<<endl; } }; class Tea: public Drink { public: void make() { cout << "Tea: "<<price<<endl; } }; int main() { Coffee c; Tea t; Drink *e1 = &c; Drink *e2 = &t; e1->setPrice(5); e2->setPrice(6); c.make(); t.make(); } //This is the correct answer
26th Apr 2023, 9:49 AM
palash sanghvi
palash sanghvi - avatar