I believe it depends on the compiler (correct me if I'm wrong). initialization or zeroing out of memory takes time. If they choose not to add that overhead when developing the compiler, it will be ultimately faster. Programmers should ALWAYS initialize their own variables.

"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

In general, you cannot assume that a variable will be initialized to a specific value unless you do it yourself. There are some instances where the rules of the language stipulate that the value be zero-initialized, but member variables are not one of them.

BTW, you don't necessarily need a default constructor and an overloaded constructor here. For this particular class your instructor might be saying that the requirements dictate that both constructors are necessary. Nothing in the language or the information you provided so far indicates that this is true, though.

Now my question that I missed out on asking during the lecture was, why exactly do we need a default constructor to initialize?I thought that it is not really needed because compiling and running a program will set aside "clean memory" for that process.

It's not only about initializing your member variables. If you define any constructor for your class, then the compiler won't supply a default constructor. That means you cannot create objects like this:

Code:

MyClassName obj;

If you try that, you will get an error that essentially says, "you are trying to call a constructor that takes no arguments, but there isn't one."

Then, the reason you don't just define the defalut constructor like this:

Code:

MyClassName(){}

is to make sure you don't have junk values in the memory set aside for the member variables.

I thought that it is not really needed because compiling and running a program will set aside "clean memory" for that process.

I've never heard anyone say that, I've never seen anyone post that, and I've never read that anywhere. Although, in Java the member variables are initialized to 0, \u000, or null.

It's kind of analogous to the right and left associativity rules and operator precedence. You can certainly memorize them and set up your expressions in accordance with those rules, or you can can concentrate on learning more important things and just use parentheses to force the precedence you want, which also makes it absolutely clear what is happening to anyone reading the code.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens