C++ Notes
C++ Notes
C++ Notes
C++ NOTES-1
_______________________________________________________________________________________________
What is C++?
C++ is a cross-platform language that can be used to create high-
performance applications.
C++ was developed by Bjarne Stroustrup in 1980, as an extension
to the C language.
C++ gives programmers a high level of control over system
resources and memory.
The language was updated 3 major times in 2011, 2014, and 2017
to C++11, C++14, and C++17.
Get Started
This tutorial will teach you the basics of C++.
pg. 1
A compiler, like GCC, to translate the C++ code into a language that
the computer will understand
There are many text editors and compilers to choose from. In this
tutorial, we will use an IDE (see below).
Popular IDE's include Code::Blocks, Eclipse, and Visual Studio. These are
all free, and they can be used to both edit and debug C++ code.
C++ Quickstart
Let's create our first C++ file.
Open your IDE and go to File > New > Empty File.
Write the following C++ code and save the file
as myfirstprogram.cpp (File > Save File as):
myfirstprogram.cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Don't worry if you don't understand the code above - we will discuss it in
detail in later chapters. For now, focus on how to run the code.
Main Function
pg. 2
The main function is where the bulk of the code will go. For a program to run, it must
have a main function. Visual Studio starts a program with code that looks like this:
All the code that the main function runs goes between an opening curly bracket { and a
close ng curly bracket }
Building your solution tells the IDE to test for errors and compile the
code. Compiling converts your code from C++ to complex machine code that the
computer can run.
Variable names
Understanding variables and how they work is critical to C++. A variable is like a chest
or a container that can be filled with different values. The containers are names so they
can be found later.
When a type of variable is specified, like a string, doing so tells the program that this
chest can only be filled with words, phrases, and multiple characters. It won't be able to
hold numbers, true/false values, and so on.
Variable types
In C++, there are different types of variables (defined with different
keywords), for example:
pg. 3
double - stores floating point numbers, with decimals, such as 19.99
or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
string - stores text, such as "Hello World". String values are
surrounded by double quotes
bool - stores values with two states: true or false
Syntax
type variableName = value;
Example
int myNum = 15;
cout << myNum;
To change a variable, simply write the variable name and a string value. Whatever value
was written before will now be overwritten by the new value.
2)Char
A char is a single letter or symbol you can type, enclosed in single quotation marks.
char a = 'a';
char exclamation = '!';
3)Int
Int is short for integer, which is a whole number.
4)Double
Doubles and floats are numbers with a decimal. A double is more precise than a float; it
can hold more numbers before and after the decimal.
5)Bool
Bool is short for boolean. It represents a value that's true or false.
pg. 5
Arithmetic operators
Arithmetic operators are crucial to games. For example, when players endure damage or
earn experience points, totals need to be calculated in some fashion.
C++ arithmetic operators read from left to right and follow the normal mathematical
order of operations. Some basic operators and their order of precedence are listed in the
table below.
Arithmetic operators can be performed on both variables and literals (data typed in
directly).
Example Games
There are a wealth of options in terms of games that can be coded with C++. Here are
just a few examples to get coders thinking about what they might want to create!
Pong
Pong: a true gaming icon and one of the earliest arcade games. Using a couple UI
elements in SFML, a simple single-player Pong game can be created.
pg. 6
Tetris
Tetris is a classic puzzle game where you try to position falling blocks to create lines
without building over the top of the board. Tetris has some interesting programming
challenges involved in creating it.
Checkers
Creating a new game from scratch can seem like a daunting task. However, with good
planning, you can break a project into smaller pieces. (Stay tuned for a tutorial on how to
create your own checkers game with C++!
C++ Comments
pg. 7
Comments can be used to explain C++ code, and to make it more
readable. It can also be used to prevent execution when testing
alternative code. Comments can be singled-lined or multi-lined.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler
(will not be executed).
Example
// This is a comment
cout << "Hello World!";
Example
cout << "Hello World!"; // This is a comment
Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World!";
pg. 8