Showing posts with label initialization. Show all posts
Showing posts with label initialization. Show all posts

Thursday, June 28, 2018

Can you initialize multiple variables in one statement?

Yes. You can initialize multiple variables in the same statement. Refer to the previous post and try out the following in Visual Studio Community 2017 (free) :
        int ix = 5, iy = 6;
  cout << ix << "," << iy;
cout << "\n";

This is also possible:

 int iz(7), izz{ 8 };
 cout << iz << "," << izz;
 cout << "\n";

This one as well possible:

 int a = 100, b(50), c{ 100 };
 cout << "a= " << a << " b= " << b << " c= "<< c;
 cout << "\n";

The result of running this is shown here:


Here is a copy of the code.


Wednesday, June 27, 2018

How do you initialize a variable in C++?

Let us say we are trying to initilaize a variable of type integer, iX. There are three ways you can initialize a variable.

We use the following statement to initialize:

int iX = 100;  This is the syntax for copy initialization.

This is called copy initialization saying that intX's value is 5.

In direct initiization you use the statement:

int iX(100);  This is the syntax for direct initialization but should not be confused with a function.

There is yet another kind of initialization stated this way:

This is called Uniform initizlization used most of the data types.
int  ix{100};

Review the following initialize.cpp file.

The file was created using a Blank C++ project in Visual Studio Community 2017 Version 15.5.7