Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
New ways to Assign values to Variables in C++ 17 ?
In C++ 17, there are introduced two new ways by which a programmer can assign values to a variable or declared them. In this update, elser then the classical way of assigning values to a variable the following two ways to initialise values.
Classical method
Initially the assignment operator ‘=’ is used for assignment and declaration of a variable. assignment of a variable using = looks like,
datatype variable_name = value;
Example,
int val = 243;
New method
Uniform initialisation
In uniform initialisation of variables we do not use the ‘=’ operator. the value is enclosed inside a pair of curly braces ' {} '. Value inside the pair of curly braces is passed to the variable.
Syntax
data_type variable_name{ value};
Example
#include <iostream>
using namespace std;
int main() {
cout<<"Declaring Values using uniform initialization
";
int val1{ 367 };
cout << "val1 = " <<val1<<endl;
int val2 = { 897 };
cout << "val2 = " << val2<<endl;
return 0;
}
Output
Declaring Values using uniform initialization val1 = 367 val2 = 897
Constructor initialisation
another method to assign values to a variable. in constructor initialisation of variables, we use a pair of parentheses instead of the = operator. the value of variable is enclosed inside a pair of parentheses ( ).
Syntax
data_type variable_name(values);
Example
#include <iostream>
using namespace std;
int main() {
cout<<"Declaring Values using constructor initialization
";
int val1( 367 );
cout << "val1 = " <<val1<<endl;
int val2 = ( 897 );
cout << "val2 = " << val2<<endl;
return 0;
}
Output
Declaring Values using constructor initialization val1 = 367 val2 = 897
