Best Way to Write Constructors in C++?

Posted by Ace9750@reddit | learnprogramming | View on Reddit | 32 comments

I'm in my first semester as a computer science major, and I just learned about classes. I think I have a pretty good understanding of them, but I had a question on constructors. My professor exposed me to two different ways to write them, and I wanted to ask which one is better to use in a professional setting.

Note that these constructors correspond to the following class Box:

class Box
{
private:
    int length;
    int width;
    int height;
public:
    Box();
};

The two ways we could write the constructor were

Option 1:

Box::Box()
: length(1), width(1), height(1)
{}

Option 2:

Box::Box()
{
    length = 1;
    width = 1;
    height = 1;
}

Which one would be more acceptable in a professional setting? Is there an even better alternative than these two?