#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;
class myClass {
int d;
public:
myClass(){};
int get_rand() const {
return rand() % 10;
}
};
int operator +(myClass d, myClass e) {
return d.get_rand() + e.get_rand();
}
int main() {
myClass mC;
int sum = mC + mC;
cout << sum;
}
I want to be able to had as many Mc’s as I want eg. int sum = mC + mC + mC + mC;
You need an overloaded operator for this, namely
int operator+(int, myClass):+operations are evaluated left-to-right, so the type ofmC + mCisint. This is why you need another operatorint operator +(int, myClass).