I am having a huge disconnect while trying to figure out constructors. My question is this: Are constructors with initialization lists replacing the set methods of a class? For example:
Class Rock
{
private:
double Weight;
public:
void setWeight(double)
{ Weight = wei; }
double getWeight()
{ return Weight; }
double calcWeight()
{ return Weight *= 2; }
}
Would using a constructor with initialization list that looks something like: Rock::Rock(double): Weight(w) {}
replace using the set method?
Setters and constructors serve completely different purposes, and one never "replaces" the other.
Constructors set up the initial state of instances, and setters allow you to change that state after construction.
That being said, if you find yourself doing something like the following:
Then what you are actually doing is setting the initial state of the rock. In that case, then yes, a constructor taking the weight as a parameter is preferable.
However, notice that this is a distinction that happens at the point where the class is used, not when it is declared.