Um unsere Webseiten für Sie optimal zu gestalten und fortlaufend zu verbessern, verwenden wir Cookies. Durch Bestätigen des Buttons »Akzeptieren« stimmen Sie der Verwendung zu. Über den Button »Einstellungen« können Sie auswählen, welche Cookies Sie zulassen wollen.

AkzeptierenEinstellungen
Zum Hauptinhalt springen
Nicht aus der Schweiz? Besuchen Sie lehmanns.de

C++ (eBook)

The Comprehensive Guide

(Autor)

eBook Download: EPUB
2025
1093 Seiten
Packt Publishing (Verlag)
978-1-80610-056-9 (ISBN)
Systemvoraussetzungen
58,79 inkl. MwSt
(CHF 57,40)
Der eBook-Verkauf erfolgt durch die Lehmanns Media GmbH (Berlin) zum Preis in Euro inkl. MwSt.
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

This book begins by grounding readers in the essentials of modern C++23, covering syntax, compiling, and core programming concepts. Early chapters introduce building blocks like data types, functions, and statements, ensuring a solid foundation. Readers also learn coding best practices focused on readability and modularization.
As the journey progresses, the focus shifts to object-oriented programming, exploring classes, inheritance, namespaces, and lifecycle management. The text includes advanced topics such as templates, macros, and the integration of C libraries. Readers develop skills in designing secure, maintainable, and extensible code while mastering error handling and testing.
The final sections dive into concurrency, standard library features like containers and algorithms, and advanced stream handling. Practical guidance on thread management, synchronization, and modern concurrency tools prepares readers for real-world applications. Concluding chapters present C++ guidelines, emphasizing sustainable and quality code development, completing a comprehensive path from fundamentals to expert-level mastery.

3    C++ for Newcomers


This chapter is primarily aimed at those transitioning from Java or C#, but newcomers from other higher-level languages, especially object-oriented languages, will also benefit, as will those refreshing their C++ skills after some time. I provide a general overview of the idiosyncrasies of C++ that may surprise or pose difficulties for newcomers.

All other readers can skip this chapter; the overview and introduction follow.

The following elements are ones that many developers should recognize:

  • Statements
    Programs are executed statement by statement, one after the other—at least per thread, at least in the model. The rule of thumb is that semicolons separate statements from each other. Statements can be combined into blocks.

  • Expressions
    An expression recursively consists of expressions down to indivisible units such as literals or variables. Arithmetic expressions contain mathematical calculations, for example. In C++, an exact type can be assigned to every expression.

  • Data types
    C++ offers a range of simple data types such as int and double. There are also pointers and references, which are separate types in C++. An int and a pointer to it int* are different types. You can aggregate several types together to obtain new types.

  • Functions and methods
    To prevent programs from turning into long spaghetti, reused areas can be outsourced to functions. Functions that are in a data type are called methods.

  • Classes
    Data types that you also bundle with behavior—that is, to which you add methods—are called classes.

  • Function calls
    A function call takes parameters and returns a result. What happens within the function is partially invisible from the outside.

  • Parameters
    Functions receive parameters. In C++, the function decides whether the parameter is to be used as a value (by value) or as a reference—or pointer (by reference)—not the caller.

  • Returns
    The same applies to returns from functions. A result can be assigned to a variable or used within an expression. The function decides whether the return value is an independent copy or a reference.

