Introduction to C++. Programming

Содержание

Слайд 2

OBJECTIVES
In this lecture you will learn:
To write simple computer programs in

OBJECTIVES In this lecture you will learn: To write simple computer programs
C++.
To write simple input and output statements.
To use fundamental types.
Basic computer memory concepts.

Слайд 3

Introduction

C++ Programming
Facilitates disciplined approach to computer program design
Programs process information and

Introduction C++ Programming Facilitates disciplined approach to computer program design Programs process
display results
Examples Demonstrate
How to display messages
How to obtain information from the user

Слайд 4

First Program in C++: Printing a Line of Text

Simple Program
Prints a line

First Program in C++: Printing a Line of Text Simple Program Prints
of text
Illustrates several important features of C++

Слайд 5

Outline

fig02_01.cpp (1 of 1) fig02_01.cpp output (1 of 1)

Outline fig02_01.cpp (1 of 1) fig02_01.cpp output (1 of 1)

Слайд 6

Good Programming Practice
Every program should begin with a comment that describes

Good Programming Practice Every program should begin with a comment that describes
the purpose of the program, author, date and time.

Слайд 7

Common Programming Error

Forgetting to include the header file in a

Common Programming Error Forgetting to include the header file in a program
program that inputs data from the key­board or outputs data to the screen causes the compiler to issue an error message, because the compiler cannot recognize references to the stream components (e.g. cout).

Слайд 8

Common Programming Error
Syntax errors are also called compiler errors, compile-time errors

Common Programming Error Syntax errors are also called compiler errors, compile-time errors
or compilation errors, because the compiler detects them during the compilation phase. You will be unable to execute your program until you correct all the syntax errors in it. As you will see, some compilation errors are not syntax errors.

Слайд 11

Tokens: The smallest individual units of a program are called tokens.

Constants
Variables
Keywords
Data Types
A

Tokens: The smallest individual units of a program are called tokens. Constants
C++ program is written using these tokens, white spaces , and the syntax of the language.

Слайд 12

Constants , Identifiers and Keywords
The alphabets , numbers and special symbols when

Constants , Identifiers and Keywords The alphabets , numbers and special symbols
properly combined form constants , identifiers and keywords.
Constant: a constant is a quantity that does not change. This can be stored at a location in memory of computer.
Variable(identifiers) : is considered as a name given to the location in memory where this constant is stored. Naturally the contents of the variable can change. There are fundamental requirement of any language. Each language has its own rules for naming these identifiers.

Слайд 13

Following are the rules for naming identifiers:
Only alphabetic characters digits and underscores

Following are the rules for naming identifiers: Only alphabetic characters digits and
are permitted.
The name cannot start with a digit.
Uppercase and Lowercase letters are distinct
A declared keyword cannot be used as a variable name.
For Example:
3X + Y = 20

Слайд 14

Keywords

Keywords implement specific C++ language features.
They are explicitly reserved identifiers and

Keywords Keywords implement specific C++ language features. They are explicitly reserved identifiers
cannot be
used as names for the program variables or other
user defined program elements.

Слайд 16

Data Type
Data Type : type of data to be stored in a

Data Type Data Type : type of data to be stored in
variable
Primitive data type (built-in data type): provided as an integral part of the language
Integer type
Real type

int val;

Слайд 17

Primitive Data Type

Primitive Type

I
N
T
E
G
E
R

R
E
A
L

or more

big

or more

Primitive Data Type Primitive Type I N T E G E R

Слайд 18

Primitive Data Type

Why do we need to define the type of data?
Efficient

Primitive Data Type Why do we need to define the type of
use of memory space
Data loss can happen when store big data into small memory space

Слайд 19

Primitive Data Type

sizeof operator
Return memory size of operand in byte
Need () when

Primitive Data Type sizeof operator Return memory size of operand in byte
the operand is data type
Otherwise () is optional

#include
using namespace std;
int main(void)
{
int val=10;
cout << sizeof val << endl;// print memory size
cout << sizeof(int) << endl;// print int data type
return 0;
}

Слайд 20

Primitive Data Type

Criteria of selection of data type
Real type data
Accuracy
‘double’ is common

Primitive Data Type Criteria of selection of data type Real type data Accuracy ‘double’ is common

Слайд 21

Primitive Data Type

Example

#include
#include
using namespace std;
int main(void)
{
double radius;
double area;
cout <<

Primitive Data Type Example #include #include using namespace std; int main(void) {
"Input radius of circle" << endl;
cin >> radius;
area = radius * radius * 3.1415;
cout << area;
return 0;
}

Слайд 22

Primitive Data Type

unsigned: the range of data is changed
Positive integer only
Can

Primitive Data Type unsigned: the range of data is changed Positive integer
not be used in real data type

Слайд 23

Primitive Data Type

How to express letter (including characters, notations, …) inside computer?

