Constructor Order Inheritance

    // TODO 4: Call function to print the value

  #include <iostream>

  // Define the base driver class
  class BaseDriver {
  public:
      BaseDriver() {
              std::cout << "Base driver initialized" << std::endl;
                  }
                  };

                  // Define the derived driver class, inheriting publicly from BaseDriver
                  class DerivedDriver : public BaseDriver {
                  private:
                      int value;

                      public:
                          // Constructor for DerivedDriver that receives and stores an integer value
                              DerivedDriver(int n) : value(n) {
                                      std::cout << "Derived driver initialized" << std::endl;
                                          }

                                              // Function to print the stored value
                                                  void printValue() {
                                                          std::cout << "Driver value " << value << std::endl;
                                                              }
                                                              };

                                                              int main() {
                                                                  int input_value;

                                                                      // 1. Read one integer from standard input
                                                                          if (!(std::cin >> input_value)) {
                                                                                  // Handle input error if necessary
                                                                                          return 1;
                                                                                              }

                                                                                                  // 2. Create one object of the derived driver
                                                                                                      // The program output will show that 'Base driver initialized' is printed
                                                                                                          // before 'Derived driver initialized', proving the execution order.
                                                                                                              DerivedDriver driver_object(input_value);

                                                                                                                  // 3. Call the derived driver function to print the value
                                                                                                                      driver_object.printValue();

                                                                                                                          return 0;
                                                                                                                          }
                                                                                                                            

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

Base driver initialized Derived driver initialized Driver value 5