Please take a look at my code and suggest how I change the code in the class definition, not main.
The using namespace std;
is useless in there, just remove it.
Then, you are having virtual
methods and protected
members, which suggests that you are willing to inherit from that class: either don't or define a virtual
destructor as well.
Also, the speak
member function you are calling from the main, returns void
, which is not a type you want to pass to operator<<
of std::cout
.
And finally: why are you using nake pointers and dynamic allocation? Don't use it unless you are absolutely forced to, and even then, use std::shared_ptr
or std::unique_ptr
(or any other of the smart pointers family) instead:
std::unique_ptr<Pet> ptr(new Pet( "Sunset", "Cat", "meow..." ));
After that line, ptr
will behave almost like any other pointer, except it will clean himself up (will call delete
) when you are done with it (when it leaves the scope).