fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Vehicle {
  5. public:
  6. void SetSpeed(int speedToSet) {
  7. speed = speedToSet;
  8. }
  9.  
  10. void PrintSpeed() {
  11. cout << speed;
  12. }
  13.  
  14. private:
  15. int speed;
  16. };
  17.  
  18. class Car : public Vehicle {
  19. public:
  20. void PrintCarSpeed() {
  21. cout << "Speed: ";
  22. PrintSpeed();
  23. }
  24. };
  25.  
  26. class AnimalPowered : public Vehicle {
  27. public:
  28. void SetAnimal(string animalToSet) {
  29. animal = animalToSet;
  30. }
  31.  
  32. void PrintAnimalSpeed() {
  33. cout << animal << " speed: ";
  34. PrintSpeed();
  35. }
  36.  
  37. private:
  38. string animal;
  39. };
  40.  
  41. int main() {
  42. Car myCar;
  43. AnimalPowered wagon;
  44.  
  45. myCar.SetSpeed(50);
  46. wagon.SetSpeed(3);
  47. wagon.SetAnimal("Camel");
  48.  
  49. myCar.PrintCarSpeed();
  50. cout << endl;
  51. wagon.PrintAnimalSpeed();
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Speed: 50
Camel speed: 3