I am reading "Accelerated C++" by Andrew Koenig and Barbara E. Moo, and I'm at the chapter about constructors (5.1).
They mention here that
We said that constructors exist to ensure that objects are created with their data members in a sensible state. In general, this design goal means that every constructor should initialize every data member. The need to give members a value is especially critical for members of built-in type. ...
Although we explicityly initialized only
midterm
andfinal
, the other data members are initialized implicitly. Specifically,n
is initialized by thestring
default constructor, andhomework
is initialized by thevector
default constructor.
The class they are talking about is
class Student_info {
public:
std::string name() const (return n;}
bool valid() const {return !homework.empty();}
std::istream& read(std::istream&);
double grade() const;
private:
std::string n;
double midterm, final;
std::vector<double> homework;
};
and their default constructor is
Student_info::Student_info(): midterm(0), final(0) {}
I would just like to clarify that this means that things like int
and double
where there isn't a std::
before the term will need to be initialized specifically?