Basic elements in
programming
IT1134
Chapter-02
C++
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++ runs on a variety of platforms, such as Windows, Mac OS,
and the various versions of UNIX.
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.
C++
C++ IDE
Borland C++ is a C and C++ programming environment (that is,
an integrated development environment) for MS-DOS and
Microsoft Windows. It was the successor to Turbo C++, and
included a better debugger, the Turbo Debugger, which was
written in protected mode DOS.
Turbo C++ was a C++ compiler and integrated development
environment and computer language originally from Borland.
(often abbreviated as MSVC or VC++) is a
Microsoft Visual C++
commercial (free version available), integrated development
environment (IDE) product from Microsoft for the C, C++, and
C++/CLI programming languages. It features tools for developing
and debugging C++ code, especially code written for the Microsoft
Windows API, the DirectX API, and the Microsoft .NET Framework.
The Basics of a C++ Program
A C++ program is a collection of one or more
subprograms (functions)
Function
Collection of statements
Statements accomplish a task
Every C++ program has a function called main
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
return 0;
}
Source code or source program-A program written in a
high-level language
Preprocessor
Compiler
Object Program-The machine language version of the high-
level language program
Linker-A program that combines the object program with
other program in the library and is used in the program to
create the executable code.
Loader-A program that loads an executable program into
main memory.
Errors
Three kinds of errors:
1. Compile time errors
2. Runtime errors
3. Logical errors
Compile Time Errors
Errors that occur during compile
time
Two types of errors
Syntax errors
Semantic errors
Syntax Errors
Mistakes in the use of language‘s
grammar (or syntax)
• Usually not critical
• Usually discovered by compiler
• ...but sometimes hard to find !
Runtime Errors
• Occurring during program execution
• Critical if appearing in special cases
• Not discovered by compiler
• Forces the computer to exit program (if
no recovery code is written)
Common Runtime Errors
Logical Errors
• Most Critical Errors
• Occur in the design – phase
• Can’t be detected by computer, not at
compile-time, not at run-time
• What can be done ?
C++ Terminology
All procedure-like entities are called functions
in C++. Things that are called procedures,
methods, functions, or subprograms in other
languages are all called functions in C++.
A C++ program is basically just a function called
main; when you run a program, the run-time
system automatically invokes the function
named main.
The body of the function main is enclosed in
braces, {}. When the program is run, the
statements in the braces are executed.
C++ Tokens
The smallest individual unit when
writing program (similar to the alphabet
and vocabulary of English)
Three types
Special symbols
Word symbols
Identifiers
//basics.cpp
//This is to show some basic elements of C++
//The following two lines are preprocessor directives
#include <iostream>
using namespace std;
/*Here begins the main() function
Every C++ program has a main() function*/
int main() {
cout << "Hello world!\n\n"; //Every C++ statement ends
//with a semicolon
return 0;
} //The main() finishes here with this right curly bracket
C++ Tokens in basics.cpp
// Double slashes Single-line comment
/* … */ Multiple-line comment
# Pound sign Preprocessor command
<…> Angle brackets Contain header file names
(…) Parentheses Contain function argument list
{…} Curly brackets (braces) Delimiters to enclose the body of a
function
<< Stream insertion Insert something into an output
operator stream
; Semicolon Terminates all C++ statements
“…“ Double quotes Contains a string
\n Escape sequence Represent special character (new
line in this case)
Blank Special Separator
C++ Tokens in basics.cpp
include Specify the header file to
be included by the
preprocessor
using Specify the namespace in
the header file where the
identifiers are declared
namespace
int Integer data type
return Return a value to where
the current function is
called
Word Symbols (Reserved Words, Keywords)
C++ Tokens in basics.cpp
Name Meaning
std Where cin and cout are declared in
iostream header file
main Every C++ program must have a
main()
cout A stream output object that can be
used to display something on screen
These are all predefined identifiers from the standard library. But
you can define your own identifiers.
Identifiers
C++ Tokens
If a token is made of more than two
characters, no spaces are allowed among
these characters. (editor can help)
You are not allowed to use keywords for
other purposes, e.g. naming your own
variable int
A C++ identifiers can have letters, digits,
and underscore (_) and cannot begin with
a digit.
C++ is case-sensitive!!!!!!!!!!!!!
Use meaningful identifiers
C++ Data Types
Computer uses different # of digits to store different types of data
Data Types and Arithmetic Operators
Data Type: A set of values together with a set of
operations is called a data type.
C++ data types fall into three categories
• Simple Data Type.
• Structured Data Type.
• Pointers.
Simple Data Type
C++ simple data can be classified into three
categories
1. Integral, which is a data type that deals with integers,
or numbers without a decimal part.
2. Floating-point, which is a data type that deals with
decimal numbers.
3. Enumeration type, which is a user-defined data type.
The bool Data Type
The data type bool has two values,
true and false. The central purpose
of this data type is to manipulate logical
(Boolean) expressions. We call true and
false the logical (Boolean) values.
InC++, bool, true, and false are
reserved words.
char Data Type
• char is the smallest integral data type.
• char data type is used to represent
characters, that is letters, digits and special
symbols.
• Each character is enclosed within single
quote marks. Some of the values belonging
to char data type are:
'A', 'a', '0', '*', '+', '$', '&'
• Blank space is a character and is written ' ',
with a space left between the single quotes.
The Most Common Character Sets:
• ASCII (American Standard Code for
Information Interchange) and EBCIDIC.
• The ASCII character set has 128 values.
• EBCIDIC has 256 values and is used by IBM.
ASCII Character Set
• Each of the 128 values of the ASCII character set represents a
different character.
• The value 65 represents 'A', and the value 43 represents
'+'.
• Each character has a pre-defined ordering, which is called a
collating sequence, in the set.
• The collating sequence is used when you compare characters.
• The value representing 'B' is 66, so 'A' is smaller than 'B'.
• '+' is smaller than 'A' since 43 is smaller than 65.
• The first 32 characters in the ASCII character set are
nonprintable.
• The 14th character in the set is the new line character.
• In C++, the new line character is represented as '\n'.
• The horizontal tab character is represented in C++ as '\t'.
• The null character is represented as '\0'.
C++ Data Types (Integral)
C++ Data Types (Floating point)
Memory Range Significant Precision
digits
float 4 Bytes -3.4E+38 6 or 7 Single
and
3.4E+38
double 8 Bytes -1.7E+308 15 Double
and
1.7E+308
On most newer compilers, the data types double and long dou
30
types double and long double are the same.
Floating-Point Data Types
Scientific notation
43872918 = 4.3872918 *10^7 {10 to the power of seven},
.0000265 = 2.65 * 10^(-5) {10 to the power of minus five},
47.9832 = 4.7983 * 10^1 {10 to the power of one}
Torepresent real numbers C++ uses scientific notation called
floating-point notation.
float: The data type float is used in C++ to represent any real
number between -3.4E+38 and 3.4E+38.The memory allocated for
the float data type is 4 bytes.
double: The data type double is used in C++ to represent any real
number between -1.7E+308 and 1.7E+308.The memory allocated
for the double data type is 8 bytes.
• On most newer compilers, the data types double and long
double are the same.
• The maximum number of significant digits—that is, the number of
decimal places—in float values is 6 or 7.
• The maximum number of significant digits in values belonging to the
double type is 15.
• The maximum number of significant digits is called the precision.
• float values are called single precision
• double values are called double precision. 2
Arithmetic Operators
Five arithmetic operators
+ addition
- subtraction
* multiplication
/ division
% remainder (modulus)
Terms
Operands
Binary vs. unary operators
Operators
Unary operator: An operator that has only one operand
Binary operator: An operator that has two operands
In the expression
–5
– has only one operand, which is 5 and so - acts as a
unary operator.
In the expression
+27
+ is a unary operator.
Further, in the expressions
3 + 4
23 - 45
both the operators + and – are binary operators.
– and + are both unary as well as binary arithmetic
operators.
*, /, and % are binary arithmetic operators.
Order of Precedence
*, /, And % have higher level of precedence than + and –
No precedence within the same level
Parentheses have highest precedence
Operators *, /, and % have the same level of precedence.
Operators + and - have the same level of precedence.
When operators are all on the same level, they are performed from left to
right.
3*7–6+2*5/4+6
Means the following:
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
= ((21 – 6) + (10 / 4)) + 6
(Evaluate *)
= ((21 – 6) + 2) + 6 (Evaluate /. Note
that this is an integer division.)
= (15 + 2) + 6
(Evaluate –)
= 17 + 6
(Evaluate first +)
= 23
(Evaluate +)
Expressions
Integral expression yields integral value
Decimal expression yields decimal value
Mixed expression yields the highest precision
of the values of all the operands
Some C++ integral expressions:
2 + 3 * 5
3 + x - y / 7
x + 2 * (y - z) + 18
Some C++ floating point expressions:
12.8 * 17.5 - 34.50
x * 10.5 + y - 16.2
Mixed Expressions
An expression that has operands of different
data types is called a mixed expression.
A mixed expression contains both integers and
floating-point numbers.
Examples of mixed expressions
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
Rules to evaluate a mixed expression
1. When evaluating an operator in the mixed
expression:
a. If the operator has the same types of operands (that is, either both
integers or both floating-point numbers), the operator is evaluated
according to the type of the operands.
b. If the operator has both types of operands (that is, one is an integer
and the other is a floating-point number) then during calculation the
integer is changed to a floating-point number with the decimal part
of zero and the operator is evaluated. The result is a floating-point
number.
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 is allowed for clarity.