Let's assume I have a base class
class Base
{
public:
Base();
Base(int i, double j);
Base(int i, double j, char ch);
virtual void print();
private:
int m;
double l;
char n;
};
And I want to derive a class which overrides the print function but apart from that is exactly the same as the base class.
class Derived : public Base
{
public:
void print();
};
Is there a way I can use all the constructors of the base class on the derived class without me rewriting all of them for the Derived class?
Since C++11, you may use using
for that:
class Derived : public Base
{
public:
using Base::Base; // use all constructors of base.
void print() override;
};
Live demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With