I'm still learning C++; I was trying out how polymorphism works and I got a segmentation fault when calling a virtual method.
(Note: I didn't mark the destructor as virtual, I was just trying out to see what happens.) Here's the code:
#include <iostream>
using namespace std;
class Base
{
protected:
char *name;
public:
Base(char *name)
{
cout << name << ": Base class cons" << endl;
}
~Base()
{
cout << name << ": Base class des" << endl;
}
virtual void disp();
};
void Base::disp()
{
cout << name << ": Base disp()" << endl;
}
class Child : public Base
{
public:
Child(char *name):
Base(name)
{
cout << name << ": Child class cons" << endl;
}
~Child()
{
cout << name << ": Child class des" << endl;
}
virtual void disp()
{
cout << name << ": Child disp()" << endl;
}
};
int main()
{
//Base b;
//b.disp();
Base c = Child("2");
c.disp();
}
Also, if you've any other tips regarding the usage of inheritance and polymorphism in general for someone who knows these concepts in Java, please let me know. Thank you!