C++ 编程/作用域/示例/程序平均值1
外观
// Program Average rewritten using a class
#include <iostream>
using namespace std;
class StatisticsPackage{
private:
float aa[20]; /* aa scope start*/
int length; /* length scope start*/
public:
float average(){
float result = 0.0; /* result scope start*/
for(int i = 0; i < length; ++i) /* i scope start*/
result += aa[i];
return result/length;
} /* result and i scope end*/
void get_data(){
length = 0;
while(cin >> aa[length++]);
--length;
}
}; /* aa and length scope end*/
int main(){
StatisticsPackage sp; /* aa and length lifetimes start */
sp.get_data();
float av = sp.average(); /* av scope start*/
cout << av << endl;
return 0;
} /* av scope end*/