What is C ++?
The programming language C ++ is widespread, is used in a variety of ways and has long been one of the most important languages. It was originally developed as a further development of the C language. Due to the relationship to C, it is also possible to develop hardware-oriented, fast programs in C ++.
C ++ makes it possible to program in a purely object-oriented manner, i.e. with classes, objects, properties and methods. In preparation for this, it is advisable to start with C ++ in a classic procedural manner, i.e. with data types, operators, control structures and functions. In this way you get a solid foundation, which then makes it easier to get started with object orientation.
The versions of C ++
C ++ was developed in 1979 by Bjarne Stroustrup. The language has seen many innovations over the years. In 1998 the standardized version C ++ 98 appeared. Further improvements were made with the standards C ++ 03, C ++ 11 and C ++ 14 in 2003, 2011 and 2014. The development of version C ++ 17 was completed in March 2017. Version C ++ 20 is already in progress.
You should work with a development environment (= IDE = Integrated Development Environment) that takes the C ++ 11 standard into account. You can make the necessary settings when installing the IDEs or when developing C ++ projects using the IDEs. Examples of IDEs are: Eclipse, NetBeans, Xcode and Orwell Dev C ++. Important reasons for using C ++ 11 are:
- The currently available C ++ compilers mostly completely implement the standards up to and including C ++ 11, but not always all components of the subsequent versions.
- In contrast to the following versions, the standards up to and including C ++ 11 contain many new features that are also of interest to beginners.
If you need more information about the C ++ language and how its elements are assigned to the various versions, I recommend the C ++ reference at http://cppreference.com .
User interfaces with the Qt library
There are numerous libraries that expand the already extensive possibilities of C ++. For the development of programs with user interfaces, so-called GUIs (GUI = Graphical User Interface) and for access to databases, you can use the widely used, operating system-independent Qt library and the associated IDE Qt-Creator. Since version 5.7 from June 2016, Qt has been completely based on C ++ 11. Qt primarily works with C ++, but can also be used with other programming languages.
Event-driven programming is used in Qt. The user interfaces contain control elements, so-called widgets, with the help of which the user operates the program. The operation of a widget, for example clicking a button or selecting an entry in the list, is an event. A signal is sent in the process. A signal can be connected to a slot. This is a function, the code of which is run through as a result of the event.
In addition to the classes for GUI development, the Qt library also offers many useful classes that extend the possibilities of C ++ in general, such as QString for strings, QDate for dates or QTime for times.
Containers and Iterators
When developing applications, there is often a need to store large amounts of data. Various data structures in different forms are suitable for this. They each have certain advantages (and disadvantages) for organizing the data. The C ++ container library contains a large number of class templates and methods. A class template is a template that can be applied to different types of data. The different containers enable the programming implementation of different data structures.
A container manages the storage space for its elements and provides methods for accessing these elements. You can access the elements directly or using iterators. These are intelligent pointers that are suitable for the passage of containers.
The selection of the suitable container depends, among other things, on the problem, the desired options for accessing the data and the amount of data to be expected. The data load, i.e. the amount of data to be expected per unit of time, is also an important factor.
A vector field
The class template vector is suitable for storing a variable, i.e. changeable set of several values of the same data type. This creates an intelligent dynamic field, also called a vector field. Figure 1 shows a vector field of integer values of the data type int, the associated iterator and some methods.
In the following program “container_vector.cpp” some typical operations and methods with vector fields are explained. The output of the program follows:
Ausgabe des gesamten vector-Felds
void ausgeben(vector<int> &x)
{
// Iterator für vector-Felder
vector<int>::iterator position;
// Schleife von der Beginnposition bis vor die Endposition
for(position = x.begin(); position != x.end(); position++)
// Ausgabe eines ELements
cout << *position << ” “;
cout << endl;
}
int main()
{
// Erzeugung des vector-Felds
vector<int> v;
vector<int>::iterator position;
// Festlegen der Größe
v.resize(3);
// Füllen mit Werten
for(unsigned int i=0; i<v.size(); i++)
v.at(i) = 10 * (i+1);
cout << “at(): “;
// Ausgabe
ausgeben(v);
// Anhängen von Elementen am Ende
for(int i=0; i<3; i++)
v.push_back(4 * (i+1));
cout << “push_back(): “;
ausgeben(v);
// Entfernen von Elementen am Ende
v.pop_back();
cout << “pop_back(): “;
ausgeben(v);
// Einfügen von Elementen mitten im Feld
cout << “insert(): “;
position = v.begin() + 1;
// Prüfen der Position
if(position >= v.begin() && position < v.end())
{
v.insert(position, 77);
ausgeben(v);
}
else
cout << “Position nicht im vector-Feld” << endl;
// Löschen von Elementen mitten im Feld
cout << “erase(): “;
position = v.end() – 2;
// Prüfen der Position
if(position >= v.begin() && position < v.end())
{
v.erase(position);
ausgeben(v);
}
else
cout << “Position nicht im vector-Feld” << endl;
// Löschen aller Elemente
v.clear();
cout << “clear():”;
ausgeben(v);
// Initialisieren mit neuen Werten
v = {19, 18, 17};
cout << “Neue Werte: “;
ausgeben(v);
}