1 #include <iostream> 2 #include <array> 3 4 class Circle 5 { 6 private: 7 double r = 1.0;//默认值 8 public: 9 Circle() = default;//无参构造函数(默认构造函数) 10 Circle(double r){this->r = r;}//有参构造函数 11 double getArea() 12 { 13 return 3.14*r*r; 14 } 15 }; 16 int main() 17 { 18 double TotalArea = 0.0; 19 //调试,看不到调用默认构造函数,少了默认构造函数还不可以 20 std::array<Circle,10> CircleArray = {1.0,2.0,3.0,4.0,5.0}; 21 22 for ( auto it = CircleArray.begin(); it != CircleArray.end(); ++it ) 23 { 24 TotalArea += (*it).getArea(); 25 } 26 std::cout<<TotalArea<<std::endl; 27 return 0; 28 }
https://zh.cppreference.com/w/cpp/container/array
原文:https://www.cnblogs.com/GoldenEllipsis/p/10940972.html