Jackie开了一家水果店,店里昨日剩余m公斤葡萄(Grape),今天新进了n公斤,葡萄的属性是重量(weight)。
葡萄的每公斤单价是price。请你编写程序计算葡萄的总重量和总价格。
用C++编写Grape类来完成代码,调用格式见“Append Code”。
Grape::weight()葡萄的重量。
Grape::weight(double)修改葡萄的重量。
Grape::value()葡萄的价格。
Grape的构造根据题意设计。
先输入葡萄的单价price,然后分别输入昨天剩余和今天新进的葡萄重量。
输出葡萄的总重量和总价格。
22.5
11
22
33
742.5
int main()
{
double w, price;
cin >> price;
cin >> w;
const Grape grap(price, w);
Grape grape(price);
cin >> w;
grape.weight(w);
cout << grape.weight() + grap.weight() << endl;
cout << grape.value() + grap.value() << endl;
}
#include <iostream>
#include <cstdio>
using namespace std;
class Cherry {
private:
double m;
double n, w;
public:
Cherry(double wei = 0, double amo = 0) : n(wei), m(amo) { w = m * n; }
double weight() {
return w;
}
Cherry& weight(double wei) {
w = wei;
return *this;
}
Cherry& sold_weight(double sw) {
w -= sw;
if(w <= 0) w = 0;
return *this;
}
};
class Grape {
private:
double price, w;
public:
Grape(double p = 0, double ww = 0) : price(p), w(ww) {}
double weight() const { return w; } //注意grap是const类型变量
Grape& weight(double ww) {
w = ww;
return *this;
}
//注意grap是const类型变量
double value() const {
return w * price;
}
};
int main()
{
double w, price;
cin >> price;
cin >> w;
const Grape grap(price, w);
Grape grape(price);
cin >> w;
grape.weight(w);
cout << grape.weight() + grap.weight() << endl;
cout << grape.value() + grap.value() << endl;
}