| 1 |
#include <iostream> |
| 2 |
#include <string> |
| 3 |
|
| 4 |
using namespace std; |
| 5 |
|
| 6 |
class Fibo |
| 7 |
{ |
| 8 |
size_t d_return = 0; |
| 9 |
size_t d_next = 1; |
| 10 |
|
| 11 |
public: |
| 12 |
size_t next(); |
| 13 |
}; |
| 14 |
|
| 15 |
size_t Fibo::next() |
| 16 |
{ |
| 17 |
size_t ret = d_return; // the next fibonacci number |
| 18 |
|
| 19 |
d_return = d_next; // at the next call: return d_next; |
| 20 |
d_next += ret; // prepare d_next as the sum of the |
| 21 |
// original d_return and d_next |
| 22 |
return ret; |
| 23 |
} |
| 24 |
|
| 25 |
int main(int argc, char **argv) |
| 26 |
{ |
| 27 |
Fibo fibo; // create a Fibo object |
| 28 |
|
| 29 |
size_t sum = 0; |
| 30 |
|
| 31 |
// use its 'next' member to obtain |
| 32 |
for ( // the sequence of fibonacci numbers |
| 33 |
size_t begin = 0, end = argc == 1 ? 10 : stoul(argv[1]); |
| 34 |
begin != end; |
| 35 |
++begin |
| 36 |
) |
| 37 |
sum += fibo.next(); |
| 38 |
|
| 39 |
cout << sum << '\n'; |
| 40 |
} |