How to Compile and Run C++ 11 Codes in Dev C++
This brief guide is about compiling C++ 11 codes with Dev C++.
The C++ 11 Version
C++ 11 is the new version of C++ after C++ 3. It was released on August 12, 2011, after approval from International Standard Organization (ISO).
This new version of C++ adds a whole set of new features in C++. Though, the use of new features is users’ own choice.
Nevertheless, some of the features are very helpful and make programming in C++ more interesting.
The primary goals of C++ 11 characterized by Bjarne Stroustrup are:
- Increase C++ strengths. Rather than extending C++ to add some new features that can allow building heavy GUI applications (which is the weaker area of C++), the focus is on improving what it already does.
- Make C++ easier to learn and use. It provides functionalities that make C++ more consistent and easier.
The committee that put together C++ 11 features followed some of the main principles, which are:
- Make C++ 11 compatible with older versions of C++ such that programs that work under older versions should generally work with C++ 11.
- Rather than adding more extensions to the core language, they put a bundle of changes in the standard library.
- They focused on improving abstraction mechanisms (classes, templates).
- They added features for both the new entries and the experts of C++ programming, providing a little something for every group of developers.
- Increased the type safety of language to prevent unintentional bugs and errors.
- Improved the performance of C++, allowing it to work directly with the hardware.
Features of C++ 11
C++ 11 is not a big migration from C++ 03, but it did add some major new features. We will discuss some of them here.
- Type deduction feature: C++ 11 introduced a new keyword
auto
for automatically deducing the variable’s data type defined by the initializer value.
auto num1 = 5;
int num2 = 10;
As we have initialized num1
with an integer, the type of num1
will be int
. Similarly, the auto
can also deduce the type of variable using another variable as an initializer.
Example:
int a = 5;
auto b = a;
In this code, b
will be initialized with the value of a
, and its type will be set as a
that is int
. Variables that are not initialized with a value can also be set using decltype
.
Example:
int var1 = 0;
decltype(var2) var1;
We didn’t initialize var2
with any value, but its data type will be the same as var1
. Although type deduction is a powerful feature of C++ 11, it is only recommended where type cannot be specified or where using the type deduction mechanism improves the efficiency or readability of code.
- Uniform Initialization of Variables: C++ 11 introduced a new method pf initializing a variable using curly braces
{}
. Note that the previous methods of initialization still work in C++ 11.
type identifier {initial_value};
Example:
int x{0}; // This will initialize `x` with 0.
For-each
Loops: We have seen that thefor
loop iterates through the array elements, but there is a chance of error in the increment or condition. Thefor-each
loop has been introduced to iterate through arrays to ease this difficulty.
Syntax:
for (element_declaration : array) statement;
Example:
#include <iostream>
using namespace std;
int main() {
int evens[]{0, 2, 4, 6, 8, 10, 12, 14};
for (int num : evens) {
cout << num << ' ';
}
cout << '\n';
return 0;
}
Output:
0 2 4 6 8 10 12 14
We can also use the auto
type in this loop for more ease.
Example:
#include <iostream>
using namespace std;
int main() {
int evens[]{0, 2, 4, 6, 8, 10, 12, 14};
for (auto num : evens) {
cout << num << ' ';
}
cout << '\n';
return 0;
}
This is the ideal case for using the type deduction mechanism. Whatever the array type, it will be set for the iterator variable.
A bundle of new features is introduced in C++ 11, but we have discussed some of the most commonly used features here.
Similarly, there are many new classes added to the standard C++ library, which are:
- Multi-threading and thread-local storage
- Hash tables
- Random number generation
- Reference wrappers
- Regular expressions
std::tuple
std::unique_ptr
Install Dev C++ to Compile and Run C++ 11 Codes
Dev C++ compiler has its own specific integrated development environment (IDE) for writing, debugging, and running C++ programs. It is packaged with an efficient text editor for writing and editing programs and a C++ compiler.
This text editor has limited features that allow you to write a program, modify it, and save it on your disk storage space. Dev C++ provides an editor and compiler not only for C++ but also for C.
After downloading and installation of Dev C++, you should have the following Window:
You can now create a new project by going to File->New->Project
as shown in the image below:
From the new Window, select Console Application
. This will create a new project where you can add source files for C++ programming.
A single source file, main.cpp
, is created for you by default. For C++ 11, you need first to configure its compiler. For this purpose, go to Tools->Compiler Options
.
Go to the Settings
tab, then the Code Generation
tab. From the Language Standard
menu, select ISO C++11
.
You are now ready to compile C++ 11 code in Dev C++. You can now write the code in your main file already created for you and compile the code by hitting the F11 key, which will compile and run the code for you.
So, we have seen that Dev C++ is an easy-to-use compiler with the latest functionalities of C++ 11 available in it. We can quickly compile and run C++ 11 codes using Dev C++.
If you do not find the option for C++ 11 in the last step, make sure you have downloaded the latest version of Dev C++.
Husnain is a professional Software Engineer and a researcher who loves to learn, build, write, and teach. Having worked various jobs in the IT industry, he especially enjoys finding ways to express complex ideas in simple ways through his content. In his free time, Husnain unwinds by thinking about tech fiction to solve problems around him.
LinkedIn