496. Toy Factory
class Toy {
public:
virtual void talk() const=0;
};
class Dog: public Toy {//继承
// Write your code here
void talk() const{
cout << "Wow" << endl; //多态
}
};
class Cat: public Toy {
// Write your code here
void talk() const{
cout << "Meow" << endl;
}
};
class ToyFactory {
public:
/**
* @param type a string
* @return Get object of the type
*/
Toy* getToy(string& type) {
// Write your code here
if(type == "Dog"){
return new Dog();
}
if(type == "Cat"){
return new Cat();
}
return NULL;
}
};
Comments
Post a Comment