Constructor Order Inheritance

#include <iostream>

// TODO 1: Base driver class
class base_driver {
public:
    base_driver() {
        std::cout << "Base driver initialized\n";
    }
};

// TODO 2: Derived driver class
class driver : public base_driver {
public:
    int val;

    driver(int n) : val(n) {
        std::cout << "Derived driver initialized\n";
    }

    void print() {
        std::cout << "Driver value " << val << "\n";
    }
};

int main() {
    int value;
    std::cin >> value;

    // TODO 3: Create derived driver object
    driver d(value);

    // TODO 4: Print value
    d.print();

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

Base driver initialized Derived driver initialized Driver value 5