编写购物车结算程序(C++)

问题描述

#include<iostream>
#include<string>
using namespace std;

class Commodity
{
public:
	Commodity(string name, double price, double num)
	{
		this->name = name;
		this->price = price;
		this->num = num;
	}
	Commodity()
	{
		name = "";
		price = 0.0;
		num = 0.0;
	}
	Commodity(Commodity&a)
	{
		this->name = a.name;
		this->price = a.price;
		this->num = a.num;
	}

	double getprice()
	{
		return price;
	}
	void printInfo()
	{

		cout << name << "," << price << "," << num << endl;
	}

private:
	string name;
	double price;
	double num;

};
class Cart
{
public:
	int i = 0;

	Cart() {}

	void addItem(Commodity & a)
	{
		item[i] = a;
		i++;
	}
	void checkout()
	{
		double sum = 0;
		for (int i = 0; i<5; i++)
		{
			sum += item[i].getprice();
		}
		cout << "您需要支付" << sum << "元." << endl;
	}
	void printInvoice()
	{
		for (int i = 0; i<5; i++)
		{
			item[i].printInfo();
		}

	}
private:
	Commodity item[20];
};
int main()
{
	Commodity tShirt("T shirt", 79, 2);
	Commodity suit("suit", 1099, 1);
	Commodity hat("hat", 129, 3);
	Commodity tv("tv set", 4899, 1);
	Commodity ac("air condition", 5280, 1);

	Cart myCart;
	myCart.addItem(tShirt);
	myCart.addItem(suit);
	myCart.addItem(hat);
	myCart.addItem(tv);
	myCart.addItem(ac);

	myCart.checkout();
	myCart.printInvoice();
	system("pause");
	return 0;

}

顺带一提,我之前都用codeblocks编的,然后这个程序用codeblock编结果不对,然后用vs编译结果对了。之后有空再去找一下原因吧。我可能要转移到vs编cpp了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值