码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-12-29 19:17:13      阅读:133      评论:0      收藏: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;
}

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

标签:

原文地址:http://www.cnblogs.com/lnzhangsong/p/5086690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!