The following is a C++ code for the constructor of linear_program class I have created. The problem is that I have a feeling that, by design, I should overload the >> operator for this class instead of just using >> it in constructor for the class. But then I have to allocate memory dynamically which depends on input taken so I canno segregate the logic completely and even if I overload the operator then I will not be able to take all input at once. That is why I do not see a benefit in overloading >> in this case.
linear_program::linear_program() {
cin >> dim >> no_constr;
lp = new plane[no_constr];
double *temp = new double [dim];
double constant;
for (int i = 0; i < no_constr; ++i) {
for (int j = 0; j < dim;++j) {
cin >> temp[j];
}
cin >> constant;
lp[i].set_plane(temp, constant, dim);
}
for (int i = 0; i < no_constr; ++i) {
cin >> cost[i];
}
}
Is this acceptable by design standards. I would also like to know if there are other healthy alternatives for such cases.
Depends on what you mean by ‘fine’. But I would suggest to keep object initializations in a constructor, and move the business logic(that does not correspond to creation of that object) to another function.
A constructor should initialize the object, nothing more.