Step 3: Create a header file for the derived class declaration. Let's name it "Derived.h".
// Derived.h
#include "Base.h"
class Derived : public Base {
private:
int derivedVar;
public:
Derived();
Derived(int baseValue, int derivedValue);
void display();
};
Step 4: Create a source file for the derived class implementation. Let's name it "Derived.cpp".
In the above example, the base class "Base" contains a single member variable "baseVar" and a constructor to initialize it. It also has a member function "display()" to display the value of "baseVar".
The derived class "Derived" inherits from the base class "Base" and adds its own member variable "derivedVar". It defines its own constructor and overrides the "display()" function to display the value of both "baseVar" and "derivedVar".
To compile and run the program, you can use the following command:
This example demonstrates how to split the class definitions into separate header and source files, and how to properly use constructors to initialize the base and derived classes.