The case must be used to initialize the member initialization list

// 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;
}

  members shall be initialized in the member initialization list, this time, you must use the member initialization list to initialize because the class is nested in another class.

foo's volume: 6283.19

Guess you like

Origin www.cnblogs.com/CodeWorkerLiMing/p/11074579.html