我正在开发一个程序,基本上是假设将IceCreams的成分,名称,价格和折扣列在代码中,我试图采用2种不同的冰淇淋名称,如果说是用户进入Caramel Apple Delight和巧克力我需要它给我“Caramel Apple Delight +巧克力”,因为我对操作员的工作原理很新,而且这需要与操作员一起完成,我已经被困了几个小时,我我遇到了类似的问题,我不知道怎么做到这一点,我考虑过连字符串,但是对于打印部分它没有任何意义。
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;
}
这是代码的其余部分,我知道我有一些问题,但这是我需要帮助解决的问题,因为我无法理解所有运算符的工作原理。
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;
}
答案 0 :(得分:0)
您可以更改设计并为此问题提供更好的解决方案:
// 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。
答案 1 :(得分:0)
您无法使用+
连接char
数组。您需要为new
的新字符串分配空间,然后复制并连接到该字符串。
IceCream &operator +(const IceCream &i){
IceCream temp;
delete[] temp.name; // Free the string allocated by the default constructor
temp.name = new char[strlen(this->name) + strlen(i.name) + 1];
strcpy(temp.name, this->name);
strcat(temp.name, i.name);
return temp;
}