Uploaded by mohamed abdelbaky

C++ Variables: Introduction & Data Types

advertisement
INTRODUCTION
TO VARIABLES
C++
Introduced by /
What is a variable ?
■ Variables are containers for storing data
values.
■ Each variable has three :
1- Name
2- Type
3- Value
What are data types ?
Data type
Size in
memory
Description
bool
1 byte
Store true or false values
char
1 byte
Store only one character
short
2 bytes
Store small integer numbers
int
4 bytes
Store integer numbers
long long
8 bytes
Store big integer numbers
Float
4 bytes
Stores fractional numbers, containing one or
more decimals. Sufficient for storing 6-7
decimal digits
double
8 bytes
Stores fractional numbers, containing one or
more decimals. Sufficient for storing 15 decimal
digits
• 1 byte = 8 bits
• Bits mean
binary digits
• Don’t forget :
computers
understand
only binary
digits ( 0 and
1)
How to create a variable ?
type variableName = value;
Examples :
int number = 5;
double price = 10.5;
char letter = ‘a’;
Some problems
■ Problem 1 : write a c++ program that calculate the summation of two integer values
Solution :
Problem 2 : write a c++ program that
calculates the multiplication of three double
values and print the result.
Variables naming rules (c++ identifiers)
1) Variable names must begin with a letter (a-z, A-Z) or an
underscore (_).
2) Variable names can contain letters, digits (0-9), and underscores
but can’t contain special characters like * , & , @ , ……
3) Variable name can’t be reserved word like ( int , double , return
,……)
4) Variable names are case sensitive : VAR is not like var is not like
vAr : (Those are different)
5) All c++ variable names must be unique names.
6) Names can’t contain white spaces.
Some reserved keywords
C++ primer 5th edition
Some exercise
Is this a variable identifier (name) in c++ ? Answer with yes or no (if no say why)
1) num%ber
2) _Mohamed11
3) AnFjT
4) Amount of money
5) 9x
6) Amount-of-money
7) Amount_of_money
Best practice of naming rules
■ Using descriptive names
■ Follow : camelCase or snake_case
■ Avoid using single letter variables except for loops
■ Use just upper case letters and (_) for const
variables
Declaration and Initialization
■ Declaration : is generally an introduction to a new memory
allocated to something that we may call with some name.
■ Properties of declaration :
1.Memory creation occurs at the time of declaration itself.
2.Variables may have garbage(unwanted) values.
3.Variables cannot be used before declaration.
Examples for declaration
■int num;
■float price;
■char my_letter;
What is initialization ?
Initialization : is assigning value to the declared variable.
(At the time of declaration)
Examples :
■ int num = 0;
■ float price = 0.0;
■ char my_letter = ‘a’;
Solve : cumulating problem ( very important )
Const variable
1) Const variable : is one whose value cannot be changed
after it has been initialized
2) Syntax : const data type variable_name
Ex : const int num = 0;
3) Be carful : const variable must be initialized
You can’t do this : const int num; (this is a compilation error)
User input
■ You have already learnt that cout is used to print
values . Now we will use cin to take values from the
user to make our program interactive
■ Syntax : cin>> variable_name (after declaration)
Ex : int num = 0;
cin>>num;
Problems
■ Simple calculator
■ Rectangle problem
■ Average for grades ( After arithmetic
operators )
■ Print string
The variable scope
■ Scope : is a part of the program in which a name
has a particular meaning.
■ Most scopes in C++ are delimited by curly braces {}
.
■ Names are visible from the point they are declared
until the end of the scope in which the declaration
appears.
Download