Documente Academic
Documente Profesional
Documente Cultură
1233:
Introduction to C++
by: ZARITA (FTMK)
LECTURE 3
1 (Sem 2, 15/16)
Learning Outcomes
At the end of this lecture, you should be able to:
Identify basic structure of C++ program (pg 3)
Describe the concepts of :
Character set. (pg 11)
Token (pg 13): keyword, identifiers, operator,
punctuation, string literal
Data type (pg 25)
Input function, output function (pg 33)
Operator (pg 37) : arithmetic operators & assignment
operators
Formatting the Output (pg 49)
2 LECTURE 3
Basic Structure of a C++ Program
/* This program computes the distance between two
points. */ comment
#include <iostream> // Required for cout, endl.
#include <cmath> // Required for sqrt()
using namespace std; // Tells which namespace to use
comments
// Define and initialize global variables.
double x1=1, y1=5, x2=4, y2=7;
3 LECTURE 3
Figure 3.1 Structure of a C++ Program
Structure of a C++ Program : Comments
Use to write document parts (notes) of the program.
Comments help people read programs:
Indicate the purpose of the program
Describe the use of variables
Explain complex sections of code
Are ignored by the compiler.
In C++ there are two types of comments.
Single-Line comments : Begin with // through to the end
of the line.
Multi-Line comments : Begin with /* and end with */
4 LECTURE 3
Single-Line Comments
Use to write just one line of comment :
Example :
int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area
5 LECTURE 3
Multi-Line Comments
Could span multiple lines:
/* this is a multi-line
comment
*/
Could begin and end on the same line:
int area; /* calculated area */
6 LECTURE 3
Structure of a C++ Program : Preprocessor
Directives
• Provide instructions to the compiler that are performed before the
program is translated.
• Begin with a pound sign (#)
• Do NOT place a semicolon (;) at the end of a preprocessor
directive line.
• Example:
#include <iostream>
• The #include directive instructs the compiler to include the
statements from file iostream into the program.
• The program needs <iostream> header file because it might need
to do input and/or output operations.
7
LECTURE 3
Structure of a C++ Program : using Directive
8 LECTURE 3
Structure of a C++ Program : Function
9 LECTURE 3
Structure of a C++ Program : Global/Local
Declarations Area
An identifier cannot be used before it is defined.
Consist of :
1. Number : 0 to 9
2. Alphabet : a to z and A to Z
3. Spacing
4. Special Character :
, . : ; ? ! ( ) {} ” ’ + - * / = > < # % & ^ ~ | / _
11
LECTURE 3
Special Characters
Character Name Meaning
// Double slash Beginning of a comment
# Pound sign Beginning of preprocessor
directive
< > Open and close Enclose header file name in
brackets #include
( ) Open and close Used when naming a
parentheses function
{ } Open and close Encloses a group of
brace statements
" " Open and close Encloses string of
quotation marks characters
; Semicolon End of a programming
statement
Token
Combination of characters, that consist of :
1. Reserved words/keywords
2. Identifiers (variable, constant, function name)
3. Punctuators
4. Operators
5. String Literal
13
LECTURE 3
Reserved word/ Keyword
A word that has special meaning in C++.
Used only for their intended purpose.
Keywords cannot be used to name identifiers.
All reserves word appear in lowercase.
14
Identifiers
Allows programmers to name data and other objects in the program :
variable, constant, function etc.
C++ is case-sensitive; this means that CASE, Case, case, and CaSe are
four completely different words.
15 LECTURE 3
Identifiers
A sum$ // $ is illegal
student_name 2names // can’t start with 2
_aSystemName stdnt Nmbr // can’t have
pi space
al int // can’t use reserved
stdntNm word
_anthrSysNm
PI
16 LECTURE 3
Identifiers : Constant
17 LECTURE 3
Constant: How to Define
How a constant is defined is reflected as Defined constant and
Memory constant
Defined constant
Placed at the preprocessor directive area.
Using the preprocessor command define prefaced with the pound
sign (#)
E.g #define SALES_TAXES_RATE .0825
The expression that follows the name (.0825) replaces the name
wherever it is found in the program.
Memory constant
Placed at global/local declaration area, depending on
constant’s scope. Add the type qualifier, const before the definition.
E.g. const double TAX_RATE = 0.0675;
18 const int NUM_STATES = 50; LECTURE 3
Constant : How to use
19 LECTURE 3
Identifiers : Variables
Variable is memory location/s that store values that can be modified.
Has a name and a type of data it can hold.
Must be defined before it can be used.
A variable name should reflect the purpose of the variable. For example:
itemsOrdered
The purpose of this variable is to hold the number of items ordered.
Once defined, variables are used to hold the data that are required by the
program from its operation.
Example:
double x1=1.0, x2=4.5, side1;
side1 = x2-x1;
x1, x2 and side1 are examples of variables that can be modified.
20 LECTURE 3
Variables
22 LECTURE 3
Variable initialization
Eg:
int count = 0;
int count , sum = 0; // Only sum is initialize.
int count=0 , sum = 0; OR int count =0; int sum = 0;
23 LECTURE 3
Punctuator
Operator
C++ uses a set of built in operators ( Eg : +, -, *, / etc).
There are several types of operators : arithmetic, assignment,
relational and logical.
24 LECTURE 3
Data types
Set of values for each type is known as the domain for the
type
25 LECTURE 3
Standard Data Type
26 LECTURE 3
Data types
C++ contains five standard data types
void
int (short for integers)
char (short for characters)
float ( short for floating points)
bool (short for boolean)
27 LECTURE 3
Data types
void
– Has no values and operations
– Both set of values are empty
char
– A value that can be represented by an alphabet, digit or
symbol
– A char is stored in a computer’s memory as an integer
representing the ASCII code.
– Usually use 1 byte of memory
28 LECTURE 3
Data types
int
– A number without a fraction part (round or integer)
– C++ supports three different sizes of integer
• short int
• int
• long int
float
A number with fractional part such as 43.32
C++ supports three types of float
float
double
long double
bool
− Boolean (logical) data
– C++ support bool type
– C++ provides two constant to be used
• True
• False
– Is an integral which is when used with other integral type
such as integer values, it converted the values to 1 (true) and
0 (false)
31 LECTURE 3
Determining the Size of a Data Type
33 LECTURE 3
Input / output function
Output function - cout
35 LECTURE 3
Input / output function
Substraction - d=b-2 3
Multiplication * d=a*b 20
Division / d = a/2 2
Modulus % d = b%3 2
37 LECTURE 3
Assignment Operators
• Assume int x=4, y=5, z=8;
Assignmen Sample Similar Value of variable
t Operator Expression Expression after assignment
+= x += 5 x=x+5 x=9
-= y -= x y=y-x y=1
*= x *= z x = x*z x=32
38 LECTURE 3
Increment and decrement Operators
Operator Called Sample Similar Explanation
Expression Expression
For example: assume k=5 prior to executing each of the following statement.
m = ++k; both m and k become 6
39 n = k--; n becomes 5, k becomes 4
Precedence of Arithmetic and
Assignment Operators
Precedence Operator Associativity
1 Parentheses: () Innermost first
40 LECTURE 3
A Closer Look at the / Operator
41 LECTURE 3
A Closer Look at the % Operator
42 LECTURE 3
Operator Precedence
Example 1: Example 2:
int a=10, b=20, c=15, d=8; int a=15, b=6, c=5, d=4;
a * b / (-c * 31 % 13) * d ; d *= ++b – a / 3 + c ;
int main()
{
int num1, num2;
int value_div, value_mod ;
#include <iostream>
using namespace std;
int main ( )
{
int a = 3, b = 4, c = 5, x, y;
cout << "Initial values of the variables:\n";
cout << "a = " << a << " b = " << b << "c = " << c <<endl;
cout << endl;
x = a * 4 + b / 2 - c * b;
cout << "Value of a * 4 + b / 2 - c * b is : "<< x <<endl;
y = --a * (3 + b) / 2 - c++ * b;
cout << "Value of --a * (3 + b) / 2 – c++ * b is: ";
cout << y << endl << endl;
cout << "Values of the variables now are :\n";
cout << "a = " << a << " b = " << b << " c = "<< c <<endl;
return 0;
}
45
Output :
Initial values of the variables :
a = 3 b = 4 c = 5
Value of a * 4 + b / 2 - c * b is : -6
Value of --a * (3 + b) / 2 – c++ * b is: -13
46
Formatting Output
For the input and output objects, these functions have been giv a
special name: manipulator.
Manipulators Use
48 LECTURE 3
Basic Command of Output : Escape Sequence
\n or endl New line Takes the cursor to the beginning of the next line
\v Vertical Tab Takes the cursor to the next tab stop vertically.
49 LECTURE 3
Example: LECTURE 3
int main( )
{
char letter;
int num;
float amount;
51 LECTURE 3