求c++大神帮忙,新手表示实在不会
设计一个仓库类(1)私有成员为,仓库名称,仓库中物品(名称,数量,单价),采用指针存储动态数组方式存储物品信息。
(2)重载“+”操作,表示两个仓库合并,仓库名称由两个仓库名称连接到一起,其中新仓库为为两个仓库的所有物品,且同一种物品能够合并。
(3)重载[]操作以获得第i个物品记录。
(4)该类的对象不能拷贝构造及赋值
#ifndef __WAREHOUSE_H__ #define __WAREHOUSE_H__ #include <iostream> #include <string> struct Goods { std::string name; unsigned int nums; double price; Goods(void) { name = "default_goods"; nums = 11; price = 17.5; } friend bool operator==(const Goods& lhs, const Goods& rhs) { return lhs.name == rhs.name && lhs.nums == rhs.nums && lhs.price == rhs.price; } friend bool operator!=(const Goods& lhs, const Goods& rhs) { return !(lhs == rhs); } }; class WareHouse { public: WareHouse(const std::string& name, const unsigned int& size); ~WareHouse(void); private: WareHouse(const WareHouse&); WareHouse& operator=(const WareHouse&); public: friend WareHouse* operator+(const WareHouse& lhs, const WareHouse& rhs); static void destory(WareHouse const* p); Goods* operator[](const unsigned int& idx); private: std::string name; unsigned int cnt; Goods** goodsLst; }; #endif // ifndef __WAREHOUSE_H__
#include "WareHouse.h" WareHouse::WareHouse( const std::string& name, const unsigned int& size ) { goodsLst = new Goods*(); for (unsigned int i = 0; i < size; ++i) { goodsLst[i] = new Goods(); } cnt = size; this->name = name; } WareHouse::~WareHouse( void ) { } void WareHouse::destory( WareHouse const* p ) { if (NULL == p) { return; } if (NULL == p->goodsLst) { return; } for (unsigned int i = 0; i < p->cnt; ++i) { if (NULL != p->goodsLst[i]) { delete p->goodsLst[i]; p->goodsLst[i] = NULL; } } delete p->goodsLst; } Goods* WareHouse::operator[]( const unsigned int& idx ) { return idx > cnt ? NULL : goodsLst[idx]; } WareHouse* operator+( const WareHouse& lhs, const WareHouse& rhs ) { std::string name("default"); unsigned int idx = 0; WareHouse* p = new WareHouse(name, ( + )); p->name = lhs.name + rhs.name; for (unsigned int i = 0; i < ++i) { memcpy(p->goodsLst[idx++], lhs.goodsLst[i], sizeof(Goods)); } for (unsigned int i = 0; i < ) { unsigned int j = 0; for (; j < idx; ++j) { if (*(p->goodsLst[j]) == *(rhs.goodsLst[i])) { break; } } if (j == idx) { memcpy(p->goodsLst[idx++], rhs.goodsLst[i], sizeof(Goods)); } else { --p->cnt; } } return p; }
#include "WareHouse.h" int main() { WareHouse warehouse1("WareHouse1", 1); WareHouse warehouse2("WareHouse2", 1); Goods* p = warehouse1[0]; Goods* q = warehouse2[0]; WareHouse* k = (warehouse1 + warehouse2); WareHouse::destory(k); return 0; }