I have an odd question :
class Square{
private:
double side_A;
double side_B;
double result;
public:
Square():side_A(0),side_B(0){result=(pow(side_A,2));}; // constructor
Square(double x, double y){side_A=x;side_B=y;result=(x*y);};//Constructor
==================================
Would it be standard practice to put the computation right in the constructor?
That way, I wouldn't need the function unless i did not want it automatically
computed on creation of the obj. Am I right?
I'm not sure if this is something I should even be doing or just stick to using functions for that kind of stuff and just let constructors initialize the class variables.
Because the one with no args will be for the square, the one with two args is for rectangle. It just didn't seem like I should create a whole separate class for something I could do by overloading. That is the only reason I did it that way.
An object should always be in coherent, predictable state.
For example, your in class there is probably invariant: side_A * side_B == result
Every method that would change side_A or side_B must update the result too.
Since the result is a member, the constructors must set it right, preferably by initialization.
If the constructor does not compute the result, and something can read the result before a function is called to update the result, then the reader receives garbage.