Alternative to dynamic casting

Viewed 2837

Is there an alternative to using dynamic_cast in C++?

For example, in the code below, I want to be able to have Cat objects purr. But only Cat objects and not Dog objects. I know this goes against deriving the class from Mammal since it's not very polymorphic, but I still want to know if I can do this without dynamic_cast.

My class declarations

class Mammal
{
       public: 
             virtual void Speak() const {cout << "Mammals yay!\n";}
};
class Cat: public Mammal
{
    public:
        void Speak() const{cout << "Meow\n";}
        void Purr() const {cout <"rrrrrrrr\n";}
};

class Dog: public Mammal
{
   public:
       void Speak() const{cout << "Woof!\n";}
};

In Main

int main()
{

    Mammal *pMammal;

    pMammal = new Cat;

    pMammal->Purr();     //How would I call this without having to use dynamic_cast?

    return 0;
}
4 Answers
Related