Constructor Order Inheritance

#include <iostream>
using namespace std;

class BaseDriver{
    public:
        BaseDriver(){
            cout << "Base driver initialized" << endl;
        }
};

// TODO 2: Create a derived driver class that inherits from the base driver
//         - Constructor takes an int value
//         - Prints "Derived driver initialized"
//         - Stores the value
//         - Has a function to print "Driver value <value>"

class DerivedDriver: public BaseDriver{
    private:
        int driver_value;
    public:
        DerivedDriver(int value): driver_value(value){
            cout << "Derived driver initialized" << endl;
        }

        void print(){
            cout << "Driver value " << driver_value << endl;
        }
};

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

    // TODO 3: Create a derived driver object using value
    DerivedDriver d(value);
    // TODO 4: Call function to print the value
    d.print();

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

Base driver initialized Derived driver initialized Driver value 5