int: Define the data type of the used variable... Input type depends on the variable type in which you store the input.. large set of data types used to define the type of the valu
Trang 3Sample C++ Program
//Sample C++ Program
/* This program prompt the user to enter two integers
and return their sum*/
# include <iostream.h>
int main()
{
int unsigned a1, a2, sum;
cout<<"Enter the first number:"; //this message will appear to the user cout << endl;
Trang 4Output
Trang 5Program Explanation I
Single line comment: after //
Multi-line comments: between /* and */
Comments are not processed by the compiler, feel free to write anything you want.
Special instructions for the preprocessor.
Start with # and usually come at the beginning of the
Header file which is simply a library that includes the
definitions of the used functions within the program, i.e the frequently used ones to avoid repeating the code.
Two types: standard (comes with C++ package) and user
defined.
Trang 6 Braces define a block of code.
{ is the start of this block and } is its end.
Names of variables and they are called identifiers.
int:
Define the data type of the used variable.
Trang 7Program Explanation III
An input function defined in the iostream library.
Get an input usually from the keyboard
Followed by the extraction operator >> then the variable name in which you want to store the input value.
Input type depends on the variable type in which you store the input.
return 0:
Tells the compiler that the main function returns 0 to the operating system in case of successful execution of the program.
Trang 8of every code line in your program.
Any line of code terminated with ; is for the compiler, preprocessor
directives do not end with ;
Trang 9C++ Versions
or the integrated development environment (IDE) or both of them.
functions, nonstandard functions are
compiler-dependent.
IDEs and different compilers.
Trang 10using Statement
(like iostream library) you need to use the
using statement as follows.
Eliminate the need to use the std:: prefix
Allow us to write cout instead of std::cout
To use the following functions without the std:: prefix,
write the following at the top of the program
Trang 11 You can place declarations in any place within
your program (but make sure to be before the first usage of the variables) but it is preferred to place them at the beginning of your program.
Multiple variables of the same data type can be defined using one statement having the
variables name comma separated
E.g
int a, b, c;
int a = 0, b = 6, c;
Trang 12Data Types
numbers, e.g real numbers, natural, fractions,
integers, etc.
large set of data types used to define the type of the value stored in a variable.
Fundamental or built-in data types.
User defined data types.
Trang 13Fundamental Data Types int
Integer:
Positive and negative integer values (no decimal point is allowed).
Syntax: int number;
What will happen if you save a float number in an
integer? (storing 3.9 in number ignore everything after the decimal point without rounding)
The range of numbers allowed to be stored in an integer variable depends on the memory size.
short: 2 bytes signed integer.
long: 4 bytes signed integer.
signed: the most significant bit is reserved for sign.
unsigned: no negative numbers.
Trang 14Fundamental Data Types int
The default of int type depends on the used compiler and the operating system under which this compiler
You cannot combine signed and unsigned for the
same variable and you cannot combine long and
short for the same variable.
What are the following correspond to?
signed a;
unsigned a;
short a;
Trang 15Fundamental Data Types float
decimal point (fractions) and exponents (power
of 10).
What happen if you enter more than 6 digits
after the decimal point? (truncation)
allowed.
Trang 16Fundamental Data Types double
range.
Trang 17Fundamental Data Types – char
Character is one byte of memory used to save any symbol, alphabet or digit number presented on the keyboard, e.g :, /, @, d, 5.
Syntax: char a;
char initialization:
Remember that you can initialize any variable in two ways:
Either from the keyboard by the user at runtime.
Or hardcoded in your program when you are writing the code.
From the keyboard: you must enter only one character If you enter more than one character for a char variable only the first one will be taken and stored in your variable.
E.g.: What happen if you enter 199 for a character using the keyboard?
it will store 1 and ignore the rest
Hard coding: only and only one character can be stored in a char variable as follows:
char t = ‘a’; or char t = ‘9’;
char t = “a” // Syntax error since “a” is not only one character
Trang 18Fundamental Data Types – char
Character coding is used to store the
values of characters in char variable in C++ since computers only knows binary numbers.
Based on this coding every character
has a number value.
Unicode : used mainly for Internet applications.
ASCII is the most dominant.
Trang 19ASCII Table
Trang 21Output Example
Trang 22Fundamental Data Types bool
keyword.
Any number other than zero, e.g 9, -2, -100, 3.4, etc.
Any character (including white spaces) or string, e.g bool a = “Hello”; //will initialize a to be true.
i.e using cout statement, it will be either 1 when true or 0 when false.
Trang 23Data Types Ranges
Name (Compiler Bytes
dependent)
Description Range (depends on number of bytes)
char 1 Character or 8 bit integer signed: -128 to 127unsigned: 0 to 255
bool 1 Boolean type It takes only two values. true or false
short 2 16 bit integer signed: -32768 to 32767unsigned: 0 to 65535
long 4 32 bit integer signed:-2147483648 to 2147483647
int 2/4
Integer Length depends on the size
of a ‘word’ used by the Operating system In MSDOS a word is 2 bytes and so an integer is also 2
bytes
Depends on whether 2/4 bytes
float 4 Floating point number
double 8 double precision floating point
Trang 24implicitly.
explicitly by the programmer (to force casting).
Trang 25Implicit Casting
running, you may store values of different data type from the data type of the variable you are dealing with.
a syntax error However, it converts the data type of the value implicitly.
Trang 26Explicit Casting
casting.
C-style explicit casting.
C++ casting operators.
Just put the data type name that you want to convert to
it before the variable name (or value) that you want to convert and place the parenthesis correctly.
Trang 27C++ Casting Operators
will take only one of them.
static_cast <new_type> (expression)
new_type: is the type you want to convert to
expression: is the variable name or the expression that you want to cast its value.
int a = 100;
bool b = static_cast<bool>(a);
Trang 28Notes On Casting
the variable only So, you do not
change the original data type of
the variable through casting.
can cause logical errors due to
possible data loss.
Trang 29from LETTER and Letter and leTTEr.
following restrictions:
Do not use any of C++ keywords, e.g if, for, int, float,
cout,
Never start your identifier with a digit (number) always
start it with alphabet or underscore.
Do not use white spaces in your identifier, use underscores instead.
Do not use special symbols in your identifier such as #, $, etc.
Do not use any of the operators (arithmetic, logical, etc.)
in your identifier such as +, =, etc.
Trang 30continue default do double else
short signed sizeof static struct
switch typedef union unsigned void
volatile while
C++ only keywords
asm bool catch class const_cast
delete dynamic_cast explicit false friend
inline mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw true
try typeid typename using virtual
Trang 31Memory Concepts
Correspond to locations in the computer's memory
Every variable has a name, a type, a size and a value
Whenever a new value is placed into a variable, it replaces the previous value - it is destroyed, such thing is called overwriting.
Reading variables from memory does not change them
A visual representation that we will always use:
Trang 32Code Lines Breakage
You can split a long line of code among
multiple lines by pressing enter However, you must be careful when selecting the break locations of the line of code:
After or before an operator.
After a comma in a comma separated list.
After the end of a string (do not break at the middle
of a string).
E.g.
cout << “Hello World\n”; //Syntax Error cout<<
“Hello World\n”; //Correct
Trang 33Additional Notes
material from the textbook:
Fourth edition:
Chapter 1: Sections 1.20 and 1.21