#C 2
#C 2
#C 2
I am Saret Eang
ICT Teacher at NGS.
You can find me at @Saret Eang
2
Getting Started with C++
To start using C++, you need two things:
4
Resource IDE
https://www.eclipse.org/ide/
http://www.codeblocks.org/
https://visualstudio.microsoft.com/
https://www.sublimetext.com/3
5
Example for single file
Using sublime IDE to run C++, Write some code then Command + B to build, Choose
c++ single file run
https://www.youtube.com/
watch?v=Kk9WLeSW8Pw
6
Code to config
{
"shell_cmd": "g++ -std=c++14 \"${file}\" -o
\"${file_path}/${file_base_name}\" &&
\"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -std=c++14 \"${file}\" -o
\"${file_path}/${file_base_name}\" &&
\"${file_path}/${file_base_name}\""
}
]
}
7
Want big impact?
Use big image.
8
Get Start Coding
9
C++ Syntax
Line 1: #include <iostream> is a
header file library that lets us
work with input and output
objects, such as cout (used in line
5). Header files add functionality
to C++ programs.
11
C++ Syntax
Note: Every C++ statement ends with a
semicolon ;.
13
C++ Output (Print Text)
The cout object, together with the
<< operator, is used to output
values/print text:
14
Welcome to C++
To continue - Section 2
16
C++ New Lines
17
C++ Comments
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.
Any text between // and the end of the line is ignored by the compiler (will not be
executed).
18
C++ Multi-line Comments
19
C++ Variables
Variables are containers for storing data values.
In C++, there are different types of variables (defined with different keywords), for
example:
● int - stores integers (whole numbers), without decimals, such as 123 or -123
● 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
20
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
type variable = value;
Where type is one of C++ types (such as int), and variable is the name of the variable
(such as x or myName). The equal sign is used to assign values to the variable.
To create a variable that should store a number, look at the following example:
Create a variable called myNum of type int and assign it the value 15:
21
Declaring (Creating) Variables
You can also declare a variable without assigning the value, and assign the value later:
Note that if you assign a new value to an existing variable, it will overwrite the previous
value:
22
Other Types
A demonstration of other data types:
23
Display Variables
The cout object is used together with the << operator to display variables.
To combine both text and a variable, separate them with the << operator:
24
Add Variables Together
Sum of 5 and 8 is 11
25
Exercise:
Sample Output:
-----------------------------------
26
To continue - Section 3
To declare more than one variable of the same type, use a comma-separated list:
27
C++ Identifiers
All C++ variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
28
C++ Identifiers
The general rules for constructing names for variables (unique identifiers) are:
29
C++ Constants
When you do not want others (or yourself) to override existing variable values, use the
const keyword (this will declare the variable as "constant", which means unchangeable
and read-only):
S = PIR2
30
រកផ្ទៃ ក្កឡា
R = 5cm
31
C++ Constants
You should always declare the variable as constant when you have values that are
unlikely to change:
32
C++ User Input
cin is a predefined variable that reads data from the keyboard with the extraction
operator (>>).
In the following example, the user can input a number, which is stored in the variable x.
Then we print the value of x:
33
Example: Circumference of a Circle
34
Example
35
Exercise
Write the program that can find:
1. Circumference of a Rectangular
2. Circumference of a Triangle
36
To continue - Section 4
37
Basic Data Types
The data type specifies the size and type of information the variable
will store:
38
C++ Numeric Data Types
Use int when you need to store a whole number without decimals, like 35 or
1000, and float or double when you need a floating point number (with
decimals), like 9.99 or 3.14515.
39
C++ Boolean Data Types
A boolean data type is declared with the bool keyword and can only take the values true or false.
When the value is returned, true = 1 and false = 0.
40
C++ Character Data Types
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
41