All the tutorials I have watched & read on objected oriented programming in C++ tell me to use default constructors in order to initialize all the member variables of a class to their null state. But from a book i'm also learning from, it has an example (which i have also tested and it works) of a class without a default constructor which leads me to question why are default constructors necessary?
#include <iostream>
class A
{
public:
int myValue;
// this class does not have a default constructor!
};
class B
{
public:
int myValue;
B() // the default constructor (default means no-params)
{
myValue = 0;
}
B(int initialValue) // here is an "explicit constructor" example
{
myValue = initialValue;
}
};
int main()
{
A myClassA;
B myClassB;
std::cout << "A's value: " << myClassA.myValue << std::endl;
std::cout << "B's value: " << myClassB.myValue << std::endl;
}
If you were to compile this, and run it; I have no idea what would appear on the first line, but I know for sure that the 2nd line will display the number '0'.
so as you stated:
"use default constructors in order to initialize all the member variables of a class to their null state"
maybe replace the word "null" with default when you read that.