using cplusplus amake a accumulator
时间: 2024-01-02 17:04:41 浏览: 64
C++ language tutorial
Sure, I can help you create an accumulator in C++. Here is an example code:
```
#include <iostream>
class Accumulator {
private:
int count;
public:
Accumulator() {
count = 0;
}
void add(int value) {
count += value;
}
int getTotal() {
return count;
}
};
int main() {
Accumulator acc;
acc.add(5);
acc.add(10);
std::cout << "Total count: " << acc.getTotal() << std::endl;
return 0;
}
```
In this code, we have defined a class named `Accumulator` which has functions `add` and `getTotal`. The function `add` takes an integer value and adds it to the count, while the function `getTotal` returns the current count of the accumulator.
In the `main` function, we have created an instance of the `Accumulator` class and added the values 5 and 10 to it. Finally, we print the total count using the `getTotal` function.
阅读全文