Chapter 2 C++
Chapter 2 C++
Chapter 2 C++
To 2
Anatomy of Monolithic C++
Program
Comments
/* This program displays “Hello World” on the monitor
Date Written:……………
Written by ……………. Preprocessor
*/ Directives
#include <iostream.> //This is preprosessor directive
Main function
using namespace std;
int main ( ) //this tells the starting point of your program Statements
{
cout << “Hello World” <<endl ; //print the text on monitor
return 0 ; //return to operating system
}
Return Statement
Function Body
Comment
Comments are used to document programs. Comments should appear in
a program to:
– Explain the purpose of the program
– Identify who wrote it
– Explain the purpose of particular statements
Any comment in your program is ignored by the compiler.
There are two ways of commenting in C++
• Block Comment - comments that can span several lines. Anything
between the symbol pair /* and the symbol pair */ is a comment. E.g
/*This is a comment that spans
three lines. Note that there is no comment
symbol of any kind on the second line.*/
• Line comment – Comments that span only a single line. Usually used
to comment a single statement. All of the text between the // and the
end of the line is a comment. E.g
y = a + b //This is a line comment
Preprocessor Directives
•C++ comes with ready made compiled library objects or functions that you can use in your
program.
•Every library has a name and is referred to by a header file.
•These header file contains the description of the objects you are going to use.
•In order to use these objects the header file for these objects should be included in your
program. “C:\TC\Include”. In CodeBlock these header files are located in the directory C:\
Program Files\CodeBlocks\MinGW\lib\gcc\x86_64-mingw32\8.1.0\include\c++
•To include a header file, you use the include preprocessor directives.
Example:
To use the C++ input/output objects, you have to include the “iostream” file as follows
#include <iostream>
using namespace std;
To use the C++ mathematical objects such as absolute value, square root, sin, cosine etc, you
have to include the “cmath” file as follows
#include <cmath>
Using Standard…
Note
• To include C++ standard library function
headers, we use the file to be included
enclosed within <> instead “”. Other file can be
included by using “”.
• It is the way of telling the preprocessor that we
are including a header file of a standard library
Token
long long or
signed long long
Or unsigned long long
Integer long long int or Or
signed long long int unsigned long long int
long long
short or int
unsigned short unsigned int long or unsigned long
signed short Or Or signed long Or
Or unsigned short int signed int Or unsigned long int
short int or long int or
signed short int signed long int
Integer…
C++ supports 4 integer types : short int(or short), int, long int( or long) and long
long( or long long int). Each have a signed and unsigned version. The next table
summarize the integer types
Type Sign Byte Minimum Value Maximum Value C++ implementation
Size
short int signed 2 -32, 768 32, 767 short (or short int)
(short)
unsigned 2 0 65,535 unsigned short
int(PC) signed 2 -32, 768 32, 767 int
unsigned 2 0 65,535 unsigned int
int(main signed 4 -2,147,483,648 2,147,483,647 int
frame)
unsigned 4 0 4.294,967,295 unsigned int
long int signed 4 -2,147,483,648 2,147,483,647 long (or long int)
unsigned 4 0 4.294,967,295 unsigned long
Long long signed 8 –9, 223, 372, 036, 9, 223, 372, 036, long long (or long
int 854, 775, 808 854, 775, 807 long int)
unsigned 8 0 18, 446, 744, 073, unsigned long long
709, 551, 615
Character Data Type
C++ supports 2 character types : char and wchar_t. Each have a signed
and unsigned version. The next table summarize the integer types
Character
char wchar_t
Logical Negation
If its operand is true or non-zero it produces false, and if it is false or zero it
produces true.
Logical And
Produces false if one or both of its operands evaluate to false or zero. Otherwise, it
produces true.
Logical Or
Produces false if both of its operands evaluate to false or zero. Otherwise, it
produces 1.
Logical…
Note
• we talk of zero and nonzero operands (not zero and 1). In general, any
nonzero value can be used to represent the logical true, whereas only
zero represents the logical false. The following are, therefore, all valid
logical expressions:
!20 // gives 0 10 || 5.5 // gives 1
10 && 5 // gives 1 10 && 0 // gives 0
• In evaluating logical && expression, if the left part is evaluated to
false, there is no need to evaluate the second expression. A similar
thing happens with || expressions. If the first of two expressions
joined with the || operator is true, then you know the entire
expression is true, no matter whether the second expression is true or
false. The C++ language operates in a “short circuit mode”, i.e., if the
truth value of the whole expression can be determined by evaluating
the left expression of && or ||, C++ does not bother to evaluate the
right expression. But this may not be true for other languages.
E.g 1 > 2 && 7 < 3 // the expression 7 < 3 will not be evaluated
1 < 7 || 5 < 4 //the expression 5 < 4 will not be evaluated
Assignment Operator
The assignment operator(=) is used for storing a value at some
memory location (typically denoted by a variable).
The part at the left of the assignment operator (=) is
known as the lvalue (left value) and the right one as the rvalue (right
value).
The lvalue has to be a variable whereas the rvalue can be either a
constant, a variable, the result of an operation or any combination of
these.
In an assignment statement, first the expression on the right-hand side of
the equal sign is evaluated
and then the variable on the left-hand side of the equal sign is set equal to
this value.
SYNTAX
Variable = Expression;
EXAMPLES
distance = rate * time;
a=5 //assign 5 to the variable a
5=a // invalid assignment left side should be an lvalue
Increment/Decrement Operators
The auto increment (++) and auto decrement (--) operators provide a convenient way
of, respectively, adding and subtracting 1 from a numeric variable. These are
summarized in the table below. The examples assume the following variable definition:
int k = 5;
Operator Name Example
++ Prefix increment Y = ++k + 3 //k will be 6 and y will be 9
++ Postfix increment Y = k++ + 3 //k will be 6 and y will be 8
-- Prefix decrement Y = --k + 3 //k will be 4 and y will be 7
-- Postfix decrement Y = k-- + 3 //k will be 4 and y will be 8
i++, ++i are identical to i= I + 1
i--, --i are identical to i= I - 1
Both operators can be used in prefix and postfix form. The difference is significant.
When used in prefix form, the operator is first applied and the outcome is then used in
the expression. When used in
the postfix form, the expression is evaluated first and then the operator
Bitwise Operators
Java defines several bitwise operators that can be applied to the
integer types:long long, long, int, short, char. These operators act
upon the individual bits of their operands.
1. Bitwise NEGATION(~) is a unary operator which reverses the
bits in its operands.
2. Bitwise AND(&) compares the corresponding bits of its
operands and produces a 1 when both bits are 1, and 0
otherwise.
3. Bitwise OR (|) compares the corresponding bits of its
operands and produces a 0 when both bits are 0, and
1otherwise.
4. Bitwise EXCLUSIVE OR(^ ) compares the corresponding bits of
its operands and produces a 0 when both bits are 1 or both
bits are 0, and 1 otherwise 0.
Bitwise…
5. The bitwise LEFT SHIFT (<<) moves the data left a specified number
of bits. Any bits that are shifted out the left side disappear. New bits
coming in from the right are zeros.
Shifting left by n places is the same as multiplying by 2n.
6. The bitwise RIGHT SHIFT (>>) does the same thing in the other
direction. When a variable is shifted to the right, C++ needs to fill the
space on the left side with something. For signed variables, C++
uses the value of the sign bit. For unsigned variables, C++ uses zero
These are summarized in Table below. In the table below, The
examples assume the following variable definition:
Bitwise…
These are summarized in Table below. In the table below, The examples assume
the following variable definition:
int cl = 0x45; //00000000000000000000000001000101 (69)
int c2 = 0x71; //00000000000000000000000001110001 (113)
3. Float expressions
Float expressions are those which, after all conversions,
produce floating – point results. Examples:
x * y / 10.0 //double
5 + float(10) //float
where x and y are integer variable
4. Relational expressions
Relational expressions yield results of type bool which
takes a value true(1) or false(0). Examples:
x <= y
a+b == c+d
Type of Expression
5. Logical expressions
Logical expressions combine two or more relational
expressions and produce bool type results. Examples:
a>b && x==10
x==10 || y==5
6. Bitwise expressions
An expression that involves one or more bitwise operator
and produce an integral value. Examples:
!a << 4
a&b
Side Effects
data source:
our program
data destination:
Standard Streams