How to port Java interface codes into C++ abstract base class? -
im new in oop programming , c++ , im learning design patterns.
just grab head first design patterns book learn from. great , im getting hold basic concepts.the first chapter talks programming interface rather implementation. unfortunately me though, examples uses java.
below java code sample books using "interface", understand cannot directly ported in c++. ive tried implement c++'s abstract base class. im lost in setting quackbehavior dynamically.
can c++ virtual functions definition dynamic? can please show me how best way port java code c++? need sure im on right track in learning oop. thanks!
//flybehavior.java public interface flybehavior{ public void fly(); //the interface flying behavior classes implement } public class flywithwings implements flybehavior { public void fly(){ system.out.println("im flying!"); //flying behavior implementation ducks fly } } //quackbehavior.java public interface quackbehavior{ public void quack(); } public class quack implements quackbehavior { public void quack() { system.out.println("quack"); } } public class squeak implements quackbehavior { public void quack() { system.out.println("squeak"); } } public class mutequack implements quackbehavior { public void quack() { system.out.println("<<silence>>"); } } //duck.java public abstract class duck{ flybehavior flybehavior; quackbehavior quackbehavior; public duck(){ } public abstract void display(); public void performfly(){ flybehavior.fly(); } public void performquack(){ quackbehavior.quack(); } public void setflybehavior(flybehavior fb){ flybehavior = fb; } public void setquackbehavior(quackbehavior qb){ quackbehavior = qb; } public void swim(){ system.out.println ("all duck float, decoys"); } } //mallardduck.java public class mallardduck extends duck{ public mallardduck(){ quackbehavior = new quack(); flybehavior = new flywithwings(); } public void display(){ system.out.println("im real mallard duck"); } } //miniducksim.java public class miniducksimulator{ public static void main (string[] args){ duck mallard = new mallardduck(); mallard.setquackbehavior(new squeak()); // im lost //how can definition of virtual functions in c++ dynamic? mallard.performquack(); mallard.performfly(); } }
dang - should have known head first gang port it. anyway here's 1 solution:
// headfirst.cpp : defines entry point console application. // #include "stdafx.h" class quackbehavior { public: virtual void quack() = null; }; class quack : public quackbehavior { public: void quack() { printf("quack"); } }; class squeak : public quackbehavior { public: void quack() { printf("squeak"); } }; class duck { public: quackbehavior *pquack; void performquack() { if (pquack!=null) pquack->quack(); } void setquackbehavior(quackbehavior *qb) { pquack = qb; } }; class mallardduck : public duck { public: mallardduck() { pquack = new quack(); } }; int _tmain(int argc, _tchar* argv[]) { duck *mallard = new mallardduck(); mallard->setquackbehavior(new squeak()); mallard->performquack(); return 0; }
Comments
Post a Comment