I'm currently reading "Accelerated C++" and I have just read the chapter on defining types. I basically understood what constructors are used for and why they are important, I did not, however, fully understand how constructors are supposed to be implemented in a class declaration.
There are two types of constructors, default constructors and constructors that include arguments. Now the authors wrote that you should always define both types of constructors, e.g. like this:
1 2 3 4 5 6
|
class Student_info {
public:
Student_info()
Student_info(std::istream&);
// as before
};
|
First of all, is there a reason why you do not have to put a semicolon behind a statement that defines a default constructor?
Then they go on to give a more specific example of a default constructor, where the two elements of the class that are of build-in type are to be given the value 0:
Student_info::Student_info(): midterm(O), final(0) { }
and then an example for a constructor with an argument:
Student_info::Student_info(istream& is) { read(is); }
Now a couple of questions:
My compiler(XCode) does not accept this kind of statement:
Student_info::Student_info(istream& is) { read(is); }
instead it only accepts the constructor definition in this way:
Student_info(istream& is) { read(is); }
I get the error: Expected member name or ";" after decleration specifiers. Why is that?
And most importantly I do not understand why you should define two kinds of constructors. Why would you default initialise a class object first and then later initialise it by reading input from the standard input into it?