0

I am new to C++ and currently I am studying polymorphism.

I have this code:

#include <iostream>
class Base
{
    public:
        void say_hello()
        {
            std::cout << "I am the base object" << std::endl;
        }
};


class Derived: public Base
{
    public:
        void say_hello()
        {
            std::cout << "I am the Derived object" << std::endl;
        }
};

void greetings(Base& obj)
{
    std::cout << "Hi there"<< std::endl;
    obj.say_hello();
}



int main(int argCount, char *args[])
{

    Base b;
    b.say_hello();

    Derived d;
    d.say_hello();

    greetings(b);

    greetings(d);

    return 0;
}

Where the output is:

I am the base object
I am the Derived object
Hi there
I am the base object
Hi there
I am the base object

This is not recognizing the polymorphic nature in the greetings functions.

If I put the virtual keyword in the say_hello() function, this appears to work as expected and outputs:

I am the base object
I am the Derived object
Hi there
I am the base object
Hi there
I am the Derived object

So my question is: The polymorphic effect is retrieved when using a pointer/reference?

When I see tutorials they will present something like:

Base* ptr = new Derived();
greetings(*ptr);

And I was wondering if had to always resort to pointers when using polymorphism.

Sorry if this question is too basic.

Trailer
  • 155
  • 9
  • You're almost there, just search around for "virtual functions in C++", you'll find lots of good guides. – JMAA Nov 29 '21 at 00:05
  • Related topics that may help clarify a few things: [What is object slicing?](https://stackoverflow.com/q/274626/11082165) and [Why do we need virtual functions in C++?](https://stackoverflow.com/q/2391679/11082165) – Brian61354270 Nov 29 '21 at 00:07
  • But am I correct in assuming that through references I get the polymorphic behavior correct? – Trailer Nov 29 '21 at 00:15
  • @Brian. Slicing just means that only the "base" part of the object is collected/copied. Which means I only get Base type functions, no polymorphism. But if I use a reference or a pointer the slicing will get me the polymorphic part. Is this correct? – Trailer Nov 29 '21 at 00:29
  • 2
    With a reference or pointer there is no copy. No copy, no slice. – user4581301 Nov 29 '21 at 00:34

2 Answers2

1

For polymorphic behavior you need 2 things:

  • a virtual method overridden in a derived class
  • an access to a derived object via a base class pointer or reference.
Base* ptr = new Derived();

Those are bad tutorials. Never use owning raw pointers and explicit new/delete. Use smart pointers instead.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • Hi, thanks for the reply. Currently I have not been taught about smart pointers. I just want to know if the polymorphic behavior can be obtained through a reference or if I must always have a pointer. If the first is not correct, why is my code showing the correct output – Trailer Nov 29 '21 at 00:17
  • as I've said, pointers or references. – bolov Nov 29 '21 at 00:34
  • *"Use smart pointers instead."* Or just `Derived d; Base& b{d};` :-) – Jarod42 Nov 29 '21 at 08:47
0

Just add a virtual declaration to the method to allow it to be overridden when using references to the base class:

virtual void say_hello() {...}

in both classes (or at least just the base class).

Adriel Jr
  • 2,451
  • 19
  • 25