编写购物车结算程序(C++)
Posted 7TribeZ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编写购物车结算程序(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了。
以上是关于编写购物车结算程序(C++)的主要内容,如果未能解决你的问题,请参考以下文章