鱼C论坛

 找回密码
 立即注册
查看: 2442|回复: 2

难题求帮助2

[复制链接]
发表于 2014-6-23 16:53:03 | 显示全部楼层 |阅读模式
6鱼币
编写一个程序以实现以下功能:
(1)定义一个商品类Goods,包含数据成员:商品名称Name,商品价格Price,商品数量amount等;
包含如下主要成员函数:
>构造函数(用来初始化一个商品对象);
>销售商品Sale(商品余量不足时给予提示,若购买数量合法则显示应付款);
>商品上架Add;
>显示商品形象ShowMe。
(2)重载输出操作符<<实现Goods类对象的输出。
(3)编写main函数,测试以上所要求的各种功能。

要求C++实现。
急用,在线等!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-6-23 16:53:04 | 显示全部楼层
  1. #include <string>
  2. #include <iostream>
  3. using namespace std;

  4. class Goods {
  5. public:
  6.         Goods( string name, double price, int amount )
  7.                 :Name(name),Price(price),Amount(amount){}
  8.         void Sale( int number );
  9.         void Add( int number ) { Amount += number; }
  10.         ostream& ShowMe( ostream& out ) const;
  11. private:
  12.         string Name;
  13.         double Price;
  14.         int Amount;
  15. };
  16. void Goods::Sale( int number ) {
  17.         if( number>Amount ) {
  18.                 cout<<"仅有"<<Amount<<"件商品"<<endl;
  19.                 return;
  20.         }
  21.         Amount -= number;
  22.         cout<<"应付钱:"<<number*Price<<endl;
  23. }

  24. ostream& Goods::ShowMe( ostream& out ) const{
  25.         out<<"商品名称:"<<Name<<"\t单价:"<<Price<<"库存:"<<Amount;
  26.         return out;
  27. }

  28. ostream& operator<<( ostream& out, const Goods& goods ) {
  29.         return goods.ShowMe(out);
  30. }

  31. int main() {
  32.         Goods goods("程序作业", 20.5, 3);
  33.         cout<<goods<<endl;
  34.         goods.Sale(100);
  35.         goods.Sale(2);
  36.         cout<<goods<<endl;
  37.         goods.Add(10);
  38.         cout<<goods<<endl;
  39. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-7-27 15:26:53 | 显示全部楼层
#include <string>
#include <iostream>
using namespace std;

class Goods {
public:
        Goods( string name, double price, int amount )
                :Name(name),Price(price),Amount(amount){}
        void Sale( int number );
        void Add( int number ) { Amount += number; }
        ostream& ShowMe( ostream& out ) const;
private:
        string Name;
        double Price;
        int Amount;
};
void Goods::Sale( int number ) {
        if( number>Amount ) {
                cout<<"仅有"<<Amount<<"件商品"<<endl;
                return;
        }
        Amount -= number;
        cout<<"应付钱:"<<number*Price<<endl;
}

ostream& Goods::ShowMe( ostream& out ) const{
        out<<"商品名称:"<<Name<<"\t单价:"<<Price<<"库存:"<<Amount;
        return out;
}

ostream& operator<<( ostream& out, const Goods& goods ) {
        return goods.ShowMe(out);
}

int main() {
        Goods goods("程序作业", 20.5, 3);
        cout<<goods<<endl;
        goods.Sale(100);
        goods.Sale(2);
        cout<<goods<<endl;
        goods.Add(10);
        cout<<goods<<endl;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 15:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表