C++ 编程题。。

2025-01-04 05:40:37
推荐回答(2个)
回答1:

//以面向对象的方法设计一个类,此类包含3个私有数据:unlead,le
//ad(无铅汽油和有铅汽油)以及total(当天总收入,无铅汽油的
//价格是17元/公升,有铅汽油的价格是16元/公升),请以构造函数方式建立此值。试输入
//某天所加的汽油量,本程序将列出加油站当天的总收入。
#include
using namespace std;
const int unleadPrice = 15, leadPrice = 17;//
class Station
{
private:
int unlead;
int lead;
double total;

public:
Station(int unleadCount,int leadCount)
{
unlead = unleadCount; lead = leadCount;//初始化无铅油和有铅油的数量
total = 0;//如果为下一行代码加作准备。呵呵。
total += (unlead * unleadPrice + lead * leadPrice);//初始化总收入
}
~Station(){}
double getTotal()
{
return total;
}
};

void main()
{
int unleadCount, leadCount;
cout<<"请输入无铅油和有铅油的数量:"< cin>>unleadCount >>leadCount;
Station S(unleadCount, leadCount);
cout<<"总收入为:"<< S.getTotal();
}

回答2:

#include
using namespace std;

class Gasoil
{
public:
Gasoil(void) { unlead = 17; lead = 16; }
void compute(int ucnt, int cnt) { total = unlead*ucnt + lead*cnt; cout<<"总收入:"<private:
int unlead;
int lead;
int total;
};

void main(void)
{
Gasoil gas;
int unlead_cnt, lead_cnt;
cout<<"输入加油量(无铅汽油和有铅汽油):";
cin>>unlead_cnt>>lead_cnt;

gas.compute(unlead_cnt, lead_cnt);
}