I don't know how to correct use of abstract factory.
I have example code:
#include <iostream>
class Animal {
public:
virtual void Eat() = 0;
};
class Fish : public Animal {
public:
/* Overrided method */
void Eat() override {
printf("Fish eats..\n");
}
/* Only fish's class method */
void Swim() {
printf("Fish swims..\n");
}
};
class Bird : public Animal {
public:
/* Overrided method */
void Eat() override {
printf("Bird eats..\n");
}
/* Only bird's class method */
void Fly() {
printf("Bird flies..\n");
}
};
int main() {
Animal* fish = new Fish();
Animal* bird = new Bird();
Fish* fish_ = reinterpret_cast<Fish*>(fish);
Bird* bird_ = reinterpret_cast<Bird*>(bird);
fish_->Eat();
fish_->Swim();
bird_->Eat();
bird_->Fly();
delete fish;
delete bird;
system("pause");
return 0;
}
In main function I creates Fish and Bird objects using Animal interface, and cast it to Animal pointer, in real program I will use interfaces(for example array with Animals, but we don't know what animal is it).
Fish and Bird classes have their own methods, Animal class hasn't it.
I need to call this methods, but I don't is it currectly using cast of interface to Fish or Bird.
Maybe there is a better way?
Aucun commentaire:
Enregistrer un commentaire