Book Image

C++ Fundamentals

By : Antonio Mallia, Francesco Zoffoli
Book Image

C++ Fundamentals

By: Antonio Mallia, Francesco Zoffoli

Overview of this book

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.
Table of Contents (9 chapters)
C++ Fundamentals
Preface

Built-in Data Types


In most programming languages, data is stored in variables, which are labels that refer to the part of memory defined by the programmer. Each variable has an associated type. The type defines what kind of values the variable can hold.

The built-in data types of C++ are divided into two categories:

  • Primitive data types: Can be used directly by the user to declare variables

  • Abstract or user defined data types: Are defined by the user, for example, to define a class in C++ or a structure

Primitive Data Types

Primitive data types consist of the following types:

  • Integer: The int type stores a whole number value ranging from -2147483648 to 2147483647. This data type usually takes up 4 bytes of memory space.

  • Character: The char type stores character data. It is guaranteed to be big enough to represent any UTF-8 single byte code unit; for UTF-16 and UTF-32, char16_t and char32_t are used, respectively. char typically takes 1 byte of memory space.

  • Boolean: The bool data type is capable of holding one of two values: true or false.

  • Floating-point: The float type is used for storing single precision floating point values. This data type usually takes up 4 bytes of memory space.

  • Double floating point: The double type is used for storing double precision floating point values. This data type usually takes up 8 bytes of memory space.

  • Void: The void type is a valueless data type that is used for functions that do not return a value.

  • Wide character: The wchar_t type is also used to represent character sets, but allows for greater size. While char supports characters between 8 and 32 bits, a wide character is 2 to 4 bytes long.

The character types char and wchar_t hold numeric values corresponding to the characters in the machine's character set.

Datatype Modifiers

The numeric types offered by the C++ programming language fall into three categories:

  • Signed

  • Unsigned

  • Floating point

The signed and unsigned types come with different sizes, which means each of them can represent a smaller or larger range of values.

Integer types can be signed or unsigned, where signed types can be used to distinguish between negative or positive numbers, while unsigned can only represent numbers greater than or equal to zero.

The signed keyword is optional; the programmer only needs to specify it if the type is unsigned. Thus, signed int and int are the same types, but they are different from unsigned int, or just unsigned for brevity. Indeed, if it is not specified, an unsigned type always defaults to int.

Integers, as previously mentioned, can come in different sizes:

  • int

  • short int

  • long int

  • long long int

The short int type, or just short, is guaranteed to be at least 16 bits according to the standard. This means it can hold values in the range of -32768 to 32767. If it was also unsigned, so unsigned short int or just unsigned int, this range would be 0 to 65535.

Note

The effective size in memory of types can change based on the platform for which the code is compiled. C++ is present in many platforms, from supercomputers in data centers to small embedded chips in industrial settings. To be able to support all these different types of machines, the standard only sets the minimum requirements on built-in types.

Variable Definition

A variable is named storage that refers to a location in memory that can be used to hold a value. C++ is a strongly-typed language and it requires every variable to be declared with its type before its first use.

The type of the variable is used by the compiler to determine the memory that needs to be reserved and the way to interpret its value.

The following syntax is used to declare a new variable:

type variable_name;

Variable names in C++ can contain letters from the alphabet, both upper and lower case, digits and underscores (_). While digits are allowed, they cannot be the first character of a variable name. Multiple variables of the same type can all be declared in the same statement by listing their variable names, separated by commas:

type variable_name1, variable_name2, …;

This is equivalent to the following:

type variable_name1;
type variable_name2;
type ...;

When declaring a variable, its value is left undetermined until an assignment is performed. It is also possible to declare a variable with a given value; this operation is also referred to as variable initialization.

One way – and probably the most common one – to initialize a variable, also referred to as C-like initialization, uses the following syntax:

type variable_name = value;

Another solution is constructor initialization, which we will see in detail in Lesson 3, Classes. Constructor initialization looks like this:

type variable_name (value);

Uniform initialization or list initialization introduces brace initialization, which allows for the initialization of variables and objects of different types:

type variable_name {value};

Demystifying Variable Initialization

When a variable is initialized, the compiler can figure out the type needed to store the value provided, which means that it is not necessary to specify the type of the variable. The compiler is indeed able to deduct the type of the variable, so this feature is also referred to as type deduction. For this reason, the auto keyword has been introduced to replace the type name during initialization. The initialization syntax becomes this:

auto vvariable_name = value;

Another way to avoid directly providing a type is to use the decltype specifier. It is used to deduce a type of a given entity and is written with the following syntax:

type variable_name1;
decltype(variable_name1) variable_name2;

Here, variable_name2 is declared according to the type deducted from variable_name1.

Note

Type deduction using the auto and decltype keywords has been introduced by the C++11 standard to simplify and facilitate variable declaration when the type cannot be obtained. But at the same time, their extended use when not really needed can reduce code readability and robustness. We will see this in more detail in Lesson 4, Generic Programming and Templates.

In the following code, we will check a valid statement for variables by creating a new source file named main.cpp and analyzing the code one line at a time.

Which one of the following is a valid statement?

int foo;
auto foo2;
int bar = 10;
sum = 0;
float price = 5.3 , cost = 10.1;
auto val = 5.6;
auto val = 5.6f;
auto var = val;
int  a = 0, b = {1} , c(0);