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 << "Driving at: ";
  22. PrintSpeed();
  23. }
  24. };
  25.  
  26. class ElectricCar : public Car {
  27. public:
  28. void SetBatteryLevel(int levelToSet) {
  29. batteryLevel = levelToSet;
  30. }
  31.  
  32. void PrintBatteryLevel() {
  33. cout << "Battery: " << batteryLevel;
  34. }
  35.  
  36. private:
  37. int batteryLevel;
  38. };
  39.  
  40. int main() {
  41. ElectricCar myCar;
  42.  
  43. myCar.SetSpeed(50);
  44. myCar.SetBatteryLevel(15);
  45.  
  46. myCar.PrintCarSpeed();
  47. cout << endl;
  48. myCar.PrintBatteryLevel();
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Driving at: 50
Battery: 15