The ISO / ANSI C++ includes several new features to the original C++ specifications. Some new features are added to provide better control in certain situations, while others are included to give convenience to C++ programmers. It is to be noted that writing a complete C++ program is technically possible without using any new features. In this tutorial, you will learn about some of the essential C++ features newly introduced.
What Are the New C++ Features?
Some of the new C++ features are:
- New Data type:
- bool
- wchat_t
- Operator keywords
- New Headers
- Newly introduced operators
- const_cast
- static_cast
- dynamic_cast
- reinterpret_cast
- typeid
- Namespace scope
- Class implementation
- Explicit Constructors
- Mutable members
New ANSI C++ Data Types
Two new data types have been added to the ANSI C++ to enhance the range of data types. These are:
bool
This data type can hold boolean values, i.e., whether true
or false
. The values true
and false
have been added to the set of C++ keywords. The bool type variable can be declared as follows:
bool g1;
g1=true;
bool ks = false;
wchar_t
ANSI C++ has defined this data type for holding 16-bit wide characters. These are used to represent a character set of languages that have more than 255 characters. This concept is essential if programmers write programs for international distribution having different international languages. Wide_character literals begin with the character/letter L and is written as:
L 'ab'
New Operators
You might have earlier used the typecast, which is used to change the type of variable dynamically. It is used to convert a value from one type to another. This concept is necessary when automatic conversion is not possible within a program. We need to use the following forms of casting:
double y = double (m); // Type casing in C++
double g = (double)n; //Type casting in C
Although these casts still work, ANSI C++ has added new typecast operators. These are:
- Static casts
- Dynamic casts
- Reinterpret casts
- Constant casts
This new concept also added another operator known as typeid for verifying the type of unknown objects.
Class Implementation in C++
New ANSI C++ has two unusual, explicit
, and mutable
keywords, generally used with class members. The explicit keyword is applied in C++ for classifying class constructors as explicit constructors. As we know, any constructor with one argument performs the implicit conversion in which the type received by the constructor is converted to an object of the class in which the constructor is defined.
Example:
class G
{
int x;
public:
explicit G(int i)
{
x=i;
}
. . . . . . . .
};
In this case, the objects of the G class can be created using only the following form:
G g_obj(100(;
The automatic conversion form:
G g_obj2 = 100;
The mutable
keyword also plays a significant role in C++. Class objects or member functions may be declared as const
, thus making their member data not modifiable. But in programming, some situations may arise where programmers may want to create a constant object but would likely modify a particular data item. In such cases, the data item may be declared mutable
.
Example:
mutable int s;
Although a function that contains k is declared const
, the value of k may be modified. This is the special characteristic of the mutable keyword.
Namespace Scope
Programmers can define their own namespace within a program. The syntax of defining a namespace is similar to the syntax of defining class. The general form of the namespace is:
Syntax:
namespace namespace_name
{
//Declaration of variables, functions and classes
}