1

I have made an example that shows exactly my point without any additional code from my actual project. I have two classes that are parent and child and each have a getClass() method that return a string with the name of their class. Obviously the getClass() method inside the child overrides the one in Parent. Then I have an additional method getClass2() that instead returns the string that is returned from getClass(). Right now it doesn't make sense why I would do that, but as I mentioned this is a simplified example. This is my code:

#include <iostream>
#include <string>

using namespace std;

class Parent
{
    public:
        string getClass(){
            return "I'm a Parent";
        }

        string getClass2(){
            return getClass();
        }
};

class Child : public Parent
{
    public:
        string getClass(){
            return "I'm a Child";
        }
};

So what happens is that i create two objects, one of each class and then i call the methods getClass() and getClass2 once for each of them.

int main()
{
    Parent p;
    Child c;

    cout << p.getClass() << endl;
    cout << c.getClass() << endl;

    cout << p.getClass2() << endl;
    cout << c.getClass2() << endl;

    return 0;
}

The getClass() calls work as intended and print:

I'm a Parent
I'm a Child

But the getClass2() calls both print the same thing:

I'm a Parent
I'm a Parent

So my question is why does calling a method(getClass) from within another method(getClass2), use the version that's inside the Parent class and not the overriden version inside the Child class.

NOTE: I care more in how it works that how to fix it. Thanks in advance to whoever can offer me help.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Mako
  • 33
  • 3
  • 1
    You need to explicitly override the method. The parent function needs to be `virtual` for this to work. – Raildex May 07 '21 at 12:25
  • 2
    If you are used to, for example, Java, you probably expect the function in the derived class to override the function with the same name in the parent class **by default**. Not so in C++. See [`virtual` keyword](https://en.cppreference.com/w/cpp/language/virtual). – BoBTFish May 07 '21 at 12:27
  • 1
    Please pick up [a decent book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or find a tutorial about *polymorphism* and *virtual functions*, which what makes overriding work in C++. – Some programmer dude May 07 '21 at 12:28
  • Hey, thanks for the tip. I'm trying to understand how exactly the code i have right now works though. My assumption right now is that when i call getClass from withing getClass2, i end up mentioning only the version of getClass inside the parent method. Is that right? Or am i understanding this wrong? – Mako May 07 '21 at 12:30

1 Answers1

2

In C++, methods have to be explicitely marked as virtual for polymorphism to be effective. If a method is virtual, the most derived override will be used whatever class the calling method is in. If it is not, the version of the calling class will be used, whatever the actual class of the object.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Oh ok, yh this makes sence. So the reason im getting the wrong outcome is because without using the virtual keyword i end up mentioning the specific implementation of my method that is inside the parent class. Am i understanding this correctly? – Mako May 07 '21 at 12:38