0

I have written a code like bill payment. Code is working fine but there are many warnings in my code which I want to remove. One of the most frequent warning is deprecated conversion from string constant to 'char* . I have tried many things and some of the warnings are gone but not all. Please anybody point out at my mistakes??

P.S: I have already tried replacing char* to const char* , but then I am not able to exchange or swap values and it was causing error. Any other solution??

Below is the code

#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;
}
Aman
  • 696
  • 1
  • 8
  • 26
Sabiqa Rani
  • 51
  • 10
  • 2
    Change it to `const char*`. – songyuanyao Jan 16 '18 at 00:42
  • I have already tried doing it, but then I am not able to exchange or swap values and it was causing error Any other solution? – Sabiqa Rani Jan 16 '18 at 00:43
  • 3
    String literals can't be modified; that's the point of "deprecated conversion from string constant to `char*`". – songyuanyao Jan 16 '18 at 00:49
  • 1
    Don't change the member (or if you do, use real C++ strings: [std::string](http://en.cppreference.com/w/cpp/string/basic_string)), just change parameters that are not modified in the function/constructor. I suggest you also learn about [virtual](http://en.cppreference.com/w/cpp/language/virtual) – O'Neil Jan 16 '18 at 01:01
  • Unrelated: consider making `~item()` `virtual`. [Should save you some trouble later.](https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors) – user4581301 Jan 16 '18 at 01:20
  • A literal like `"Anything"` is constant; you are not supposed to change any of the characters, much like you are not supposed to change the meaning of `1` or `2`. You can ignore the warnings and the code may seem to "work", but you are really just lucky. In fact, that's why the compiler warns you in the first place. Fix your broken code by getting rid of all those pointers and use `std::string`. – Christian Hackl Jan 16 '18 at 08:58

0 Answers0