#include using namespace std; class Sum { double total; public: Sum(double init = 0.0) : total(init) {} double operator() (void) const { return total; } double operator() (double x) { return total += x; } void reset(double init = 0.0) { total = init; return; } }; class Avg { Sum s; long n; public: Avg(void) : s(), n(0) {} double operator()(void) const { return s()/(n==0?1:n); } double operator()(double x) { ++n; return s(x)/(n==0?1:n); } void reset(void) { s.reset(); n = 0; return; } void reset(double sum, long count) { s.reset(sum); n = count; return; } }; int main(void) { Sum s; Avg a; long d[10] = { 34, 56, 23, 76, 5, 12, 4, 48, 65, 30 }; double e[10] = { 3.4, 5.6, 2.3, 7.6, 5, 1.2, 4, 4.8, 6.5, 3.0 }; cout << "First data: "; for (short i = 0; i != 9; i++) { cout << d[i] << '+'; s(d[i]); a(d[i]); } s(d[9]); a(d[9]); cout << d[9] << " = " << s() << endl; cout << "Average is: " << a() << endl; cout << "Second data: "; for (short i = 0; i != 9; i++) { cout << e[i] << ' '; } cout << e[9] << endl; s.reset(); cout << "Second data total: "; for (short i = 0; i != 10; i++) { s(e[i]); } cout << s() << endl; a.reset(s(),10); cout << "Second data average: " << a() << endl; return 0; }