我需要帮助(写和读函数)c ++

时间:2015-04-13 23:35:36

标签: c++ function class

该计划正在以这种方式运作。但我需要改变价格体系。我需要使用priceRead和priceWrite。

priceRead (): double 
priceWrite (in _price: double): void 

priceRead:price是读取属性的函数。 priceWrite(double):price属性是写作的函数。

抱歉英语不好。 :(

#include <iostream>
#include <string.h>

using namespace std;

class shoes{
    protected:  
              int size;
              string colour;
              char sex;
              string brand; 
    public: shoes(int _size, string _colour, char _sex, string _brand)
                    :size(_size),colour(_colour), sex(_sex), brand(_brand)
                    {cout << "shoe configurator" << endl;} 
            ~shoes(){ }
            void shoesInf();

};

class storage{
    protected:
              int number;
              int maxsize;
              int minsize;
    public: storage(int _number, int _maxsize, int _minsize)
                    :number(_number),maxsize(_maxsize),minsize(_minsize)
                    {cout << "store configurator" << endl;} 
            ~storage(){ } 
            void storageInf();

};

void shoes::shoesInf()
{

    cout << "Size :" << size << endl;
    cout << "Colour :" << colour << endl;
    cout << "Sex :" << ((sex=='M')?"Man":"Woman") << endl;
    cout << "Brand:" << brand << endl;
}

void storage::storageInf()
{
    cout << "Number :" << number << endl;
    cout << "Max size :" << maxsize << endl;
    cout << "Min size :" << minsize << endl;
}   

class Store: public storage{
    private:  int number;
              int maxsize;
              int minsize;
    public: Store(int _number, int _maxsize, int _minsize)
                  :storage(_number,_maxsize,_minsize)
                {cout << "storage INF" << endl;}    


};

class Nike: public shoes{
    private:  double price;
    public: Nike(int _size, string _colour, char _sex, string _brand,
                 double _price):shoes(_size,_colour,_sex,_brand),price(_price)
                {cout << "Shoes INF" << endl;}  

            void shoesInf()
            {
                ((shoes*)this)->shoesInf();
                cout << "Price :" << price << endl;
            }


};

class Lacoste: public shoes{
    private:  double price;
    public: Lacoste(int _size, string _colour, char _sex, string _brand,
                 double _price):shoes(_size,_colour,_sex,_brand),price(_price)
                {cout << "Shoes INF" << endl;}  



            void shoesInf()
            {
                ((shoes*)this)->shoesInf();
                cout << "Price:" << price<< endl;
            }


};

int main(int argc, char** argv) {
    Store*sh=new Store(1,50,15);
    sh->storageInf();
    Nike *sh2=new Nike(38,"Blue",'F',"Nike",100);
    sh2->shoesInf();
    Lacoste *sh3=new Lacoste(40,"Yellow",'M',"Lacoste",350);
    sh3->shoesInf();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

class Shoes
{
private:
    int size;
    string colour;
    char sex;
    string brand;
    double price;
public:
    Shoes(int _size, string _colour, char _sex, string _brand, double _price) : 
    size(_size), colour(_colour), sex(_sex), brand(_brand), price(_price)
    {}

    void writePrice(double newPrice)
    {
        price = newPrice;
    }

    void readPrice()
    {
        cout << price << endl;
    }
};

注意:

  • 所有的鞋子都有价格,为什么不把它放在基地?
  • 品牌由品牌字符串定义,所有鞋子都相同,因此无需为每个品牌推出新类别。
  • 不需要空的析构函数。
  • 此外,如果在导出时没有重新定义price,则无需在每次从shoeInf派生时重新定义Shoe
  • 还有一件事:用CapitalLetters启动类名(以那种方式!)。