-3

i'm trying to make specifics statements on the same class function's. there's an example of what i'm triying to make

#include <stdio.h>

class animal
{
   public:
      void Talk();
};

int main()
{
   animal dog;
   animal cat;

   dog::Talk()
   {
      printf("Wof");
   };

   cat::Talk()
   {
      printf("Meow");
   };

   dog.Talk();
   cat.Talk();

   return 0;
}

I also try it with class inheritance, something like

#include <stdio.h>

class cat
{
   public:
      void Talk()
      {
         printf("Meow");
      };
};

class dog
{
   public:
      void Talk()
      {
         printf("Wof");
      }
};

class animal{};

int main()
{
   animal Schnauzer: public dog;
   animal Siamese: public cat;

   Schnauzer.Talk();
   Siamese.Talk();

   return 0;
}

There's a way to do something like this?

1 Answers1

0

It is a very basic thing to do in c++. You just have to know a little bit about inheritance. However, I am guessing that you are new to c++ and don't have much experience in using inheritance. So, I am giving you a simple solution using class inheritance below. Feel free to ask me if there is any confusion.

#include <iostream>

using namespace std;

class animal {
    public:
    virtual void Talk() {cout << "default animal talk" << endl;}
};

class dog: public animal {
    public:
    void Talk() {cout << "Wof" << endl;}
};

class cat: public animal {
    public:
    void Talk() {cout << "Meow" << endl;}
};

int main()
{
    dog Schnauzer;
    cat Siamese;

    Schnauzer.Talk();
    Siamese.Talk();
    return 0;
}