1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
struct MyBase {
MyBase (int aa, int bb) : a(aa), b(bb) {}
virtual void show() const { std::cout << "base: " << a << " " << b << '\n'; }
int a {}, b {};
};
struct MyDer : public MyBase {
MyDer(int aa, int bb, int cc) : MyBase(aa, bb), c(cc) {}
void show() const override { std::cout << "derived: " << a << " " << b << " " << c << '\n'; }
int c {};
};
int main() {
const MyBase* elems[] {new MyBase(1, 2), new MyDer(3, 4, 5), new MyBase(6, 7), new MyDer(8, 9, 0)};
for (const auto& e : elems) {
e->show();
delete e;
}
}
|