| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1785 人关注过本帖
标题:求c++大神帮忙,新手表示实在不会
只看楼主 加入收藏
littlecc
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2016-12-27
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
求c++大神帮忙,新手表示实在不会
设计一个仓库类
(1)私有成员为,仓库名称,仓库中物品(名称,数量,单价),采用指针存储动态数组方式存储物品信息。
(2)重载“+”操作,表示两个仓库合并,仓库名称由两个仓库名称连接到一起,其中新仓库为为两个仓库的所有物品,且同一种物品能够合并。
(3)重载[]操作以获得第i个物品记录。
(4)该类的对象不能拷贝构造及赋值
搜索更多相关主题的帖子: 仓库 动态 记录 信息 
2016-12-27 19:15
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:20 
WareHouse.h
程序代码:
#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__


WareHouse.cpp
程序代码:
#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;
}


main.cpp
程序代码:
#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;
}

~~~~~~我的明天我知道~~~。
2016-12-27 21:31
快速回复:求c++大神帮忙,新手表示实在不会
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039878 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved