C++ Language
C++ Language
C++ Language
Machine code: It is basically the same as machine language and it is a set of instructions that a
CPU natively understands (Zeroes and ones).
Assembly Language: Higher level language in which each instruction is identified by a short name,
needing to be translated (assembled) into machine code.
Languages Types
Compiler: is a program that reads code and produces a stand-alone executable program that the
CPU can directly understand.
Interpreter: is a program that directly executes your code without compiling it into machine code
first.
When compiling (.o) files are created which are the compiled version of the Source File (.cpp)
The LINKER is a program which also comes to play an important part in the compiling process since
it links all the object files generated by the compiler into a single executable.
IDE TIPS
When creating a project in an IDE (Integrated Developement Environment) you are set by default
to the DEBUG configuration. This includes all the extra code and repositories needed to debug the
program. Once the program code has been completed, the RELEASE configuration should be used.
This setting will compile your program without the debugging tools, making it a much smaller file
size andf thus increasing compiling speed.
STATEMENTS
int x; is a declaration statement since it declares that X will hold and integer.
std::cout << x; is an output statement. It outputs the value of x (which we set to 5 in the
previous statement) to the screen.
A RETURN STATEMENT indicates to the OS whether the program was run successfully or not.
x = 2 + 3;
Every C++ program must contain a special function called main. When the C++ program is run,
execution starts with the first statement inside of function main.
PREPROCESSOR DIRECTIVES (or libraries)
Tell the compiler to perform a special task like in #include <iostream> which orders the compiler
to include the iostream library.
COMMENTS
A comment is a line (or multiple lines) of text that are inserted into the source code to explain
what the code is doing. In C++ there are two kinds of comments.
The // symbol begins a C++ single-line comment, which tells the compiler to ignore everything to
the end of the line.
The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the
symbols is ignored.
OBJECTS
An object is a piece of memory that can be used to store values. When an object is defined, a
piece of that memory is set aside for the object.
VARIABLES
A variable in C++ is simply an object that has a name. All variables in C++ as treated as l-values
(refers to a value that has an address in memory).
The opposite are R-values, which refers to any value that can be assigned to an l-value.
ASSIGNMENT VS INITIALIZATION
After a variable is defined, a value may be assigned to it via the assignment operator (the = sign):
C++ will let you both define a variable AND give it an initial value in the same step. This is called
initialization.
A variable that has not been given a known value (through initialization or assignment) is called an
uninitialized variable.
std::cout
As noted in previous sections, the std::cout object (in the iostream library) can be used to output
text to the console. To print more than one thing on the same line, the output operator (<<) can
be used multiple times. For example:
std::endl
If we want to print things to more than one line, we can do that by using std::endl. When used
with std::cout, std::endl inserts a newline character (causing the cursor to go to the start of the
next line).
std::cin
Is the opposite of std::cout -- whereas std::cout prints data to the console using the output
operator (<<), std::cin reads input from the user at the console using the input operator (>>).
FUNCTIONS
A function call is an expression that tells the CPU to interrupt the current function and execute
another function. The function initiating the function call is called the caller, and the function
being called is the callee or called function.
The actual value returned from a function is called the return value. The word void is used for
functions that do not return a value. We can set the return type of a function as shown:
ARGUMENTS OF FUNCTIONS
An argument is a value that is passed from the caller to the function when a function call is made:
When a function is called, all of the parameters of the function are created as variables, and the
value of each of the arguments is copied into the matching parameter. This process is called pass
by value.
When a function becomes too long, too complicated, or hard to understand, it should be split into
multiple sub-functions. This is called refactoring.
KEYWORDS
C++ reserves a set of 84 words (as of C++14) for its own use. These words are called keywords (or
reserved words), and each of these keywords has a special meaning within the C++ language.
IDENTIFIER NAMING RULES
The name of a variable, function, type, or other kind of object in C++ is called an identifier.
The identifier can only be composed of letters (lower or upper case), numbers, and the
underscore character. That means the name cannot contain symbols (except the underscore) nor
whitespace (spaces or tabs). The identifier must begin with a letter (lower or upper case) or an
underscore. It cannot start with a number.
C++ distinguishes between lower and upper case letters. nvalue is different than nValue is
different than NVALUE.
LOCAL SCOPE
A variable’s scope determines when a variable can be seen and used during the time it is
instantiated. Function parameters and variables defined inside the function body both have local
scope. That is, those variables can only be seen and used within the function that defines them.
Local variables are created at the point of definition, and destroyed when they go out of scope
(usually at the end of the function).
EXPRESSIONS
A literal is a fixed value that has been inserted (hardcoded) directly into the source code, such as
5, or 3.14159.
Literals, variables, and function calls that return values are all known as operands. Operands
supply the data that the expression works with. We just introduced literals, which evaluate to
them. Variables evaluate to the values they hold. Functions evaluate to produce a value of the
function’s return type (unless the return type is void).
Unary operators act on one operand. An example of a unary operator is the - operator. In the
expression -5, the - operator is only being applied to one operand (5) to produce a new value (-5).
Binary operators act on two operands (known as left and right). An example of a binary operator is
the + operator. In the expression 3 + 4, the + operator is working with a left operand (3) and a right
operand (4) to produce a new value (7).
Ternary operators act on three operands. There is only one of these in C++, which we’ll cover
later.
A forward declaration allows us to tell the compiler about the existence of an identifier before
actually defining the identifier.
To write a forward declaration for a function, we use a declaration statement called a function
prototype. The function prototype consists of the function’s return type, name, parameters, but
no function body (the part between the curly braces).
HEADER FILES
Header files usually have a .h extension, but you will sometimes see them with a .hpp extension or
no extension at all. The purpose of a header file is to hold declarations for other files to use.
Rule: Use angled brackets to include header files that come with the compiler.
Use double quotes to include any other header files.
Rule: Each .cpp file should explicitly #include all of the header files it needs to
compile.
Rule: use the non .h version of a library if it exists, and access the functionality
through the std namespace. If the non .h version does not exist, or you are
creating your own headers, use the .h version
PREPROCESSOR
When the preprocessor runs, it simply scans through each code file from top to bottom, looking
for directives. Directives are specific instructions that start with a # symbol and end with a newline
(NOT a semicolon).
INCLUDES
When you #include a file, the preprocessor copies the contents of the included file into the
including file at the point of the #include directive.
#include <filename> tells the preprocessor to look for the file in a special place defined by the
operating system where header files for the C++ runtime library are held. You’ll generally use this
form when you’re including headers that come with the compiler (e.g. that are part of the C++
standard library).
#include "filename" tells the preprocessor to look for the file in the directory containing the
source file doing the #include. If it doesn’t find the header file there, it will check any other include
paths that you’ve specified as part of your compiler/IDE settings. That failing, it will act identically
to the angled brackets case. You’ll generally use this form for including your own header files.
MACRO DEFINES
The #define directive can be used to create a macro. A macro is a rule that defines how an input
sequence (e.g. an identifier) is converted into a replacement output sequence (e.g. some text).
CONDITIONAL COMPILATION
The conditional compilation preprocessor directives allow you to specify under what conditions
something will or won’t compile. The only conditional compilation directives we are going to cover
in this section are #ifdef, #ifndef, and #endif.
ADDRESSING MEMORY
The smallest unit of memory is a binary digit (bit), which can hold a value of 0 or 1.
Memory is organized into sequential units called memory addresses (or addresses for short).
The smallest addressable unit of memory is known as a byte. The modern standard is that a byte is
comprised of 8 sequential bits.
C++ comes with built-in support for certain data types. These are called fundamental data types
(in the C++ specification), but are often informally called basic types, primitive types, or built-in
types.
Because C++ grew organically, the copy initialization and direct initialization forms only work for
some types of variables (for example, you can’t use either of these forms to initialize a list of
values).
In an attempt to provide a single initialization mechanism that will work with all data types, C++11
adds a new form of initialization called uniform initialization (also called brace initialization):
Initializing a variable with an empty brace indicates default initialization. Default initialization
initializes the variable to zero (or empty, if that’s more appropriate for a given type).
DATA TYPES
VOID:
Void is the easiest of the data types to explain. Basically, it means “no type”!
1. Most commonly, as a way to indicate that a function does not return a value
2. In C, as a way to indicate that a function does not take any parameters.
Rule: Use an empty parameter list instead of void to indicate no function parameters are expected.
bit 0 bit 1
0 0
0 1
1 0
1 1
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
To generalize, a variable with n bits can hold 2n (2 to the power of n, also commonly written 2^n)
possible values. With an 8-bit byte, a byte can store 28 (256) possible values.
THE SIZE OF C++ BASIC DATA TYPES
C++ guarantees that the basic data types will have a minimum size:
The sizeof operator is a unary operator that takes either a type or a variable, and returns its size in
bytes.
INTEGERS
An integer type (sometimes called an integral type) variable is a variable that can only hold non-
fractional numbers (e.g. -2, -1, 0, 1, 2). C++ has five different fundamental integer types available
for use: