fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class BakedGood {
  5. private:
  6. double price;
  7. public:
  8. BakedGood(double p) : price(p) { }
  9. virtual void print() const { std::cout << "($" << price << ')'; }
  10. };
  11.  
  12. class Bread : public BakedGood {
  13. public:
  14. Bread(double p) : BakedGood(p) { }
  15. void print() const {
  16. std::cout << "Wheat bread ";
  17. BakedGood::print();
  18. }
  19. };
  20.  
  21. class CupCake: public BakedGood {
  22. public:
  23. CupCake(double p) : BakedGood(p) { }
  24. void print() const {
  25. std::cout << "Chocolate cupcake ";
  26. BakedGood::print();
  27. }
  28. };
  29.  
  30. int main() {
  31. Bread b(4.5);
  32. CupCake c(1.95);
  33.  
  34. c.print(); std::cout << std::endl;
  35. b.print(); std::cout << std::endl;
  36. // your code goes here
  37. return 0;
  38. }
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
Chocolate cupcake ($1.95)
Wheat bread ($4.5)