C++ Fundamentals
eBook - ePub

C++ Fundamentals

Hit the ground running with C++, the language that supports tech giants globally

  1. 350 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

C++ Fundamentals

Hit the ground running with C++, the language that supports tech giants globally

About this book

Write high-level abstractions while retaining full control of the hardware, performances, and maintainability.

Key Features

  • Transform your ideas into modern C++ code, with both C++11 and C++17
  • Explore best practices for creating high-performance solutions
  • Understand C++ basics and work with concrete real-world examples

Book Description

C++ Fundamentals begins by introducing you to the C++ compilation model and syntax. You will then study data types, variable declaration, scope, and control flow statements. With the help of this book, you'll be able to compile fully working C++ code and understand how variables, references, and pointers can be used to manipulate the state of the program. Next, you will explore functions and classes — the features that C++ offers to organize a program — and use them to solve more complex problems. You will also understand common pitfalls and modern best practices, especially the ones that diverge from the C++98 guidelines.

As you advance through the chapters, you'll study the advantages of generic programming and write your own templates to make generic algorithms that work with any type. This C++ book will guide you in fully exploiting standard containers and algorithms, understanding how to pick the appropriate one for each problem.

By the end of this book, you will not only be able to write efficient code but also be equipped to improve the readability, performance, and maintainability of your programs.

What you will learn

  • C++ compilation model
  • Apply best practices for writing functions and classes
  • Write safe, generic, and efficient code with templates
  • Explore the containers that the C++ standard offers
  • Discover the new features introduced with C++11, C++14, and C++17
  • Get to grips with the core language features of C++
  • Solve complex problems using object-oriented programming in C++

Who this book is for

If you're a developer looking to learn a new powerful language or are familiar with C++ but want to update your knowledge with modern paradigms of C++11, C++14, and C++17, this book is for you. To easily understand the concepts in the book, you must be familiar with the basics of programming.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Chapter 1

Getting Started

Lesson Objectives

By the end of this chapter, you will be able:
  • Explain the C++ compilation model
  • Execute the main() function
  • Illustrate the declaration and definition of variables
  • Determine built-in arithmetic types, references, and pointers
  • Explain the scope of a variable
  • Use control flow statements
  • Define and utilize arrays
In this chapter, you will learn about the usage of variables and control flow statements to create more robust programs.

Introduction

C++ has been a major player in the software development industry for more than 30 years, supporting some of the most successful companies in the world.
In recent years, interest in the language has been growing more than ever, and it is an extremely popular choice for large-scale systems, with many big companies sponsoring its advancement.
C++ remains a complex language, which puts a lot of power in the hands of the developer. However, this also comes with a lot of opportunities to make mistakes. It is a unique language as it has the ability to enable programmers to write high-level abstractions while retaining full control of hardware, performance, and maintainability.

The C++ Compilation Model

It is fundamental to know how C++ compilation works to understand how programs are compiled and executed. Compiling C++ source code into machine-readable code consists of the following four processes:
  1. Preprocessing the source code.
  2. Compiling the source code.
  3. Assembling the compiled file.
  4. Linking the object code file to create an executable file.
Let's start with a simple C++ program to understand how compilation happens.
Create a file named HelloUniverse.cpp and save it on the Desktop after copy-pasting the following code:
#include <iostream>
int main(){
// This is a single line comment
/* This is a multi-line
comment */
std::cout << "Hello Universe" << std::endl;
return 0;
}
Now, using the cd command on the Terminal, navigate to the location where our file is saved and execute the following command if you are on UNIX:
> g++ -o HelloUniverse HelloUniverse.cpp
> ./HelloUniverse
If you are on a Windows system, a different compiler must be used. The command to compile the code with the Visual Studio compiler is as follows:
> cl /EHsc HelloUniverse.cpp
> HelloUniverse.exe
This program, once executed, will print Hello Universe on the Terminal.
Let's demystify the C++ compilation process using the following diagram:
Figure 1.1: C++ compilation of the HelloUniverse file
Figure 1.1: C++ compilation of the HelloUniverse file
  1. When the C++ preprocessor encounters the #include <file> directive, it replaces it with the content of the file creating an expanded source code file.
  2. Then, this expanded source code file is compiled into an assembly language for the platform.
  3. The assembler converts the file that's generated by the compiler into the object code file.
  4. This object code file is linked together with the object code files for any library functions to produce an executable file.

Difference Between Header and Source Files

Source files contain the actual implementation code. Source files typically have the extension .cpp, although other extensions such as .cc, .ccx, or .c++ are also quite common.
On the other hand, header files contain code that describes the functionalities that are available. These functionalities can be referred to and used by the executable code in the source files, allowing source files to know what functionality is defined in other source files. The most common extensions for header files are .hpp, .hxx, and .h.
To create an executable file from the header and the source files, the compiler starts by preprocessing the directives (preceded by a # sign and generally at the top of the files) that are contained in them. In the preceding HelloUniverse program, the directive would be #include. It is preprocessed by the compiler before actual compilation and replaced with the content of the iostream header, which describes standard functionality for reading and writing from streams.
The second step is to process each source file and produce an object file that contains the machine code relative to that source file. Finally, the compilers link all the object files into a single executable program.
We saw that the preprocessor converts the content of the directives into the source files. Headers can also include other headers, which will be expanded, creating a chain of expansions.
For example, let's assume that the content of the logger.hpp header is as follows:
// implementation of logger
Let's also assume that the content of the calculator.hpp header is as follows:
#include <logger.hpp>
// implementation of calculator
In the main.cpp file, we include both directives, as shown in the following code snippet:
#include <logger.hpp>
#include <calculator.hpp>
int main() {
// use both the logger and the calculator
}
The result of the expansion will be as follows:
// implementation of logger
// implementation of logger
// implementation of calculator
int main() {
// use both the logger and the calculator
}
As we can see, the logger has been added in the resulting file twice:
  • It was added the first time because we included logger.hpp in the main.cpp file
  • It was added the second time because we included calculator.hpp, which then includes logger.hpp
Included files that are not directly specified in a #include directive in the file we are compiling, but are instead included by some other included file, are called transitive included files.
Often, including the same header file multiple times creates a problem with multiple definitions, as we will see in Lesson 2, Functions, and the Lesson 03, Classes.
Including th...

Table of contents

  1. Preface
  2. Chapter 1
  3. Getting Started
  4. Chapter 2
  5. Functions
  6. Chapter 3
  7. Classes
  8. Chapter 4
  9. Generic Programming and Templates
  10. Chapter 5
  11. Standard Library Containers and Algorithms
  12. Chapter 6
  13. Object-Oriented Programming
  14. Appendix

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access C++ Fundamentals by Antonio Mallia, Francesco Zoffoli in PDF and/or ePUB format, as well as other popular books in Computer Science & Object Oriented Programming. We have over one million books available in our catalogue for you to explore.