Some things may seem familiar to experienced programmers at first glance, but they have important conceptual differences from other languages in detail. For example, if Java developers bring incorrect preconceptions here, they might later be confused and encounter nasty surprises. Therefore, I want to briefly mention a few things that could be stumbling blocks:

  • Stack and heap instead of garbage collection
    It will come as no surprise when I tell you that there is no automatic cleaning up of objects in C++. Don't see this as a disadvantage; live the advantage. Separate between things that are automatically managed on the stack by the compiler when the block is exited and those that you request on the heap with new and for which you take responsibility for clearing away. Don't mess around: better use RAII (see Chapter 17).

  • Virtual versus real machines
    It is known that Java code, once translated to class files (thanks to the Java virtual machine [JVM]), runs on all platforms (“write once, run anywhere”). C++ code must be compiled separately for each platform because the compiler output is directly executable machine code (“write once, compile anywhere”). Although this is not a requirement according to the standard, it is usually the case.

  • C++-char versus Java-char
    C++-char is usually eight bits wide and appears on different systems sometimes as signed and sometimes as unsigned. Without the corresponding designation, you can therefore only rely on a value range from 0 to 127. Only if you write signed char does it correspond to the Java byte. Java-char corresponds more to C++-short and guaranteed to int16_t. The latter does not have to be present, but it is de facto.

  • C++-optional versus Java-optional
    In Java, you often use optional as part of the Java Stream API. In C++, the containers and the new ranges correspond most closely to the Java stream API. However, optional is not a container in C++, and therefore you will not use optional as in Java. However, with C++23 the monadic transformations have been added, which move optional in this direction.

  • Function objects versus lambdas
    At first glance, Java lambdas are similar to C++ lambdas. On closer inspection, the anonymous function objects in C++ are more rounded. In Java, some effort still has to be made at call time in the JVM to dynamically create a function object. In C++, the compiler has completely outsourced the function and only given it an invisible name. Binding to local variables is similar, but in C++ you can choose between binding as a value or as a reference.

  • Values and references in C++ versus Java and the like
    Everything that is an object is a reference in Java. In C++, everything is a value and is copied for parameters and returns. Only with special provision with & and * can a function explicitly request references and pointers instead.

  • Throwing values instead of pointers
    In C++, you do not write “throw new X(...)”, but only throw X(...). If you don't pay attention to this, you will run into trouble in the medium term. For example, you would have to delete the exception objects yourself. And this is not always possible, as is the case with catch(...) (catch-all). And what about rethrowing? You should instead rely on the C++ mechanisms, where it is guaranteed that an exception instance exists as long as it is needed. The time to deviate from this rule is when a framework requires you to explicitly clear away because it throws exception instances as pointers. In this case, you should of course adhere to what the framework prescribes. Read its documentation to find out whether you should clear the exception in the catch block.

  • const versus final
    Because all objects in Java are references, final in Java only refers to the reference marked with it. You certainly know that you can change its content wildly despite final if the interface allows this (which fortunately is not the case with things like Integer). Because everything is initially a value in C++, const also protects the content. In connection with references and pointers, you have even more control, as you can see in Listing 3.6 and its explanation.

  • Templates versus generics
    Both use angle brackets, and yet they are completely different. In Java, only a single function or class is created per generic; you are only relieved of the type conversions—for example, for returning a value from methods. The only thing you know about the type parameter is that it is either an object or that it implements a specific interface. A generic therefore always applies to a specific group of objects. In C++, a template is instead a stencil, which must just be parseable C++ code from the compiler, nothing more. Only when you use it do you specify the types of parameters, and C++ then inserts them—and generates the actual function at this moment (at compile time). This can then be different functions for each type.

  • Interface approaches in C++ and Java
    In C++, there is multiple inheritance without restriction. And because, according to Terry Pratchett, nothing good ever follows the word multiple,[ 2 ] the Java thinkers didn't want to bring this specific devil into their house either. But you can't do without it completely, because then object orientation would be impossible. This is why Java has implementation-free[ 3 ] interface declarations. This sensible approach puts a stop to the misuse of multiple inheritance and makes it more difficult to design overcomplicated interface hierarchies. In C++, it is the other way around: restrictions that are intended to protect against design errors must come from the developers; they do not come from the language. As a sensible restriction, you can start by adopting the idea from Java: for example, if multiple inheritance is used, then make sure that at most one of the parent classes contributes implementations and the rest are only so-called signature classes—that is, classes that contain only pure virtual methods. This is what C++ calls abstract methods—that is, virtual methods that have zero assigned instead of...

Erscheint lt. Verlag 18.6.2025
Sprache englisch
Themenwelt Informatik Programmiersprachen / -werkzeuge C / C++
ISBN-10 1-80610-056-8 / 1806100568
ISBN-13 978-1-80610-056-9 / 9781806100569
Informationen gemäß Produktsicherheitsverordnung (GPSR)
Haben Sie eine Frage zum Produkt?
EPUBEPUB (Ohne DRM)

Digital Rights Management: ohne DRM
Dieses eBook enthält kein DRM oder Kopier­schutz. Eine Weiter­gabe an Dritte ist jedoch rechtlich nicht zulässig, weil Sie beim Kauf nur die Rechte an der persön­lichen Nutzung erwerben.

Dateiformat: EPUB (Electronic Publication)
EPUB ist ein offener Standard für eBooks und eignet sich besonders zur Darstellung von Belle­tristik und Sach­büchern. Der Fließ­text wird dynamisch an die Display- und Schrift­größe ange­passt. Auch für mobile Lese­geräte ist EPUB daher gut geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür die kostenlose Software Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür eine kostenlose App.
Geräteliste und zusätzliche Hinweise

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
C++ lernen – professionell anwenden – Lösungen nutzen

von Ulrich Breymann

eBook Download (2023)
Carl Hanser Verlag GmbH & Co. KG
CHF 48,80

von Carsten Vogt

eBook Download (2024)
Carl Hanser Verlag GmbH & Co. KG
CHF 38,95