Cob 2202: Computer Programming 2: Facilitator: Mr. Kyambille G
Cob 2202: Computer Programming 2: Facilitator: Mr. Kyambille G
PROGRAMMING 2
1
LITERAL – CONSTANT QUALIFIERS
Literals are constants to which symbolic names are associated for
the purpose of readability and ease of handling standard constant
values. C++ provides the following three ways of defining constants:
#define preprocessor directive
enumerated data types
const keyword
float PI = 3.1452;
2
Defines a variable named PI, and is assigned with the
floating-point numeric constant value 3.1452. It is known
that the constant value does not change. In the above case,
the variable PI is considered as a constant, whose value
does not change throughout the life of the program
(complete execution-time).
5
EXAMPLE 1.8
#INCLUDE <IOSTREAM.H>
MAIN()
FLOAT RADIUS;
FLOAT AREA;
6
ENTERING THE VALUE OF RADIUS AS 2 AND RUNNING THE PROGRAM, THE OUTPUT WILL BE AS
FOLLOWS:
ENTER RADIUS OF CIRCLE: 2
AREA OF CIRCLE = 12.5808
IN THE ABOVE PROGRAM, THE USE OF THE STATEMENT SUCH AS
PI = 2.3;
TO MODIFY A CONSTANT TYPE VARIABLE LEADS TO THE COMPILATION ERROR: CANNOT MODIFY A
CONST OBJECT.
7
DATA TYPES, OPERATORS AND EXPRESSIONS IN C++
Data types
8
Figure 2: Hierarchy of C++ data types
9
THE USE-DEFINED DATA TYPE ENABLES THE PROGRAMMER TO INVENT HIS/HER
OWN DATA TYPES AND DEFINES WHAT VALUES IT CAN TAKE ON.
DERIVED DATA TYPES ARE BUILT FROM THE BASIC INTEGER AND FLOATING-
POINT DATA TYPES. THE ARRAY DATA TYPE IS ONE EXAMPLE OF DERIVED DATA
TYPES. AN ARRAY CAN HOLD SEVERAL VALUES OF THE SAME TYPE, UNDER ONE
VARIABLE NAME.
10
INTEGRAL TYPE
11
IN BOTH CASES THE CHARACTER A IS LOADED INTO THE CHARACTER VARIABLE C.
INT C;
C = ‘A’;
C = 65;
ON BOTH CASES THE DECIMAL VALUE 65 IS LOADED INTO THE INTEGER VARIABLE C.
12
FLOATING TYPE
Floating type can further be classified into:
float
double
float means floating point data-type and represent fractions such as
0.356
0.000001
To declare a variable capable of holding one of these values you use float
or double keywords.
double stands for double precision. It is capable of holding a real number
ranging from 1.7 x 10 –308 to 1.7 x 10 308 which gives twice as much
precision as presented by a float. The precision refers to the number of
decimal places that can be represented.
13
VOID TYPE
VOID DATA TYPE HAS TWO IMPORTANT PURPOSES:
THIS INDICATES THAT A FUNCTION DOES NOT RETURN ANY USEFUL VALUE. LIKEWISE, ON THE CALLING SIDE YOU
WOULD DECLARE A FUNC() AS:
THIS INFORMS THE COMPILER THAT ANY ATTEMPT TO USE THE RETURNED VALUE FROM FUNC() IS A MISTAKE AND
SHOULD BE FLAGGED AS AN ERROR. FOE EXAMPLE, YOU COULD INVOKE FUNC() AS FOLLOWS:
FUNC(X, Y);
14
VARIABLE DECLARATIONS
Every variable must be declared before being used. Declaration provides
the compiler with about how many bytes should be allocated and how
those bytes should be represented. Usually declarations of the same
data trype are grouped together. For example
int j, k;
float x, y, z;
The word int and float are reserved words specifying the integer data
type and real data type. There are nine reserved words for data types in
C++ as given below:
From To
16
To declare j as short int and k as long int we write
short int j;
long int k;
If you wish to store integer values less than –32,768 or grater that 32,
768, you should use declaration
long int.
If you need to store integer values in the range of –32,768 and 32,768,
then you can use short int.
17
UNSIGNED INTEGERS
In a situation where a variable is to hold non-negative values such as
counting things, such variables are restricted to non-negative
numbers (or unsigned), thereby doubling its positive range. Looking at
table 1 we note that signed short int has a range of -32,767 to
32,767 whereas an unsigned short int has a range from 0 to 65,535.
unsigned int k;
unsigned short k;
unsigned long n; 18
CHARACTERS AND INTEGERS
C++ makes a distinction between numeric and character data. The
data type char can be used to hold either characters or numbers. For
example, after you make the declaration
char = c;
You can make either of the following assignments:
c = ‘A’;
or
c = 65;
• Integer constants
• Floating-point constants
• String constants
Integer constants
Integer constants are values, which are mostly used by programmers
and computer users. A computer can also uses octal and hexadecimal
constants. Octal constants are written by preceding the octal value
with the digit zero. A hexadecimal constant is written by preceding the
value with zero and an x or X. Table 2 illustrates integer constants in
octal and hexadecimal equivalences.
20
TABLE 2: INTEGER CONSTANTS
Decimal Octal Hexadecimal
3 003 0x3
8 010 0x8
15 017 0xf
16 020 0x10
21 025 0x15
21
FLOATING-POINT CONSTANTS
Because an integer data type is inadequate for presenting very large
22
TABLE 3: VALID AND INVALID FLOATING-
Floating point
Constants
POINT
Comment Remarks
CONSTANTS
3.1429 Valid
.4444444 Valid
0.4 Valid
3e2 Valid
5E-2 Valid
3.7e12 Valid
The backslash character (\) alters the meaning of the character that
follows it. Thus the character ‘n’ when typed after the backslash, i.e. \n,
will mean to print a new line. Table 4 gives a list of backslash
character strings and the action they produce. 24
TABLE 4: TYPICAL RANGE AND SIZE OF
BASIC DATA TYPES
Backslash Character Meaning
26