If two classes have methods with the exact same name, and one class inherits the other, how does the program know which method should be called? Example:
class One
{
public:
int x, y;
void Print()
{
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
}
};
class Two : public One
{
public:
int X, Y;
void Print()
{
std::cout << "X = " << X << std::endl;
std::cout << "Y = " << Y << std::endl;
}
};
int main()
{
Two two;
two.x = 10;
two.y = 15;
two.X = 20;
two.Y = 30;
two.Print();
std::cin.get();
}
Output:
"X = 20"
"Y = 30"
So why did the program decide to call the 'Two' member function 'Print()' rather than the inherited 'One' member function 'Print()', and how is this decision made?
I'm pretty sure someone will answer your question in a competent way, so I won't try. But I think this issue is merely a problem of bad design on your part.
First it's a bad idea to have variables in your public part of your classes. It's a good habit to have accessor and mutator functions to change their values.
Second you should name your variables accordingly to what they hold a value for and not just x, y, z, i, j etc.
Therefore their accessors and mutators would have a name to go along with that.
What's the point of having an inherited class that is identical to the mother class? You'd better just create a second object of the first class.
I changed your code a bit to illustrate my purpose
H00G0: This isn't a program that I am actually planning to use... I just created this quickly as an example. There is no point in having an inherited class that is identical to the base class, I just wanted to try and understand how the program would distinguish between the two functions in this kind of situation.
I can't really answer you accurately, as I said. My best guess would be that if you have an overloaded function between the mother and child class, the first one that is encountered will be the one called i.e the one from the inherited class.
I'd be interested as well, if someone could explain in detail how this works..
It's call "shadowing" a function. You declared a variable of type Two, so it will choose Two's (or whatever the "closest" subclass is in the case of more levels of inheritance) member function. In renders One's member function inaccessible unless you cast the object and slice it.
#include <iostream>
class One
{
public:
int x, y;
void Print()
{
std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;
}
};
class Two : public One
{
public:
int X, Y;
void Print()
{
std::cout << "X = " << X << std::endl;
std::cout << "Y = " << Y << std::endl;
}
};
int main()
{
Two two;
two.x = 10;
two.y = 15;
two.X = 20;
two.Y = 30;
two.Print();
One one = two; // slice two into one
one.Print();
}
As Hoogo said, it's probably bad design to have to do this. I would avoid anything where the subclass and superclass have public variables that differ by just capitalization. If polymorphism is wanted, then virtual functions should be used.