Array resize bug in beta build 530 - page 8

 
angevoyageur:
After fruits (apple, pear), cat, dog and animal. The lack of imagination is terrible when people are talking about oop. (cyclops993, not about you, but wikipedia.) Which real program need a class "animal" with a method "talk". I will post a "trading" example as soon as possible.

Slightly more complete example script:

class Animal {
    public: virtual string talk() {return "";}
};
 
class Cat : public Animal {
    public: string talk() {return "Meow!";}
};
 

class Dog : public Animal {
    public: string talk() {return "Woof!";}
};

void OnStart()
{
   Animal * random;
   if (GetTickCount() % 2 == 0) {
      random = new Cat();
   } else {
      random = new Dog();
   }
   MessageBox(random.talk());  
   delete random;
}

What I hadn't noticed before it is that it doesn't seem to be possible to have virtual functions with no bodies if there is a reference to the base class. In the above example, you can't do the following:

class Animal {
   public: virtual string talk();
};

...whereas in C++ you can do the following:

class Animal {
    public: virtual const char * talk() = 0; // Pure virtual function
};
 
class Cat : public Animal {
    public: const char *  talk() {return "Meow!";}
};
 

class Dog : public Animal {
    public: const char * talk() {return "Woof!";}
};

void main()
{
   Animal * random;
   if (GetTickCount() % 2 == 0) {
      random = new Cat();
   } else {
      random = new Dog();
   }
   printf(random->talk());   
}
 
SDC:

so what would you call class cat and class dog, are they subclasses of animal ? could you create another subclass of dog in the dog class and call it class pitbull ?

Yes it's exactly that. And when using these classes, you use a variable and affects any objects of one class and its subclasses. As shown in the example of cyclops993 (main function), when you call talk() the good function is used.
 

ok thats kinda neat I think I'm going to make some effort to learn this

 
SDC:

ok thats kinda neat I think I'm going to make some effort to learn this

For example:

class Pitbull : public Dog {
   public: 
      string talk() {return "Growl!";}
      void menace() {}
};

...Pitbulls can not only talk, like cats and other dogs, but can also menace things.

Reason: