I want to learn virtual functions. I have made print() in class Base virtual and my program crashes. Why do I have to create the two objects with new?
#include <iostream>
class Base {
public:
virtual void print();
};
void Base::print() {
std::cout << "Base print method" << std::endl;
}
class Derived : public Base {
public:
void print();
};
void Derived::print() {
std::cout << "Derived print method" << std::endl;
}
int main()
{
Base* b; // Base* b = new Base(); runs
b->print();
Derived* d; // Derived *d = new Derived(); runs
d->print();
}