OK, I wrote a program and it was something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
// Abstract Parent Class
class Parent
{
public:
virtual void DoSomething() = 0;
};
// Derived Class A
class FirstSon: public Parent
{
public:
void DoSomething() {
std::cout << "A" << std::endl;
}
};
// Derived Class B
class SecondSon: public FirstSon
{
public:
void DoSomething() {
std::cout << "B" << std::endl;
}
};
// Derived Class C
class ThirdSon: public SecondSon
{
public:
void DoSomething() {
std::cout << "C" << std::endl;
}
};
|
In few words, I have a super class from which I'm deriving a class A, from which I'm deriving a class B, from which I'm deriving a class C. The super class has a pure virtual member function, which I want to overload. However, I'm a little confused of what is actually happening. There is nothing wrong with my program, because the correct version of the method DoSomething() is invoked depending on the class that called it.
But my question is: Which version of DoSomething am I overloading? For example, if I call ThirdSon::DoSomething(), is it the overloaded version of SecondSon::DoSomething(), or Parent::DoSomething()? Does this counts as polymorphism?
I did a little test to try to understand, and it was like this:
1 2 3 4 5 6 7 8 9 10 11
|
int main()
{
SecondSon s;
ThirdSon t;
Parent* a;
Parent* b;
a = &s;
b = &t;
a -> DoSomething();
b -> DoSomething();
}
|
The output was:
What I actually want to do is to overload in
all derived classes the virtual member function from Parent, but I don't know if I did it correctly.
Can someone please explain this to me?