Today I was taught about virtual methods but got lost somewhere in the pointers.
Here is an example
class Person
{
protected:
string name;
public:
virtual string get_name()
{
return name;
}
};
class TFaculty:public Person
{
private:
string title;
public:
virtual string get_name()
{
return title + " " + Person::get_name();
}
};
int main()
{
Person *arr[2]=
{ new TFaculty("Jane Doe", "Dr."),
new TFaculty("John Doe","Mr.")
}
return 0;
}
My code might be incomplete as I can't remember all of it.
What I'm confused about is that I have a two element array pointing to class Person which each element makes a new TFaculty object. I'm confused on how we are able to create an object of TFaculty if we are pointing to Person?
I know i'm seeing things wrong I hope someone can show me what is really going on.
The way i see it is like this:
int *ptr;
ptr=new double
I know this is not possible. So how is it that we can create a different datatype TFaculty when it's pointing to Person?
Please help me understand thanks.