Elements of C++-1
Elements of C++-1
C++ PROGRAMMING
Example 2 – 1:
From Example 1 – 1, we design an
algorithm to find the perimeter and
area of a rectangle. Given the
length and the width of a rectangle,
the C++ program in this example
computes and displays the perimeter
and area.
INTRODUCTION
• A computer program, or a
program, is a sequence of
statements whose objective is to
accomplish a task.
• Programming is a process of
planning and creating a program.
A QUICK LOOK AT A C++ PROGRAM
A QUICK LOOK AT A C++ PROGRAM
Sample Run
A QUICK LOOK AT A C++ PROGRAM
#include <iostream>
allows us to use the (predefined
object) cout to generate output
and the (manipulator) endl.
A QUICK LOOK AT A C++ PROGRAM
int main ()
This is the heading of the function
main. The next line consists of a left
brace. This marks the beginning of the
(body) of the function main. The right
brace (at the last line of the program)
matches this left brace and marks the end
of the body of the function main.
A QUICK LOOK AT A C++ PROGRAM
• Comments
- can be used to identify the authors of
the program, give the date when the
program is written or modified, give
a brief explanation of the program,
and explain the meaning of key
statements in a program
THE BASICS OF A C++ PROGRAM
• Comments
- are for reader, not for the compiler
- when a compiler compiles a
program to check for the syntax
errors, it completely ignores
comments.
- two type of comments: single-line
and multiple-line comments
THE BASICS OF A C++ PROGRAM
• Single – Line Comments
- single-line comments begin with //
and can be placed anywhere in the
line
- everything encountered in that line
after // is ignored by the compiler
cout << “7 + 8 = ” << 7 + 8 << endl; //prints: 7 + 8 = 15
THE BASICS OF A C++ PROGRAM
• Special Symbols
- the smallest individual unit of a
program written in any language is
called a token.
- C++’s tokens are divided into
special symbols, word symbols, and
identifiers.
THE BASICS OF A C++ PROGRAM
• Special Symbols
- the following are some of the special
symbols:
+ - *
/ (mathematical symbols)
. ; ?
, (punctuation marks)
<= != ==
>= (two characters regarded as a single symbol)
THE BASICS OF A C++ PROGRAM
• Special Symbols
- commas are used to separate items
in
- semicolons are used to end a C++
statement
- blank is also a special symbol
created by pressing a space bar (only
once) on the keyboard
THE BASICS OF A C++ PROGRAM
• Reserved Words (Keywords)
- a second category of tokens reserved
word symbols
- some of the reserved word symbols
include the following:
int, float, double, char, const, void, return
- reserved words are also called
keywords
THE BASICS OF A C++ PROGRAM
• Reserved Words (Keywords)
- the letters that make up a reserved word
are always lowercase
- like special symbols, each is considered
to be a single symbol
- reserved words cannot be redefined
within any program; they cannot be used
for anything other than their intended
use
THE BASICS OF A C++ PROGRAM
• Reserved Words (Keywords)
THE BASICS OF A C++ PROGRAM
• Identifiers
- are names of things that appear in
programs, such as variables,
constants, and functions
- all identifiers must obey C++’s
rules for identifiers
THE BASICS OF A C++ PROGRAM
• Identifiers
- a C++ identifier consists of letters,
digits, and the underscore character
(_) and must begin with a letter or
underscore
- some identifiers are predefined;
others are defined by the user
THE BASICS OF A C++ PROGRAM
• Identifiers
- can be made of only letters, digits,
and the underscore character; no
other symbols are permitted to form
an identifier
THE BASICS OF A C++ PROGRAM
Example:
The following are legal identifiers in
C++:
first
conversion
payRate
counter1
THE BASICS OF A C++ PROGRAM
Examples of illegal identifiers in C+
+:Illegal Identifier Reason A Correct Identifier
employee Salary There can be no employeeSalary
space between
employee and
salary
Hello! The exclamation Hello
mark cannot be used
in an identifier.
one+two The symbol + cannot onePlusTwo
be used in an
identifier.
2nd An identifier cannot second
begin with a digit.
THE BASICS OF A C++ PROGRAM
Note:
Compiler vendors usually begin
certain identifiers with an underscore (_).
When the linker links the object program
with the system resources provided by the
integrated development environment
(IDE),certain errors could occur.
Therefore, it is advisable that you should
not begin identifiers in your program with
an underscore (_).
THE BASICS OF A C++ PROGRAM
• Whitespaces
- every C++ program contains
whitespaces
- it include blanks, tabs, and newline
characters
- In C++, they are used to separate
special symbols, reserved words,
and identifiers.
THE BASICS OF A C++ PROGRAM
• Whitespaces
- are nonprintable in the sense that
when they are printed on a white
sheet of paper, the space between
special symbols, reserved words,
and identifiers is white.
- proper utilization of whitespaces in a
program is important.
THE BASICS OF A C++ PROGRAM
• Data Types
- Data type is a set of values together
with a set of allowed operations.
- C++ data types fall into the
following three categories:
Simple data type
Structured data type
Pointers
THE BASICS OF A C++ PROGRAM
• Simple Data Types
- is the fundamental data type in C++
because it becomes a building block
for the structured data type
- Three categories of simple data:
Integral
Floating-point
Enumeration
THE BASICS OF A C++ PROGRAM
• Note:
The enumeration type is C++’s
method for allowing programmers to
create their own simple data types.
Integral data types are further
classified into the following
categories:
char, short, int, long, bool, unsigned char,
unsigned short, unsigned int, unsigned long, long
long, and unsigned long long.
THE BASICS OF A C++ PROGRAM
Values and Memory Allocation for Simple Data Types
Data Type Values Storage (in bytes)
int -2147483648 (= – 231) to 4
2147483648 (=231–1)
bool true and false 1
Char -128 (= – 27 ) to 127 (= 27 – 1) 1
long long -9223372036854775808 (–263) to 64
9223372036854775807 (263 – 1)
For example:
counter = 5;
interestRate = 0.05;
grade = ‘A’;
The first statement stores 5 in the
variable counter, the second statement
stores 0.05 in interestRate, and the
third statement stores the character ‘A’
in grade.
ARITHMETIC OPERATORS, OPERATOR PRECEDENCE, AND
EXPRESSIONS
Example 2 – 2:
Given length in inches, write a
program that determines and outputs
the equivalent length in feet and
(remaining) inches.
ARITHMETIC OPERATORS, OPERATOR PRECEDENCE, AND
EXPRESSIONS
Example 2 – 2:
Example:
–5, 8 – 7, 3 + 4, 2 + 3 * 5, 5.6 + 6.2 *
3, and x + 2 * 5 + 6 / y where x and y
are unknown numbers are called
arithmetic expressions.
ARITHMETIC OPERATORS, OPERATOR PRECEDENCE, AND
EXPRESSIONS
Example
Consider the following C++ integral
expressions:
12.8 * 17.5 – 34.50
x * 10.5 + y – 16.2
In these expressions, x and y are
variables of type double.
ORDER OF PRECEDENCE
Mixed Expressions
- an expression that has operands of
different data types
- contains both integers and
floating-point numbers
Examples:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
ORDER OF PRECEDENCE
Two rules apply when evaluating a
mixed expression:
1. When evaluating an operator in
a mixed expression:
a. If the operator has the same
types of operands, the operator
is evaluated according to the
type of the operands
ORDER OF PRECEDENCE
Two rules apply when evaluating a
mixed expression:
1. When evaluating an operator in
a mixed expression:
b. If the operator has both types of
operands, then during calculation,
the integer is changed to a floating-
point number with the decimal part
of zero and the operator is
evaluated.
ORDER OF PRECEDENCE
2. The entire expression is evaluated
according to the precedence rules;
the multiplication, division, and
modulus operators are evaluated
before the addition and subtraction
operators. Operators having the
same level of precedence are
evaluated from left to right.
Grouping using parentheses is
allowed for clarity.
ORDER OF PRECEDENCE
Two rules apply when evaluating a
mixed expression:
1. When evaluating an operator in
a mixed expression:
a. If the operator has the same
types of operands, the operator
is evaluated according to the
type of the operands