If you have been programming for a long time, you probably know that there are many ways to initialize variables. And while some of these methods are better than others, they all have their advantages and disadvantages. But when you want to be able to initialize variables in a standard manner, there is no better method than to use the default constructor.
So let’s look at how you would like to be able to initialize all variables in a standard manner.
Default Constructor
C++ provides a default constructor for each class. This means that whenever you declare a new variable of a certain type, the compiler automatically creates a default constructor for that variable.
So here is how you would like to be able to initialize all variables in a standard manner C++ Programming:
Initialize all variables The C++ Most Vexing Parse Problem
class MyClass
{
private:
int m_Member;
public:
MyClass() = default;
MyClass(const MyClass& rhs) = default;
};
int main()
{
MyClass objectA;
MyClass objectB(MyClass());
return 0;
Example – 2:
#include
using namespace std;
class Student
{
public:
Student()
{
cout << "I am a student" << endl;
}
};
int main()
{
Student s1; // this line will create a default constructor
Student s2 = Student(); // this line will create a default constructor
}
Using Uniform Initialization to Solve the Vexing Parse Problem
class MyClass
{
private:
int m_Member;
public:
MyClass() = default;
MyClass(const MyClass& rhs) = default;
};
int main()
{
MyClass objectA;
MyClass objectB{MyClass{}};
return 0;
}
Using Uniform Initialization to Prevent Narrowing Conversions
int main()
{
int number{ 0 };
char another{ 512 };
double bigNumber{ 1.0 };
float littleNumber{ bigNumber };
return 0;
}
Using a static_cast to Compile Narrowing Conversions
int main()
{
int number{ 0 };
char another{ static_cast(512) };
double bigNumber{ 1.0 };
float littleNumber{ static_cast(bigNumber) };
return 0;
}
If you are working on a large project in C++ and you want to initialize all the variables in a standard way, then you don’t need to use a separate function. Just put all the variables in a class and initialize them in constructor. The reason behind this is that in a standard project, it is a common practice to use a class in which all the variables are initialized to 0.
There are certain reasons why this practice is followed in a standard project. First of all, you can’t initialize all the variables in a class in a single function. Secondly, you don’t have the knowledge that what will be the value of the variable if it is initialized to 0. So, if you want to initialize all the variables in a standard way, then you don’t need to use a separate function.
Here is a sample program that illustrates the idea of a standard initialization.
#include
using namespace std;
class Test {
private:
int x;
int y;
public:
Test() {
x = 0;
y = 0;
}
};
int main() {
Test t;
cout << t.x <<'' << t.y <<'' << endl;
return 0;
}
Conclusion:
I hope you liked this post about “How to Initialize All Variables in a Standard Manner”. I have shared a few simple examples with you that illustrates the idea of a standard initialization.