Answer the question
In order to leave comments, you need to log in
How to pass an array of objects to a class method, assign each element a value of your choice and return, for example, the sum of its elements?
Hello, I would like to get an answer, at least for the first part of the question
. Here is an example code:
#include <iostream>
#include <string>
using namespace std;
const int n=2;
class xxx{
private:
int y;
public:
void sum(){
cout<<"Enter y: "; cin>>y;
switch(y){
case 1:{
break;
}
case 2:{
break;
}
}
}
};
int main(){
xxx x[n];
for(int i=0;i<n;i++){
x[n].sum();
}
}
Answer the question
In order to leave comments, you need to log in
#include <iostream>
#include <numeric>
constexpr int n = 2;
struct Class {
static int summ (int (&arr)[n] ) {
return std::accumulate(std::begin(arr), std::end(arr), 0);
}
};
int main() {
int x[n] = {4,6};
std::cout << "Summ " << Class::summ(x) << std::endl;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question