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 7722253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:07:24+00:00 2026-06-01T04:07:24+00:00

So, I need to write an example code using the decorator dessign pattern and

  • 0

So, I need to write an example code using the decorator dessign pattern and this is my example, it’s very simple but idk why it doesn’t print what I want.
I’m new here, this is my first question so idk if im doing this in the right way but here’s the code in C++:

include < iostream >

using namespace std;

class CafeAbstracto {
protected:
    int size;
    int precio;
public:

    void setsize(int n) {
        this->size = n;
        this->precio = n*10;
    }

    void setprecio(int p) {
        this->precio += p;
    }
    int getprecio(){
        return this->precio;
    }
    virtual void imprimirprecio(void) = 0;
};

class Cafe : public CafeAbstracto {
public:  

    void imprimirprecio() {
       int prec=getprecio();
        cout << "El precio del cafe es de: " << prec << endl;
    }
};

class CafeDecorator : public CafeAbstracto {
protected:

    CafeAbstracto *Cafe_con_extras;
public:

    CafeDecorator(CafeAbstracto *nuevo_cafe) {
        Cafe_con_extras = nuevo_cafe;
    }
    virtual void imprimirprecio()=0;
};

class Leche : public CafeDecorator {
public:

    Leche(CafeAbstracto* nuevo_cafe, bool tiene) : CafeDecorator(nuevo_cafe) {
    }

     void imprimirprecio() {        
        cout << "El cafe tiene leche" << endl;
        Cafe_con_extras->setprecio(5240);
        Cafe_con_extras->imprimirprecio();
    }
};

class Azucar : public CafeDecorator {
public:

    Azucar(CafeAbstracto* nuevo_cafe, bool tiene) : CafeDecorator(nuevo_cafe) {
    }

    virtual void imprimirprecio() {
        Cafe_con_extras->setprecio(50);
        cout << "El cafe tiene azucar" << endl;
        Cafe_con_extras->imprimirprecio();
    }
};

class Descafeinado : public CafeDecorator {
public:

    Descafeinado(CafeAbstracto* nuevo_cafe, bool es) : CafeDecorator(nuevo_cafe) {
    }
    void imprimirprecio() { 
        Cafe_con_extras->setprecio(100);
        cout << "El cafe es descafeinado" << endl;
        Cafe_con_extras->imprimirprecio();
    }
};

int main(int argc, char *argv[]) {

    Cafe *coffee = new Cafe();
    coffee->setsize(6);
    Descafeinado *cafedescaf = new Descafeinado(coffee, true);
    cafedescaf->imprimirprecio();

    Leche *descafyconleche = new Leche(cafedescaf, true);
    descafyconleche->imprimirprecio();
    Azucar *cafe_des_leche= new Azucar(descafyconleche, true);
    cafe_des_leche->imprimirprecio();

    delete coffee;
    delete cafedescaf;
    delete descafyconleche;
    delete cafe_des_leche;

    return (0);

}

this is the output

El cafe es descafeinado
El precio del cafe es de: 160 //this one is correct

El cafe tiene leche
El cafe es descafeinado
El precio del cafe es de: 260 //incorrect

El cafe tiene azucar
El cafe tiene leche
El cafe es descafeinado
El precio del cafe es de: 360 //incorrect

  • 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-06-01T04:07:25+00:00Added an answer on June 1, 2026 at 4:07 am
    Leche *descafyconleche = new Leche(cafedescaf, true);
    descafyconleche->imprimirprecio();
    Azucar *cafe_des_leche= new Azucar(descafyconleche, true);
    cafe_des_leche->imprimirprecio();
    

    should be:

    Leche *descafyconleche = new Leche(coffee, true);
    descafyconleche->imprimirprecio();
    Azucar *cafe_des_leche= new Azucar(coffee, true);
    cafe_des_leche->imprimirprecio();
    

    Because you need to use the decorator to decorate your coffe (your concrete component), not to decorate another decorator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I need to write a java script. This is supposed to validate if the
I need to write code that picks up PGP-encrypted files from an FTP location
Here's an interesting problem. Is there a way to write some code using LINQ
I need to write some code that deals with generating and manipulating multivariable polynomials.
I'm not clear on how to write simple byte code arrays with ruby, more-so
I am not sure if this is possible and I may need to write
I need write an update statement that used multiple tables to determine which rows
I need to write a program used internally where different users will have different
I need to write a Java Comparator class that compares Strings, however with one

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.