Introduction to C++
Basic Concepts in the C++ Programming Language
Updated: 17 July 2024
Basic Structure
A C++ program has a specific structure in terms of how it’s written
We can import libraries using the #include
preprocessor directive, in this case iostream
that allows input and output to things like the console window
Each program must have a method main in this case which returns an int
C++ makes use of curly braces for containing code blocks as well as semicolons to denote the end of a statement
The std::
part indicates that cout
is part of the std
namespace
Lastly, we can return values from a method using the return
statement
Compilation
C++ code needs to be compiled. This is preprocessed > compiled > linked. The compiler checks the syntax, etc.
Once the compiler has completed its tasks the linker is involved in taking all the object files and linking them together into an executable
Language overview
Formatting
C++ is case sensitive and consits of a few different additional elements
- Preprocessor directives
- Using directives
- Function headers have a return type, name, and params
- Function bodies contain statements
- Comments
- A return statement at the end of functions
- Curly braces to block things together
The compiler ignores whitespace for the most part with some exceptions such as
if
statements
Statements
C++ makes use of a variety of different statements, such as
- Declarations
- Assignments
- Preprocessor directives
- Comments
- Function declarations
- Executable statements (note the
hello world
above)
Types
C++ has lot of different data types built in - non-standard types start with an _
C++ is strongly typed,it has some built in types as well as user-defined types. We can also change types with by casting data from one type into another
Doing something like assigning an int
to a float
will lead to truncation
Assigning a non-bool
to a bool
will lead to pretty much anything besides 0
Variables
You can create a variable using the following syntacxes in which a variable is initialised and defined
Constants
Constants are named memory locations and their value does not change during runtime. They must be assigned values when initialised
Casting
We can cast variables from one data type to another. Sometimes these may lead to a loss of data and other times not
We can also explicitly cast variables using any of the following methods
Beware the integer division, we can use something like the auto
type which will automatically detect the type, but the data is still strongly typed
Arrays
C++ provides support for complex data types, referred to as compound data types as they usually store more than one piece of data
Arrays in C++ need to be defined using the type and size of the array, or by initializing the array or some of its elements
We can access a value in an array using []
syntax
Arrays are 0-indexed as usual
In C++ an array is simply a pointer to a memory location and accessing an index that is not in the range of the array will return some random value - the next value in memory
Strings
Strings are an array of characters, a string must end with the \0
(null
) character in order to tell the compiler where it ends
A char
array must always be one character longer than necessary in order to store the \0
Practically though you can also define an array using a string literal
Using the above method the compiler will infer the length of the string, note that if explicitly defining the length you must leave space for the \0
character
There is also a string class which can be used, this will need to be referenced in the header file and can be used as follows
Structures
Structs allow us to store more complex information as a compound data type. They are known as user-defined types
We can then make use of the struct
in a couple of different ways
As seen above, properties can be accessed or assigned using the dot notation
Unions
Unions are like structs but can only data in one of it’s fields at a time
Unions are useful for working with memory-limited devices
Enumerables
Enums are a means of creating symbolic constants, by default these values will start at 0 but we can define them to start at a different number
Sunday == 0, Monday == 1, etc.
Or we can start at 1 and have each day be the human number
Operators
The available mathematical operators are as follows
Conditional Operators
COnditional Operators in C++ (aka Ternary Operators) take three values, the first of which is a condition, and the second or third are evaluated based on the result of the condition
Flow Control
If Statements
C++ makes use of boolean operators in order to build if
statements
If there is only a single statement we can leave out the curly brackets
We can also use else if
and else
Switch Statements
We can use these when the case of complex if-else
statements
Switch operators support intrisic
data types and enums
For Loops
For loops look like this:
The loop is made of the general structure of:
While Loops
While loops follow the traditional C-type
syntax
For example:
If the condition is not initally met, the loop will not run
Do-While Loops
A do-while
loop is like a while
loop but the condition is checked at the end of the loop, and will hence always run at least once
Note the semicolon at the end
Functions
Functions are defined by Name, Return Type, and Arguments. Functions with the same Name and Return type but different numbers arguments are allowed, the compiler will figure out which one you are trying to use based on the number of arguments
The compiler must know about a function before it can be called
Prototypes
A complete function signature/prototype consists of the following
- Storage class
- Return type
- Name
- Parameters
Function prototypes need to be defined in a header file, Header files are imported into source code files so that the compiler can ensure proper use of functions, etc.
The prototype does not consist of the function implementation code
A function prorotype is defined as follows
By default data is passed to functions by value and not by reference
Inline Functions
Inline functions are functions that are are essentially pieces of code that the compiler will inject into the place where it is being called instead of making a function call
This can be used to reduce some of the overhead that would be associated with using a normal function
These are better suited for small functions that are used frequeltly and make use of the inline
keyword
Storage Classes and Scope
“A storage class in the context of C++ variable declarations is a type specifier that governs the lifetime, linkage, and memory location of objects”
This means that it governs how long an object remains in memory, in what scopes it is visible and whether it should be located in a stack or heap
Some keywords that apply to storage classes are:
static
extern
thread_local
If we do not state the function prototype on the file we try to use it we will get a compiler error, if we state the prototype but that function cannot be found we will get a linker error
The reason for this is because each individual file is compiled separately
In order to avoid declaring prototypes in each file, you can create a header
file and define shared prototypes there, and include the header file, this can be done simply as follows
Math.cpp
Utilities.h
Main.cpp
Classes
Definition
Classes are definitions for custom types and defines the behaviour and characteristics of a type
We can define a Rectangle with the class
keyword
Class definitions must end with a
;
Members can be accessed with dot
notation
Initialization
We can create a new instance of a class using a few different methods
Uninitialized values will have junk data, don’t do this
Encapsulation
Encapsulation is used to describe accesibility of class members, this is used to restrict the way in which class data can be manipulated
Functions in a class have access to the instance of the class this
which is a pointer. this
is used to remove ambiguity betwen member variables and is not always necessary
We cannot directly access private members from outside of a class
A class can be defined in a header file or have some aspects of it’s functionality (or all) be placed into separate .cpp
files
Rectangle.h
Constructors
Muliple constructors can be created with different parameters, the compiler will figure out which one to use for a specific instance based on the input arguments, we can see a lot of different examples here
If a default constructor is not define the compuler will provide an implicit inline
instance
Additionally we can add default values for the properties of a class
We can also remove some parts of functionality away from our class definition into a .cpp
file
Rectangle.h
Rectangle.cpp
Immutable Objects
We can create const
objects but we need to also explicitly define member functions that will not modify the object as const
as well
And then create Rectangles and use the function as normal