#include <iostream>
#include <vector>
using namespace std;
class IntegerValue {
public:
using value_t = int;
IntegerValue() : value(0) {}
IntegerValue(const value_t value) : value(value) {}
IntegerValue& operator+=(const IntegerValue& iv) {
this->value += iv.value;
return *this;
}
private:
value_t value;
friend istream& operator>>(istream& inp, IntegerValue& iv) {
return inp >> iv.value;
}
friend ostream& operator<<(ostream& out, const IntegerValue& iv) {
return out << iv.value;
}
friend IntegerValue operator-(const IntegerValue& iv, const value_t value) {
return IntegerValue(iv.value - value);
}
};
class IntegerCollection {
public:
IntegerCollection(const size_t count) {
values.resize(count);
}
IntegerValue sum(const IntegerValue::value_t discount = 0) const {
IntegerValue result = 0;
for (const auto& value : values) result += value - discount;
return result;
}
private:
vector<IntegerValue> values;
friend istream& operator>>(istream& inp, IntegerCollection& ic) {
for (auto& value : ic.values) inp >> value;
return inp;
}
};
int main() {
cout << "count: ";
size_t count;
cin >> count;
cout << "array: ";
IntegerCollection ic(count);
cin >> ic;
const auto sum = ic.sum(1);
cout << "sum: " << sum << '\n';
}
например:
int r;
Myclass arr[10];
в цикле до 10:
r = r + arr[i].num() - 1; (num-геттер поля класса типа int)