Primitive Data Type How to express letter (including characters, notations, …) inside

ASCII (American Standard Code for Information Interchange) code was born for expressing letters.
Defined by ANSI (American National Standard Institute)
The standard of letter expression by computer
Ex) letter ‘A’ ? 65, letter ‘B’ ? 66

Слайд 24

Primitive Data Type

Range of ASCII code
0 ~ 127, ? possible using ‘char’

Primitive Data Type Range of ASCII code 0 ~ 127, ? possible
type
Declare ‘char’
Expression of letters
‘’ (quotation mark)

Слайд 25

Primitive Data Type

Example

#include
using namespace std;
int main(void)
{
char ch1='A';
char ch2=65;
cout << ch1

Primitive Data Type Example #include using namespace std; int main(void) { char
< cout << (int)ch1 << endl << (int)ch2 < return 0;
}

Слайд 26

Primitive Data Type

ASCII code

Primitive Data Type ASCII code

Слайд 27

Symbolic Constant

Make ‘variable’ to ‘constant’

#include
using namespace std;
int main(void)
{
const int

Symbolic Constant Make ‘variable’ to ‘constant’ #include using namespace std; int main(void)
MAX = 100;
const double PI = 3.1415;
return 0;
}

Слайд 28

Another C++ Program: Adding Integers

Variables
Location in memory where value can be

Another C++ Program: Adding Integers Variables Location in memory where value can
stored
Common data types (fundamental, primitive or built-in)
int – integer numbers
char – characters
double – floating point numbers
Declare variables with name and data type before use
int integer1;
int integer2;
int sum;

Слайд 29

Another C++ Program: Adding Integers (Cont.)

Variables (Cont.)
Can declare several variables of same

Another C++ Program: Adding Integers (Cont.) Variables (Cont.) Can declare several variables
type in one declaration
Comma-separated list
int integer1, integer2, sum;
Variable names
Valid identifier
Series of characters (letters, digits, underscores)
Cannot begin with digit
Case sensitive (upper and lower case letter)
Keywords

Слайд 30

Outline

fig02_05.cpp
(1 of 1)
fig02_05.cpp output (1 of 1)

Declare integer variables

Use stream extraction

Outline fig02_05.cpp (1 of 1) fig02_05.cpp output (1 of 1) Declare integer
operator with standard input stream to obtain user input

Stream manipulator std::endl outputs a newline, then “flushes output buffer”

Concatenating, chaining or cascading stream insertion operations

Слайд 31

Good Programming Practice

Place a space after each comma (,) to make

Good Programming Practice Place a space after each comma (,) to make programs more readable.
programs more readable.

Слайд 32

Good Programming Practice

Some programmers prefer to declare each variable on a

Good Programming Practice Some programmers prefer to declare each variable on a
separate line. This format allows for easy insertion of a descriptive comment next to each declaration.

Слайд 33

Portability Tip

C++ allows identifiers of any length, but your C++ implementation

Portability Tip C++ allows identifiers of any length, but your C++ implementation
may impose some restrictions on the length of identifiers. Use identifiers of 31 characters or fewer to ensure portability.

Слайд 34

Good Programming Practice

Choosing meaningful identifiers helps make a program self-documenting—a person

Good Programming Practice Choosing meaningful identifiers helps make a program self-documenting—a person
can understand the program simply by reading it rather than having to refer to manuals or comments.

Слайд 35

Good Programming Practice

Always place a blank line between a declaration and

Good Programming Practice Always place a blank line between a declaration and
adjacent executable statements. This makes the declarations stand out in the program and contributes to program clarity.

Слайд 36

Another C++ Program: Adding Integers (Cont.)

Input stream object
std::cin from
Usually connected to

Another C++ Program: Adding Integers (Cont.) Input stream object std::cin from Usually
keyboard
Stream extraction operator >>
Waits for user to input value, press Enter (Return) key
Stores value in variable to right of operator
Converts value to variable data type
Example
std::cin >> number1;
Reads an integer typed at the keyboard
Stores the integer in variable number1

Слайд 37

Another C++ Program: Adding Integers (Cont.)

Assignment operator =
Assigns value on left

Another C++ Program: Adding Integers (Cont.) Assignment operator = Assigns value on
to variable on right
Binary operator (two operands)
Example:
sum = variable1 + variable2;
Add the values of variable1 and variable2
Store result in sum
Stream manipulator std::endl
Outputs a newline
Flushes the output buffer

Слайд 38

Another C++ Program: Adding Integers (Cont.)

Concatenating stream insertion operations
Use multiple stream insertion

Another C++ Program: Adding Integers (Cont.) Concatenating stream insertion operations Use multiple
operators in a single statement
Stream insertion operation knows how to output each type of data
Also called chaining or cascading
Example
std::cout << "Sum is " << number1 + number2 << std::endl;
Outputs "Sum is “
Then, outputs sum of number1 and number2
Then, outputs newline and flushes output buffer

