Introduction to C++
Julian Thijssen
Computer Graphics and Visualization
TU Delft
1
Common Programming Languages
• C
• C++
• C#
• Java
• Python
• JavaScript
• Perl
• Ruby
• Rust
2
C++ Use In Graphics
3
What is a computer program?
• Source code – Human-readable code defining a program
4
What is a computer program?
• Source code – Human-readable code defining a program
5
What is a computer program?
• Machine code – Language understood by the computer
6
What is a computer program?
Source code Machine code
7
Programming is data manipulation
8
Programming is data manipulation
9
Primitive Data Types
• Integer (Whole numbers)
• Floating point (Fractional numbers)
• Character (Letters, Digits, Symbols)
• String (String of Characters)
• Boolean (True or False)
10
Example Primitive Data Types
• Integer (5, 8, 294, -7594) New-line
• Float (5.344, 9.2, -84.0)
• Character (‘e’, ‘K’, ‘6’, ‘$’, ‘\n’)
• String (“Pie”, “50% Off”, “Learn C++”)
• Boolean (True, False)
11
Data Types in C++
• int – Integer
• float – Floating point
• char – Character
• std::string – String
• bool - Boolean
12
Storing data in memory
Data can be stored in memory for later use
Now how do we get it back when we need it??
13
Variables
• Has a name
• Represents some location in memory
Put data in memory Label the memory location
Retrieve data from labelled location
14
Variables
15
A Basic C++ Application
Return success
16
A Basic C++ Application
17
A Basic C++ Application
Type Identifier Value
18
A Basic C++ Application
Assignment End of
Operator Statement
Type Identifier Value
19
Statements
• Instruction to perform some action
• Ends in a semicolon
Operators
Assignment Operators:
• Assignment (=)
20
A Basic C++ Application
Assignment End of
Operator Statement
Type Identifier Value
21
Output
22
Output Include output functionality
23
Streams
• Stream – Sequence of data flowing in
and out of the program
• Cout – Standard output stream
• Cin – Standard input stream
• Stream insertion operator (<<)
Stream extraction operator (>>)
24
Output Include output functionality
25
Operators
Arithmetic Operators:
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulo (%)
26
Output
27
Exercise 1
Write a program that:
1. Outputs the number 10
2. Outputs the first 4 digits of PI (3.141)
3. Calculates the result of 312 * 40
4. Calculates the result of 100 + 40 * (61 / 9) + 7
5. Stores the previous result and calculates and displays
the result multiplied by 100.
28
Declaration and Initialization
• Declaration – Declaring the type of the variable
• Initialization – Setting of initial value
29
Input
Stream extraction operator
Stream insertion operator
30
Conditions
31
Conditions
32
Conditions
33
Conditions
Condition
34
Conditions
Condition
35
Operators
Relational Operators:
• Equal to (==)
• Not equal to (!=)
• Less than (<)
• Greater than (>)
• Less than or equal to (<=)
• Greater than or equal to (>=)
36
Conditions
37
Scope and Block Structure
38
Scope and Block Structure
• Global Variables – Variables defined outside all blocks
• Local Variables - Variables defined within a block
39
Exercise 2
Write a program that:
1. Allows the user to type a number and displays it
2. Allows the user to type a number, and displays whether
this number is positive or negative.
3. Allows the user to type a number, and displays whether
this number is greater than or equal to -10.
4. Allows the user to type a number, and displays “In
Range” when the number is bigger than 10 and smaller
than 20, otherwise “Out Of Range”.
40
Loops
41
While Loops
42
For Loops
43
For Loops
Increment operator
44
Exercise 3
Write a program that:
1. Displays the first 10 natural numbers
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
2. Finds the sum of these numbers
3. Displays the cube of a user-inputted
number. (e.g. 5^3 = 125)
4. Displays the factorial of a user-inputted
number. (e.g. 5 factorial = 120)
45