Introduction To C++
Introduction To C++
C++ Programming
Language
1 OVERVIEW
2
Overview
• C++ is a statically typed, compiled, general-
purpose, case-sensitive, free-form programming
language that supports procedural, object-
oriented, and generic programming.
3
Overview
• C++ was developed by Bjarne Stroustrup
starting in 1979 at Bell Labs in Murray Hill, New
Jersey, as an enhancement to the C language
and originally named C with Classes but later it
was renamed C++ in 1983.
• C++ is a superset of C, and that virtually any
legal C program is a legal C++ program.
4
Overview
Use of C++:
• C++ is used by hundreds of thousands of
programmers in essentially every application
domain.
6
Overview
7
1 BASIC SYNTAX
8
C++ Program Structure / Syntax
9
C++ Program Structure / Syntax
Let us look at the various parts of the above
program:
1. The C++ language defines several headers,
which contain information that is either necessary
or useful to your program. For this program, the
header <iostream> is needed.
12
2 C++ Output
13
C++ Output (Print Text)
The cout object, together with the << (bitwise
shift left) operator, is used to output values/print
text.
14
3
C++
Comments
15
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.
• Single-lined 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). 16
C++ Comments
Example of Single-lined comments:
17
C++ Comments
• Multi-lined Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the
compiler.
18
4 C++ Variables
19
C++ Variables
Variables are containers for storing data values.
21
Declaring Variables
• To create/declare a variable, specify the type
and assign it a value.
23
Declaring Variables
• To create a variable for the other types, look at
the following example:
24
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.
25
26
Declare many variables
To declare more than one variable of the same
type, use a comma-separated list:
27
One Value to Multiple Variables
Assign the same value to multiple variables in one
line:
28
5
C++ Data
Types
29
Data Types
As explained in the Variables chapter, a variable in
C++ must be a specified data type.
30
Data Types
Integer (int) is used when
you need to store a whole
number without decimals.
32
Data Types
Boolean (bool) can only take the values TRUE
or FALSE.
When the value is returned, true = 1 and false
= 0.
33
Data Types
Character (char) is used to store a single
character. The character must be surrounded by
single quotes, like 'A' or 'c’.
35
Data Types
string is used to store a sequence of characters
(text). String values must be surrounded by
double quotes. Example, “Hello World”.
36
6 C++ New Lines
38
New Lines
To insert a new line, you can use the \n
character.
39
New Lines
To insert a new line between variables, you can
use the ‘\n’ character. The character should be
in a single-quote.
40
7
C++ User
Input
41
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 :
42
Good to Know
cout is pronounced "see-out". Used for output,
and uses the insertion operator / bitwise shift
left ( << ).
43
44
8
User Input
Strings
45
User Input Strings
It is possible to use the extraction operator >>
on cin to store a string entered by a user:
46
User Input Strings
However, cin considers a space (whitespace,
tabs, etc) as a terminating character, which
means that it can only store a single word (even
if you type many words):
47
User Input Strings
That's why, when working with strings, we often
use the getline() function to read a line of text.
It takes cin as the first parameter, and the string
variable as the second:
48
7 C++ Operators
49
Operators
Operators are used to perform operations on
variables and values.
51
Assignment Operators
Assignment operators are used to assign
values to variables.
In the example below, we use the assignment
operator ( = ) to assign the value 10 to a
variable called x:
52
Comparison Operators
Comparison operators are used to compare
two values (or variables). This is important in
programming, because it helps us to find
answers and make decisions.
53
Comparison Operators
54
Logical Operators
As with comparison operators, you can also test
for true ( 1 ) or false ( 0 ) values with logical
operators.
Logical operators are used to determine the
logic between variables or values.
55
Logical Operators
As with comparison operators, you can also test
for true ( 1 ) or false ( 0 ) values with logical
operators.
Logical operators are used to determine the
logic between variables or values.
56
7
String
Concatenation
57
String Concatenation
The + operator can be used between strings to
add them together to make a new string. This is
called concatenation.
58
String Concatenation
In the example earlier, we added a space after
firstName to create a space between John and
Doe on output. However, you could also add a
space with quotes ( " " or ' ‘ ).
59
String Concatenation
In the example earlier, we added a space after
firstName to create a space between John and
Doe on output. However, you could also add a
space with quotes ( " " or ' ‘ ).
60
Note!
C++ uses the + operator for both addition and
concatenation.
Numbers are added. Strings are concatenated.
61
Note!
If you add two strings, the result will be a string
concatenation:
62
String
7 Special
Characters
63
String – Special Characters
Because strings must be written within quotes, C+
+ will misunderstand this string, and generate an
error:
65