Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1898346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:45:13+00:00 2026-05-17T06:45:13+00:00

Im new in OOP programming and C++ and Im currently into learning design patterns.

  • 0

Im new in OOP programming and C++ and Im currently into learning design patterns.

Just grab the Head First Design Patterns book to learn from. Its actually great and Im already getting hold into the basic concepts.The first chapter talks about programming to an interface rather an implementation. Unfortunately for me though, the examples uses java.

Below is the java code sample from the books using “interface”, I understand that this cannot be directly ported in C++. Ive tried to implement C++’s abstract base class. But Im lost especially in setting the QuackBehavior dynamically.

Can C++ virtual functions definition be dynamic? Can someone please show me how to best way to port this java code into C++? I need this to be sure im on the right track in learning OOP. Thanks!

//FlyBehavior.java
public interface FlyBehavior{
    public void fly(); //the interface that all flying behavior classes implement
}


public class FlyWithWings implements FlyBehavior {
    public void fly(){
        System.out.println("Im flying!"); //flying behavior implementation for ducks that do 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, even decoys");
    }
}


//MallardDuck.java
public class MallardDuck extends Duck{
    public MallardDuck(){
        quackBehavior = new Quack();
        flyBehavior = new FlyWithWings();
    }

    public void display(){
        System.out.println("Im a real Mallard duck");
    }
}

//miniducksim.java
public class MiniDuckSimulator{
    public static void main (String[] args){
        Duck mallard = new MallardDuck();

        mallard.setQuackBehavior(new Squeak()); // this is where im lost
                                                //how can definition of a virtual functions in C++ be dynamic?

        mallard.performQuack();
        mallard.performFly();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:45:14+00:00Added an answer on May 17, 2026 at 6:45 am

    Dang – should have known the head first gang would port it. Anyway here’s one solution:

       // HeadFirst.cpp : Defines the entry point for the 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;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started programming OOP and I'm running into a scope problem. In the
I just started with OOP on Javascript. I am new to programming world. Could
I'm completely new to OOP PHP and currently reading PHP Objects, Patterns and Practice.
Before I start just letting you know that I'm new to OOP programming so
I'm somewhat new to OOP programming so it's very likely I'm making some stupid
I'm new to programming and some OOP concepts and am not sure what is
Im new to GUI programming, and haven't done much OOP. Im working on a
I've just completed my second OOP class, and both my first and second classes
I'm new to object-oriented programming, and I'm struggling to think of OOP. What I
I recently started learning the basics of OOP in PHP. I am new to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.