独立完成的第一个c++面向对象程序(虽然很简单 以后会增加功能)

一个简单的商品展示程序

功能如下:
1.输出一张商品表(嘻嘻 就这一个功能)
代码如下:
#include<iostream>
#include<string>
using namespace std;
class Goods {
private:
int amount;
double price;
string name;
public:
Goods();
int AddAmount(int);
int LoseAmount(int);
int SetGoods(int,double,string);
int GetPrice();
int GetSumPrice();
int GetName();
int GetAmount();
~Goods();
};
 
Goods::Goods()
{
}
 
int Goods::SetGoods(int amount, double price, string name)
{
this->amount = amount;
this->price = price;
this->name = name;
return 0;
}
 
int Goods::GetPrice()
{
cout << price;
return 0;
}
 
int Goods::GetSumPrice()
{
cout << price*amount;
return 0;
}
 
int Goods::GetName()
{
cout << name;
return 0;
}
 
int Goods::GetAmount()
{
cout << amount;
return 0;
}
 
Goods::~Goods()
{
}
 
int main() {
int i, n, amount;
double price;
string name;
Goods S[200];
cout << "请输入商品种类:" << endl;
cin >> n ;
cout << "请依次输入数量,单价,商品名" << endl;
for (i = 0; i < n; i++) {
cin >> amount >> price >> name;
S[i].SetGoods(amount,price,name);
}
cout << "数量" << '\t' << "单价" << '\t' << "商品名" << '\t' << "总价" << endl;
for (i = 0; i < n; i++) {
S[i].GetAmount(); cout << '\t';
S[i].GetPrice(); cout << '\t';
S[i].GetName(); cout << '\t';
S[i].GetSumPrice(); cout << '\t';
cout << endl;
}
return 0;
}

转载于:https://www.cnblogs.com/lnzhangsong/p/5086690.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值