I have the following code:
#include <iostream>
using namespace std;
class Base {
protected:
char const* name;
public:
Base(char const* n) : name{n} {
cout << "Base constructed" << endl;
}
};
class Color : virtual public Base {
protected:
Color(char const* n) : Base(n) {
cout << "Color constructed" << endl;
}
};
class Text : public virtual Base {
protected:
Text(char const* n) : Base(n) {
cout << "text constructed" << endl;
}
};
class NexButton : public Color , public Text {
public:
NexButton(char const* n) : Base(n), Color(n), Text(n) {
cout << "NexButton constructed" << endl;
}
};
int main() {
NexButton btn{"b0"};
}
Which outputs:
Base constructed
Color constructed
text constructed
NexButton constructed
However this code below:
#include <iostream>
using namespace std;
class Base {
protected:
char const* name;
public:
Base(char const* n) : name{n} {
cout << "Base constructed" << endl;
}
};
class Color : virtual public Base {
using Base::Base;
};
class Text : public virtual Base {
using Base::Base;
};
class NexButton : public Color , public Text {
using Color::Color;
using Text::Text;
};
int main() {
NexButton btn{"b0"};
}
Outputs:
Base constructed
I don't understand why the second code snippet only outputs Base constructed; shouldn't the derived classes output something to the terminal aswell? As they are inheriting the constructor? Other than that the derived classes are outputting to cout aren't both code snippets in a lot of ways the same? (Not identical)
Visually, why is the output not:
Base constructed
Base constructed
Base constructed
Base constructed