(Pointers to derived classes) Write
a program to illustrate how pointers to a base class is used for both base and
derived class.
#include <iostream>
struct Base {
virtual ~Base() = default;
virtual void Print() const {
std::cout << "Base\n";
}
};
struct Derived : public Base {
void Print() const override {
std::cout << "Derived\n";
}
};
int main() {
Base* arr[] = { new Base{}, new Derived{} };
arr[0]->Print();
arr[1]->Print();
delete arr[0];
delete arr[1];
return 0;
}
Comments
Leave a comment