我正在开发一个程序,该程序基本上应该列出IceCreams及其成分,名称,价格和折扣,以给出的代码点来说,我正尝试使用2种不同的Ice Cream名称,如果用户输入了Caramel Apple,愉悦和巧克力,我需要它来给我“焦糖苹果愉悦+巧克力”,由于我对运算符(operator)的工作方式还不熟悉,而这需要与运算符(operator)一起完成,因此我已经坚持了好几个小时,遇到类似的问题,我不知道该怎么做,我已经考虑过连接字符串,但是对于打印部分却没有任何意义。

IceCream &operator +(const IceCream &i){
        IceCream temp;
        temp.name = strncpy(this->name) + strncat(i.name);
        if(*this>temp){
            delete [] temp.name;
            temp.name=new char [strlen(this->name)+1];
            strncpy(temp.name,this->name,strlen(this->name)+1);

        }
        return temp;
    }

这是代码的其余部分,我知道我对此有一些问题,但这是我需要帮助解决的问题,因为我无法理解所有运算符(operator)的工作方式。
class IceCream{
    private:
    char *name;
    char ingr[100];
    float price;
    int discount;

    public:
    IceCream(){name=new char [0];}
    IceCream(char *name=" ", char* ingr=" ", float price=0.0, int discount=0){
        this->name=new char[strlen(name)+1];
        strcpy(this->name, name);
        strcpy(this->ingr,ingr);
        this->price=price;
        this->discount=discount;
    }

    IceCream(const IceCream &i){
        this->name=new char[strlen(i.name)+1];
        strcpy(this->name,i.name);
        strcpy(this->ingr,i.ingr);
        this->price=i.price;
        this->discount=i.discount;
    }

    ~IceCream(){delete [] this->name;}

    friend ostream& operator<<(ostream& out, IceCream &i){
        if(i.discount>0){
            out<<i.name<<": "<<i.ingr<<" "<<i.ingr" "<<"("i.discount")"<<endl;
        }
        else{
            out<<i.name<<": "<<i.ingr<<" "<<i.price" "<<endl;
        }
        return out;
    }


    IceCream &operator ++(int){
        discount+=5;
        return *this;
    }

    IceCream &operator +(const IceCream &i){
        IceCream temp;
        temp.name = strncpy(this->name) + strncat(i.name);
        if(*this>temp){
            delete [] temp.name;
            temp.ime=new char [strlen(this->name)+1];
            strncpy(temp.name,this->name,strlen(this->name)+1);

        }
        return temp;
    }

最佳答案

您可以更改您的设计并为该问题提供更好的解决方案:

// DecoratorPattern.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

class IiceCream
{
public:
    virtual void Make() = 0;
    virtual ~IiceCream() { }

};

class SimpleIceCream: public IiceCream
{
public:
    virtual void Make()
    {
        std::cout<<"\n milk + sugar +  Ice cream Powder";
    }


};

class IceCreamDecorator: public IiceCream
{

public:
    IceCreamDecorator(IiceCream& decorator):m_Decorator(decorator)
    {

    }

    virtual void Make()
    {
        m_Decorator.Make();
    }
    private:
    IiceCream& m_Decorator;
};

class WithFruits : public IceCreamDecorator
{

public:
     WithFruits(IiceCream& decorator):IceCreamDecorator(decorator)
     {

     }
     virtual void Make()
     {
         IceCreamDecorator::Make();
         std::cout<<" + Fruits";
     }

};

class WithNuts : public IceCreamDecorator
{

public:
    WithNuts(IiceCream& decorator):IceCreamDecorator(decorator)
    {

    }

    virtual void Make()
    {
        IceCreamDecorator::Make();
        std::cout<<" + Nuts";
    }

};

class WithWafers : public IceCreamDecorator
{

public:
    WithWafers(IiceCream& decorator):IceCreamDecorator(decorator)
    {

    }

    virtual void Make()
    {
        IceCreamDecorator::Make();
        std::cout<<" + Wafers";
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
    IiceCream* pIceCreamSimple = new SimpleIceCream();
    pIceCreamSimple->Make();

    IiceCream* pIceCreamFruits = new WithFruits(*pIceCreamSimple);
    pIceCreamFruits->Make();

    IiceCream* pIceCreamNuts   = new WithNuts(*pIceCreamFruits);
    pIceCreamNuts->Make();

    IiceCream* pIceCreamWafers = new WithWafers(*pIceCreamNuts);
    pIceCreamWafers->Make();

    delete pIceCreamSimple;
    delete pIceCreamFruits;
    delete pIceCreamNuts;
    delete pIceCreamWafers;

    return 0;
}

此解决方案使用decorator design pattern

08-06 00:39
查看更多