Constructor Order Inheritance

#include <iostream>
using namespace std;

// TODO 1: Create a base driver class with a constructor
//         that prints "Base driver initialized"

// 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 baseDriver{
    public:
    baseDriver(){
        cout<<"Base driver initialized"<<endl;
    }
};

class Driver: public baseDriver{
    int value;

    public:
    Driver(int n): value(n){
        cout<<"Derived driver initialized"<<endl;
    }
    void print(){
        cout<<"Driver value "<<value<<endl;
    }
};

int main() {
    int value;
    cin >> value;
    Driver d(value);
    d.print();
    // TODO 3: Create a derived driver object using value
    // TODO 4: Call function to print the value

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5

Expected Output

Base driver initialized Derived driver initialized Driver value 5