0% found this document useful (0 votes)
7 views

Introduction To C++

Uploaded by

henryshmrri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction To C++

Uploaded by

henryshmrri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Introduction to

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.

• C++ is regarded as a middle-level language, as


it comprises a combination of both high-level
and low-level language features.

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.

• C++ is being highly used to write device drivers


and other software that rely on direct
manipulation of hardware under real-time
constraints.
5
Overview
• C++ is widely used for teaching and research
because it is clean enough for successful
teaching of basic concepts.

• Anyone who has used either an Apple Macintosh


or a PC running Windows has indirectly used C+
+ because the primary user interfaces of these
systems are written in C++.

6
Overview

• C++ is one of the


programming language
used to create these
games.

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.

2. The line using namespace std; tells the compiler


to use the std namespace. Namespaces are a
relatively recent addition to C++.
10
C++ Program Structure / Syntax
3. The line int main() is the main function where
program execution begins.

5. The next line cout << “Hello World"; causes the


message “Hello World" to be displayed on the
screen.

6. The next line return 0; terminates main()


function and causes it to return the value 0 to the
calling process.
11
Semicolons & Blocks in C++
In C++, the semicolon is a statement terminator.
That is, each individual statement must be ended
with a semicolon. It indicates the end of one
logical entity.

A block is a set of logically connected statements


that are surrounded by opening and closing
braces.

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.

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.
20
C++ Variables
• 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.

21
Declaring Variables
• To create/declare a variable, specify the type
and assign it a value.

where type is one of C++ types (such as int), and


variableName is the name of the variable (such
as x or myName). The equal sign is used to assign
values to the variable.
22
Declaring Variables
• To create a variable that should store a number,
look at the following example:

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.

float and double are used


when you need a floating-
point number (with decimals),
like 9.99 or 3.14515.

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”.

To use strings, you must


include an additional header
file in the source code,
the <string> library.

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 ( << ).

cin is pronounced "see-in". Used for input, and


uses the extraction operator / bitwise shift right
( >> ).

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):

From this example, you


would expect the
program to print "John
Doe", but it only prints
"John".

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.

C++ divides the operators into the following


groups:
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Bitwise operators 50
Arithmetic Operators

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.

The return value of a comparison is either 1 or 0


, which means true (1) or false (0).
These values are known as Boolean values.

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.

If you add two numbers, the result will be a


number:

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:

The solution to avoid this problem, is to use the


backslash escape character.
The backslash ( \ ) escape character turns
special characters into string characters.
64
String – Special Characters

The sequence \" inserts a double quote in a string:

65

You might also like