代码改变世界

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

2015-12-29 18:39  迷你天才  阅读(169)  评论(0编辑  收藏  举报

一个简单的商品展示程序

功能如下:
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;
}