从字符串常量中删除已弃用转换为'char *的警告

时间:2018-01-16 00:41:33

标签: c++ inheritance char const

我写了一个像账单支付的代码。代码工作正常但我的代码中有许多警告要删除。最常见的警告之一是已弃用从字符串常量转换为'char * 。我尝试了很多东西,但有些警告已经消失,但并非全部。请有人指出我的错误吗?

P.S:我已经尝试将char *替换为const char *,但是后来我无法交换或交换值而导致错误。任何其他解决方案??

以下是代码

#include<iostream>
#include<string.h>
using namespace std;

class item
{
    private:
        int barcode;
        char* item_name;

    public:
        item (int num=0, char* name="NULL") : barcode(num)
        {
            item_name=new char[strlen(name)+1];
            strcpy(item_name,name);
        }
        void setbarcode(int num)
        {
            barcode=num;
        }
        int getbarcode()
        {
            return barcode;
        }
        void scanner()
        {
            int num;
            cin>>num;
            setbarcode(num);
        }
        void printer()
        {

            cout <<"\nBarcode"<<"\t\t"<<"Item Name"<<"\t\t"<<"Price"<<endl;
            cout <<barcode<<"\t\t"<<item_name<<"\t\t\t";
        }
        ~item()
        {
            delete[]item_name;
        }
};

class packedfood : public item
{
    private :
        int price_per_piece;

    public :
        packedfood(int a=0, int num=0, char* name = "NULL") : price_per_piece(a),item(num,name)
        {
        }

    void setprice(int num)
    {
        price_per_piece=num;
    }   
    int getprice()  
    {
        return price_per_piece;
    }   

    void scanner()
    {
        item::scanner();
    }

    void printer()  
    {
        item::printer();
        cout<<getprice()<<"  Per Piece";
    }   
    ~packedfood()
    {
    }   
};

class freshfood: public item
{
    private:
        int price_per_rupee;
        float weight; 

    public:
        freshfood(float b=0, int a=0,int num=0,char* name="NULL") : weight(b),price_per_rupee(a),item(num,name)
        {
        }
    void setweight(float b)
    {   
        weight=b;
    }
    int getweight()
    {
        return weight*50;
    }
    void printer()
    {
        item::printer();
        cout<<getweight()<<" Per Rupee"<<endl<<endl;    
    }
    void scanner()
    {
        item::scanner();
    }
    ~freshfood()
    {
    }
};  

int main()
{
    item x(389,"Anything");
    item y;
    packedfood m(10,118,"Chocolate");
    packedfood n;
    freshfood r(20.9,93,357,"Fruits");
    freshfood s;

    cout <<"\n\n Enter the Barcode for Packed food : ";
    m.scanner();
    m.printer();

    cout <<"\n\n Enter the Barcode for Fresh food : ";
    r.scanner();
    r.printer();    

    system("PAUSE");
    return 0;
}

0 个答案:

没有答案