Слайд 39

Memory Concept

Variable names
Correspond to actual locations in computer's memory
Every variable has

Memory Concept Variable names Correspond to actual locations in computer's memory Every
name, type, size and value
When new value placed into variable, overwrites old value
Writing to memory is destructive
Reading variables from memory nondestructive
Example
sum = number1 + number2;
Value of sum is overwritten
Values of number1 and number2 remain intact

Слайд 40

Fig. 2.6 | Memory location showing the name and value of variable

Fig. 2.6 | Memory location showing the name and value of variable number1.
number1.

Слайд 41

Fig. 2.7 | Memory locations after storing values for number1 and number2.

Fig. 2.7 | Memory locations after storing values for number1 and number2.

Слайд 42

Fig. 2.8 | Memory locations after calculating and storing the sum of

Fig. 2.8 | Memory locations after calculating and storing the sum of number1 and number2.
number1 and number2.

Слайд 43

Arithmetic

Arithmetic operators
*
Multiplication
/
Division
Integer division truncates remainder
7 / 5 evaluates to

Arithmetic Arithmetic operators * Multiplication / Division Integer division truncates remainder 7
1
%
Modulus operator returns remainder
7 % 5 evaluates to 2

Слайд 44

Common Programming Error

Attempting to use the modulus operator (%) with non integer

Common Programming Error Attempting to use the modulus operator (%) with non
operands is a compilation error.

Слайд 45

Arithmetic (Cont.)

Straight-line form
Required for arithmetic expressions in C++
All constants, variables and operators

Arithmetic (Cont.) Straight-line form Required for arithmetic expressions in C++ All constants,
appear in a straight line
Grouping subexpressions
Parentheses are used in C++ expressions to group subexpressions
Same manner as in algebraic expressions
Example
a * ( b + c )
Multiple a times the quantity b + c

Слайд 46

Fig. 2.9 | Arithmetic operators.

Fig. 2.9 | Arithmetic operators.

Слайд 47

2.6 Arithmetic (Cont.)

Rules of operator precedence
Operators in parentheses evaluated first
Nested/embedded parentheses
Operators in

2.6 Arithmetic (Cont.) Rules of operator precedence Operators in parentheses evaluated first
innermost pair first
Multiplication, division, modulus applied next
Operators applied from left to right
Addition, subtraction applied last
Operators applied from left to right

Слайд 48

Common Programming Error 2.4

Some programming languages use operators ** or ^

Common Programming Error 2.4 Some programming languages use operators ** or ^
to represent exponentiation. C++ does not support these exponentiation operators; using them for exponentiation results in errors.
Use pow(A, B) = A^B function in C++

Слайд 49

Fig. 2.11 | Order in which a second-degree polynomial is evaluated.

Fig. 2.11 | Order in which a second-degree polynomial is evaluated.

Слайд 50

Decision Making: Equality and Relational Operators

Condition
Expression can be either true or false
Can

Decision Making: Equality and Relational Operators Condition Expression can be either true
be formed using equality or relational operators
if statement
If condition is true, body of the if statement executes
If condition is false, body of the if statement does not execute

Слайд 51

Fig. 2.12 | Equality and relational operators.

Fig. 2.12 | Equality and relational operators.

Слайд 52

Common Programming Error 2.5

A syntax error will occur if any of

Common Programming Error 2.5 A syntax error will occur if any of
the operators ==, !=, >= and <= appears with spaces between its pair of symbols.

Слайд 53

Common Programming Error

Reversing the order of the pair of symbols in

Common Programming Error Reversing the order of the pair of symbols in
any of the operators !=, >= and <= (by writing them as =!, => and =<, respectively) is normally a syntax error. In some cases, writing != as =! will not be a syntax error, but almost certainly will be a logic error that has an effect at execution time. (cont…)

Слайд 54

Outline

fig02_13.cpp
(1 of 2)

using declarations eliminate need for std:: prefix

Can write cout

Outline fig02_13.cpp (1 of 2) using declarations eliminate need for std:: prefix
and cin without std:: prefix

Declare variables

if statement compares values of number1 and number2 to test for equality

If condition is true (i.e., values are equal), execute this statement

if statement compares values of number1 and number2 to test for inequality

If condition is true (i.e., values are not equal), execute this statement

Compares two numbers using relational operator < and >

Слайд 55

Outline

fig02_13.cpp
(2 of 2)
fig02_13.cpp output (1 of 3)
(2 of 3)
(3 of 3)

Outline fig02_13.cpp (2 of 2) fig02_13.cpp output (1 of 3) (2 of 3) (3 of 3)
Имя файла: Introduction-to-C++.-Programming.pptx
Количество просмотров: 42
Количество скачиваний: 0