fork download
  1. /** Simple Multiple Inheritance Example in C++17.
  2.  *
  3.  * This example demonstrates basic multiple inheritance concepts
  4.  * with clear, minimal code showing how a derived class can inherit
  5.  * from multiple base classes.
  6.  *
  7.  * @file Example2.cpp
  8.  * @version 1.0 @date 2026-02-13
  9.  */
  10.  
  11. #include <iostream>
  12. #include <string>
  13.  
  14. /** First base class - provides speaking capability.
  15.  */
  16. class Speaker {
  17. public:
  18. /** Make the object speak.
  19.   */
  20. void speak() {
  21. std::cout << "I can speak!" << std::endl;
  22. }
  23. };
  24.  
  25. /** Second base class - provides walking capability.
  26.  */
  27. class Walker {
  28. public:
  29. /** Make the object walk.
  30.   */
  31. void walk() {
  32. std::cout << "I can walk!" << std::endl;
  33. }
  34. };
  35.  
  36. /** Derived class using multiple inheritance.
  37.  * Inherits from both Speaker and Walker classes.
  38.  */
  39. class Person : public Speaker, public Walker {
  40. private:
  41. std::string name; /*!< Person's name. */
  42.  
  43. public:
  44. /** Person constructor.
  45.   *
  46.   * @param name The person's name.
  47.   */
  48. Person(const std::string & name) : name(name) {}
  49.  
  50. /** Introduce the person.
  51.   */
  52. void introduce() {
  53. std::cout << "Hello, I am " << name << ". ";
  54. speak(); // From Speaker base class
  55. walk(); // From Walker base class
  56. }
  57. };
  58.  
  59. /** Another example - Robot that can speak and walk.
  60.  */
  61. class Robot : public Speaker, public Walker {
  62. private:
  63. std::string model; /*!< Robot model. */
  64.  
  65. public:
  66. /** Robot constructor.
  67.   *
  68.   * @param model The robot model.
  69.   */
  70. Robot(const std::string & model) : model(model) {}
  71.  
  72. /** Display robot capabilities.
  73.   */
  74. void showCapabilities() {
  75. std::cout << "Robot " << model << " capabilities: ";
  76. speak(); // From Speaker
  77. walk(); // From Walker
  78. }
  79. };
  80.  
  81. /** Demonstrate multiple inheritance.
  82.  *
  83.  * @return Exit code.
  84.  */
  85. int main() {
  86. std::cout << "=== Simple Multiple Inheritance Demo ===" << std::endl;
  87.  
  88. // Create a person
  89. Person person("Alice");
  90. std::cout << "\nPerson example:" << std::endl;
  91. person.introduce();
  92.  
  93. // Create a robot
  94. Robot robot("R2-D2");
  95. std::cout << "\nRobot example:" << std::endl;
  96. robot.showCapabilities();
  97.  
  98. // Demonstrate accessing base class methods directly
  99. std::cout << "\nDirect base class access:" << std::endl;
  100. person.speak(); // Call Speaker's method
  101. robot.walk(); // Call Walker's method
  102.  
  103. std::cout << "\n=== Demo Complete ===" << std::endl;
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
=== Simple Multiple Inheritance Demo ===

Person example:
Hello, I am Alice. I can speak!
I can walk!

Robot example:
Robot R2-D2 capabilities: I can speak!
I can walk!

Direct base class access:
I can speak!
I can walk!

=== Demo Complete ===