D.类的继承与派生 | |||||
| |||||
Description | |||||
某工厂需要打造某种球形零件,在尝试的过程中使用了不同的金属,要求根据产品的尺寸信息和所用金属的密度信息以及金属的单位价格计算产品的重量及造价。 | |||||
Input | |||||
本题只有一组测试数据,测试数据由3个浮点型数组成,数据之间用空格分隔。格式如下: 半径 密度 金属价格 注:半径单位为m,密度单位为kg/m3,金属价格单位为元/kg。 | |||||
Output | |||||
输出包含四行,格式如下: Radius:半径 Volume:体积 Weight:重量 Cost:造价 | |||||
Sample Input | |||||
5 1 2 | |||||
Sample Output | |||||
Radius:5 Volume:523.333 Weight:523.333 Cost:1046.67 | |||||
Hint | |||||
class Sphere{ private: double radius; public: Sphere(double r); double getArea(); double getVol(); void show(); }; int main() { double radius,density,price; cin>>radius>>density>>price; Product product(radius,density,price); product.show(); } |
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Sphere {
private:
double radius;
public:
Sphere() {};
Sphere(double r)
{
radius = r;
}
double getR() { return radius; };
double getVol()
{
double a = 1.33 * 3.14 * pow(radius, 3);
return a;
}
};
class Product :public Sphere
{
public:
double radius,density, price;
Product(double r,double d,double p)
{
radius=r, density = d, price = p;
}
void show()
{
cout << "Radius:" <<radius << endl;
cout << "Volume:" << 4 * 3.14 * pow(radius, 3)/3 << endl;
cout << "Weight:" << 4 * 3.14 * pow(radius, 3)/3 * density << endl;
cout << "Cost:" << 4 * 3.14 * pow(radius, 3)/3 * density * price << endl;
}
};
int main()
{
double radius, density, price;
cin >> radius >> density >> price;
Product product(radius, density, price);
product.show();
}