I am trying to learn Design Patterns with C++. I am implementing the Program given in the first chapter of OReilly's Head First Design Patterns regarding the Duck problem. Please bear with me, it's quite a long question.
Anyways, I have tried to create the following 2 interfaces:
class QuackBehavior
{
public:
virtual void quack() = 0 ;
};
class FlyBehavior
{
public:
virtual void fly() = 0 ;
};
Now i have a Duck class, which needs to have instants of the above 2 classes. What I am doing is:
class Duck
{
public:
FlyBehavior flyBehavior;
QuackBehavior quackBehavior;
Duck()
{
}
virtual void display() = 0 ;
void performQuack()
{
quackBehavior.quack() ;
}
void performFly()
{
flyBehavior.fly() ;
}
void swim()
{
std::cout << "All ducks swim!\n" ;
}
};
I have also created classes that implement the interfaces:
class Quack: public QuackBehavior
{
void quack()
{
std::cout << "QUACK!\n" ;
}
};
class FlyWithWings: public FlyBehavior
{
void fly()
{
std::cout << "I'm flying...\n" ;
}
};
and likewise. I created a class inheriting the Duck class and my main method:
class MallardDuck: public Duck
{
public:
MallardDuck()
{
quackBehavior = new Quack() ;
flyBehavior = new FlyWithWings() ;
}
void display()
{
std::cout << "I'm a real duck\n" ;
}
};
int main(int argc, char const *argv[])
{
Duck mallard = new MallardDuck() ;
mallard.performFly() ;
mallard.performQuack() ;
return 0;
}
However, when I am compiling the program, I am getting a long list of errors. Can someone help me with this? Thanks in advance to people who actually read the complete problem.