私は、このコードを持って、クラスシリンダーにラインC++のコンストラクタと派生オブジェクト
Cylinder(double r, double h) : base (r), height(h) {}
が
Cylinder(double r, double h) : base.radius (r), height(h) {}
ない理由を私はダブルが引き渡されているとベースがシリンダーであることを意味するだろうbase.radiusではありませんか?
// member initialization
#include <iostream>
using namespace std;
class Circle {
double radius;
public:
Circle(double r) : radius(r) { }
double area() {return radius*radius*3.14159265;}
};
class Cylinder {
Circle base;
double height;
public:
Cylinder(double r, double h) : base (r), height(h) {}
double volume() {return base.area() * height;}
};
int main() {
Cylinder foo (10,20);
cout << "foo's volume: " << foo.volume() << '\n';
return 0;
}
継承または派生オブジェクトはありませんか? –
実際には、基本クラスの(唯一の)コンストラクタを呼び出す必要があるからです! – 7raiden7
何とか 'base'という名前に混乱していますか? –