Professional Documents
Culture Documents
L07 Basic Types and Variables
L07 Basic Types and Variables
05 March 2024
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
Overview
1 Primitive Types
3 Naming conventions
4 Declaring Variables
5 Type conversions
Primitive Types
1. Ordinal
Ordinal types are Integer, Character and
Boolean. The keywords used to represent
these types are int, char and bool
respectively.
2. Variable-length
Variable-length types include floating point,
of which there are two types: float and
double. The difference lies in precision.
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
3. Type modifiers
In C++ type modifiers are used to change
the properties of basic data types, so that
programmers can define variables more
suited to the specific application. These
modifiers include: • signed (for int and char)
• unsigned (for int and char) • short (for int)
• long (for int and double)
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
1. Variables
A variable is a letter or word that can stand
in for any number, character, string, list,etc..
Think of a variable as a place-holder for a
value.
Variables in C++ are defned by providing a
type and name for the variable in the format:
(typename)(variablename)
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
(typename)(variablename)
where typename is one of the types given
in Table 3.1 and variablename the name
the variable will be referred to as in the
progoram.
2 . The f i r s t c h a r a c t e r must be a l e t t e r o r u n d e r s c o r e .
3 . B l a n k s p a c e s c a n n o t be u s e d i n v a r i a b l e names .
5 . C++ k e y w o r d s c a n n o t be u s e d a s v a r i a b l e names .
6 . V a r i a b l e names a r e c a s e − s e n s i t i v e .
7 . V a r i a b l e t y p e can be b o o l , c h a r , i n t , f l o a t , d o u b l e , v o i d
2. Constant
Constants are used as a naming mechanism
for values that may not change during the
life of a program.
Typical values that can be defined as a
constant are Pl, MAX STUDENTS,etc
These values are defined once and can be
used a number of times within the program.
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
The s y n t a x f o r d e f i n i n g a c o n s t a n t u s i n g t h e p r e p r o c e s s o r method
i s g i v e n by :
#d e f i n e <CONSTANTNAME> <c o n s t a n t v a l u e >
Below i s an e x a m p l e t o d e f i n e P I .
#d e f i n e P I 3 . 1 4 1 5
#i n c l u d e <i o s t r e a m >
#d e f i n e P I 3 . 1 4 1 5
u s i n g namespace s t d ;
i n t main ( )
{
c o u t << ”The a r e a o f a c i r c l e w i t h a r a d i u s o f 10cm i s : ”
<< P I ∗ 10 ∗ 10 ;
return 0 ;
}
The s y n t a x t o d e f i n e t h e c o n s t a n t i s g i v e n by :
c o n s t <typename> <CONSTANT NAME> = < c o n s t a n t v a l u e >;
D e p e n d i n g on w h e r e a c o n s t a n t i n d e f i n e d i n a C++ program ,
w i l l determine the scope of the constant .
#i n c l u d e <i o s t r e a m >
c o n s t f l o a t PI = 3 . 1 4 1 5 ;
u s i n g namespace s t d ;
i n t main ( )
{
c o n s t i n t RADIUS = 10 ;
c o u t << ”The a r e a o f a c i r c l e w i t h a r a d i u s o f 10cm i s : ”
<< P I ∗ RADIUS ∗ RADIUS ;
return 0 ;
}
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
3. Scope
Defining the constant outside the main
function enables the constant to be used
globally.
If the constant is defined within the
main function, only the main function
knows about the constant and can use
the constant.
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
4. Naming conventions
It is important that when you choose a name
for a variable or constant, it is descriptive.
This will make it easier to read the code.
Code that is easier to follow is easier to
maintain.
The variable name must begin with a letter
and may have no spaces.
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
Char d a t a t y p e :
Used f o r c h a r a c t e r s : l e t t e r s , d i g i t s , and s p e c i a l s y m b o l s
’A ’ , ’a ’ , ’0 ’ , ’ ∗ ’ , ’ + ’ , ’ $ ’ , ’& ’
A b l a n k s p a c e i s a c h a r a c t e r and i s written ’ ’,
string Type
Sequence of zero or more characters
Enclosed in double quotation marks.
Example: string color = ”red”
Null: a string with no characters. Example:
string color = ” ”
Each character has relative position in string
-Position of first character is 0
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
1. Implicit Conversions
Implicit conversions are performed
automatically by the compiler when it
encounters expressions involving different
data types.
2. Explicit Conversions (Typecasting)
Also known as typecasts, are performed by
the programmer to explicitly convert a value
from one data type to another.
COS132 Basics Types and Variables
Primitive Types
Variables and Constants
Naming conventions
Declaring Variables
Type conversions
Code example section
#i n c l u d e <i o s t r e a m >
i n t main ( ) {
// E x p l i c i t t y p e c a s t i n g
double x = 3.14;
i n t y = s t a t i c c a s t <i n t >(x ) ;
// I m p l i c i t t y p e c a s t i n g
i n t a = 10;
d o u b l e b = a ; // I m p l i c i t c o n v e r s i o n from i n t t o d o u b l e
return 0;
}
c i n i s u s e d w i t h >> t o g a t h e r i n p u t
c i n >>v a r i a b l e >>v a r i a b l e
The s t r e a m e x t r a c t i o n o p e r a t o r i s >>
F o r example , i f m i l e s i s a d o u b l e v a r i a b l e
c i n >> m i l e s ;
Causes computer to get a v a l u e o f type double
Places i t in the v a r i a b l e miles
Figure: EnterBasics
COS132 Caption
Types and Variables