Introduction
What is C++?
Put simply, C++ is a compiled language that is statically typed. This brings about two obvious questions:
What does it mean to be a compiled language?
What does it mean to be statically typed (and what is a type)?
A compiled language is one where we need to pass our written code to a piece of system software called a compiler (more accurately, a compiler driver) that will translate our high-level program into instructions that the machine can understand. For example, we can pass a high-level program written in C++ to a compiler driver like g++ to translate it into an executable (something we can execute).
A type describes a value in our program. This includes information about its size, and what operations we can be performed with that value. There are built-in types like float
and int
in C++ that describes fractional and whole numbers respectively. There are also more complex types and data structures that are part of the C++ Standard Library, like the hash table std::unordered_map
. We can additionally define our own types through the struct
and class
keywords.
A statically-typed language is one where every value in our program has a single type than can not be changed, and that type is known at compile time. For example, when we define a variable in our program, we must give it a type (or the compiler must be able to infer the type). The same is true for function return values and parameters.
Relevant Links
What Is Modern C++?
The C++ language has evolved significantly since its creation (especially in the past 10-15 years). When we talk about writing “modern C++”, we typically mean writing C++ using features and syntax from the latest published standards.
For some, this means writing C++11 code. For others, this can mean using features from C++23. The exact answer to this question depends on who you’re talking to, and the project you’re working on.
Relevant Links
The Core of C++ Applications
C++ programs are centered around functions.
A function is how we tie a logical name to a sequence of statements. They have 4 key parts:
A name
We use this identifier to execute/call our function
A list of 0 or more parameters
A list of inputs to our function
A return type
The type of the output value
A function body
The sequence of statements to run when we execute/call our function
Relevant Links
Where Do Programs Begin?
The core of every C++ application is a function named main
. The main
function is special for a few reasons:
It is the logical starting point of execution of every C++ program
It must be included in every C++ program
It is not a function we directly call
It is called automatically when we execute our applications
It only has 2 valid forms to choose from
The most simple program we can write in C++ is simply a main
function that does (essentially) nothing:
Here, we used the form of main
without input parameters. This form includes:
A name
main
A list of parameters (empty)
()
A return type
int
A function body
The single
return 0;
statement encased in{}
The return type of main
is special, as it indicates the success of execution for our program. A return value of 0
means the program completed without error. Non-zero return values from main
are error codes (to help explain why execution failed).
Relevant Links
How Do Turn Off Our Code?
Comments are pieces of text in our code that are completely ignored by our compiler. We use comments for a number of things, including:
Annotating what our code is doing
Disabling pieces of code, but not deleting them
In C++, we have two forms of comments:
Single line comments
All text following
//
on a single line
Block comments
All text contained within
/*
and*/