Chapter 2 - Computer programming
Chapter 2 - Computer programming
In eighties an average large C program was of the order of a few thousands of lines of code. With
really large programs the difficulties with C language became more apparent and researchers
were busy finding better options. C++ is a redevelopment of C carried out by Bjarne Stroustrup.
Inspired by the language Smalltalk and Simula which are object oriented programming (OOP)
languages, he added the concept of classes, in order to impart similar capabilities in the then
widely popular C language. Classes are the basis of OOP. Initially the new language was called
C with classes. The name C++ was coined later by Rick Mascitti in 1983. The symbol (++) is an
operator in C which increases the value of an integer variable by 1. The name C++ aptly shows
that C++ is one up on C. The C++ is also often called as better C.
The C++ programming language became popular soon after it was officially launched. As a
result, scores of programmers got involved, who have not only tested the codes and recognised
the pitfalls, but have also added reusable program modules to C++ Standard Library. Standard
Library is a large collection of well tested and reusable programs which facilitate the writing and
execution of diverse applications. Below only major developments are listed which are
responsible for major advantages of using C++ over C.
Classes which provide a mechanism for data abstraction, encapsulation of data and functions,
information hiding and object oriented programming.
Operator overloading.
Inheritance.
Polymorphism.
Addition of namespaces.
These developments in C++ have enhanced the capabilities of C++ enormously. A very large
program may be broken into small modules and classes, which may be developed by different
programmers and tested for their performance before combining them into one large program.
There are a number of steps involved between writing a program and executing a program in C /
C++. Let's consider the steps in creating, executing and understanding a C++ application using a
C++ development environment.
1. Writing your program in a text editor and saving it with correct extension(.CPP, .C, .CP)
2. Compiling your program using a compiler or online IDE
3. Understanding the basic terminologies.
Phase 1 consists of editing a file with an editor program (normally known simply as an editor).
You type a C++ program (typically referred to as source code) using the editor, make any
necessary corrections and save the program on a secondary storage device, such as your hard
drive. C++ source code file names often end with the .cpp extensions, which indicate that a file
contains C++ source code.
The important thing is that whatever you write your program in, it must save simple, plain-text
files, with no word processing commands embedded in the text. Examples of safe editors include
Windows Notepad, codeblocks, the DOS Edit command, EMACS, and VI (for Linux).
Any meaningful program written in C++ has to contain a number of components: the main
function; some variable declarations; and some executable statements. For example, the
following is a very basic C++ program:
1. Preprocessor
There are a number of files called header files and functions in C++ Standard Library which help
in the execution of the programs. Therefore, every program has to include the header files which
are required by the program.
On line 1, the file iostream.h is included in the file. The first character is the # symbol, which is
a signal to the preprocessor. Each time you start your compiler, the preprocessor is run. The
preprocessor reads through your source code, looking for lines that begin with the pound symbol
(#), and acts on those lines before the compiler runs.
include is a preprocessor instruction that says, "What follows is a filename. Find that file and
read it in right here." The angle brackets around the filename tell the preprocessor to look in all
the usual places for this file. If your compiler is set up correctly, the angle brackets will cause the
preprocessor to look for the file iostream.h in the directory that holds all the H files for your
compiler. The file iostream.h (Input-Output-Stream) is used by cout, which assists with writing
to the screen. The effect of line 1 is to include the file iostream.h into this program as if you had
typed it in yourself.
The preprocessor runs before your compiler each time the compiler is invoked. The preprocessor
translates any line that begins with a pound symbol (#) into a special command, getting your
2. A blank line
On line 3: A blank line. Blank lines have no effect on a program. They simply improve
readability of the code.
3. Int main()
Line 3 begins the actual program with a function named int main(). Every C++ program has a
main() function. In general, a function is a block of code that performs one or more actions.
Usually functions are invoked or called by other functions, but main() is special. When your
program starts, main() is called automatically.
4. Brace
All functions begin with an opening brace ({) and end with a closing brace (}). The braces for
the main() function are on lines 4 and 7. Everything between the opening and closing braces is
considered a part of the function.
5. Statements
This line is a C++ statement. Statements are fragments of the C++ program that are executed in
sequence. The body of any function is a sequence of statements, and they are executed in the
same order that they appear within a function's body.
Notice that the statement ends with a semicolon (;). This character marks the end of the
statement, just as the period ends a sentence in English. One of the most common syntax errors
in C++ is forgetting to end a statement with a semicolon.
6. Return value
main(), like all functions, must state what kind of value it will return. The return value type for
main() in HELLO.CPP is int, which means that this function will return an integer value. This
value is "returned" to the operating system when your program completes.
Compiling a C++ program involves a number of steps (most of which are transparent to the
user):
Step 1: Preprocessing
First, the programmer gives the command to compile the program. In a C++ system, a
preprocessor program executes automatically before the compiler's translation phase begins (so
we call preprocessing phase 1 and compiling phase 2). The C++ preprocessor obeys commands
called preprocessor directives, which indicate that certain manipulations are to be performed on
the program before compilation. These manipulations usually include other text files to be
compiled and perform various text replacements. The C++ preprocessor goes over the program
text and carries out the instructions specified by the preprocessor directives (e.g., #include). The
result is a modified program text which no longer contains any directives.
Step 2: Compiling
Then, the C++ compiler translates the program code. The compiler may be a true C++ compiler
which generates native (assembly or machine) code, or just a translator which translates the code
into C. In the latter case, the resulting C code is then passed through a C compiler to produce
native object code. In either case, the outcome may be incomplete due to the program referring to
library routines which are not defined as a part of the program.
Step 3: Linking
Finally, the linker completes the object code by linking it with the object code of any library
modules that the program may have referred to. The final result is an executable file. C++
programs typically contain references to functions and data defined elsewhere, such as in the
standard libraries or in the private libraries of groups of programmers working on a particular
project. The object code produced by the C++ compiler typically contains "holes" due to these
Figure 1.1 illustrates the above steps for both a C++ translator and a C++ native compiler. In
practice all these steps are usually invoked by a single command (e.g., CC) and the user will not
even see the intermediate files generated.
C++ C
C++ C
Program Code
TRANSLATOR COMPILER
Execut-
LINKER able
2.3.1. Comments
A comment is a piece of descriptive text which explains some aspect of a program. Program
comments are totally ignored by the compiler and are only intended for human readers. C++
provides two types of comment delimiters:
1. Single Line Comment: Anything after // {double forward slash} (until the end of the
line on which it appears) is considered a comment. Eg:
cout<<var1; //this line prints the value of var1
2. Multiple Line Comment: Anything enclosed by the pair /* and */ is considered a
comment.
Eg:
Comments should be used to enhance the readability of a program. A comment should be easier
to read and understand than the code which it tries to explain.
The most common way in which a program communicates with the outside world is through
simple, character-oriented Input/Output (IO) operations. C++ provides two useful operators for
this purpose: cin>> for input and cout<< for output. The stream operators << and >> may be
used more than once in a single statement.
(cout<<): It is used to produce output on the standard output device which is usually the
display screen.
(cin>>): is used to read input from the standard input device which is usually a keyboard.
Example
#include <iostream.h>
int main ()
{
int a;
cout << "How old are you? ";
cin >> a;
cout << "Your age is= ";
cout << a;
return
}
Analysis
First, outputs the prompt ‘How old are you? ’ to seek user input.
Then, reads the input value typed by the user and copies it to a. The input operator >>
takes an input stream as its left operand (cin is the standard C++ input stream which
corresponds to data entered via the keyboard) and a variable (to which the input data is
copied) as its right operand.
Your age is = 20
Both << and >> return their left operand as their result, enabling multiple input or multiple output
operations to be combined into one statement. This is illustrated by example below which now
allows the input of both the daily work hours and the hourly pay rate.
Example
#include <iostream.h>
int main (void)
{
int workDays = 5;
float workHours, payRate, weeklyPay;
cout << "What are the work hours and the hourly pay rate? ";
cin >> workHours >> payRate;
weeklyPay = workDays * workHours * payRate;
cout << "Weekly Pay = " << weeklyPay << '\n';
}
Anotation
Line 7: This line reads two input values typed by the user and copies them to workHours and
payRate, respectively. The two values should be separated by white space (i.e., one or more
space or tab characters).
Line 9: It outputs "Weekly Pay = ", followed by the value of weeklyPay, followed by a newline
character.
What are the work hours and the hourly pay rate? 7.5 33.55
Weekly Pay = 1258.125
Reserved/Key words have a unique meaning within a C++ program. These symbols, the reserved
words, must not be used for any other purposes. All reserved words are in lower-case letters. The
keywords should not be used as names. Their use as names of variables, constants, functions
or objects will create errors in the program. However, if the case of any letter in a keyword is
changed it is no longer a keyword. Thus a safe bet is that the starting letter in a name may be
made capital. The following are some of the reserved words of C++.
A variable is a symbolic name for a memory location in which data can be stored and
subsequently recalled. Variables are used for holding data values so that they can be utilized in
various computations in a program. A variable will have three components:
Data Type: a type which is established when the variable is defined. (e.g. integer, real,
character etc). Data type describes the property of the data and the size of the reserved
memory. C++ is highly typed language which means that the data is categorized into
different types. For example, whole numbers form a category called integers. So when a
variable whose value can only be in whole numbers, is declared we write int (short form
Name/ identifier: a name which will be used to refer to the value in the variable. A
unique identifier for the reserved memory location
Value: a value which can be changed by assigning a new value to the variable. The kind
of values a variable can assume depends on its type.
2.5.1. Identifiers
An identifier is name associated with a function or data object and used to refer to that function
or data object. Identifiers can be used to identify variable or constants or functions. Function
identifier is an identifier that is used to name a function. An identifier must:
For the purposes of C++ identifiers, the underscore symbol, _, is considered to be a letter. Its use
as the first character in an identifier is not recommended though, because many library functions
in C++ use such identifiers. Similarly, the use of two consecutive underscore symbols, _ _, is
forbidden.
Important:
At this stage it is worth noting that C++ is case-sensitive, which, means that it will take ‘A’ and
‘a’ as two different objects. Therefore, while writing a program the name of a variable, constant
or a function should be written consistently in the same fashion throughout the program.
Declaring a variable means defining (creating) a variable. You create or define a variable by
stating its type, followed by one or more spaces, followed by the variable name and a
semicolon.
The variable name can be virtually any combination of letters, but cannot contain spaces and the
first character must be a letter or an underscore. Variable names cannot also be the same as
keywords used by C++. Good variable names tell you what the variables are for; using good
names makes it easier to understand the flow of your program. The following statement defines
an integer variable called myAge:
int myAge;
You can create more than one variable of the same type in one statement by writing the type and
then the variable names, separated by commas. For example:
As you can see, myAge and myWeight are each declared as integer variables. The second line
declares three individual long variables named area, width, and length. However keep in mind
that you cannot mix types in one definition statement.
A point worth mentioning again here is that C++ is case-sensitive. In other words,
uppercase and lowercase letters are considered to be different.
You assign a value to a variable by using the assignment operator (=). Thus, you would assign 5
to Width by writing
int Width;
Width = 5;
You can combine these steps and initialize Width when you define it by writing
int Width = 5;
Initialization looks very much like assignment, and with integer variables, the difference is
minor. The essential difference is that initialization takes place at the moment you create the
variable.
Just as you can define more than one variable at a time, you can initialize more than one variable
at creation. For example:
This example initializes the integer variable width to the value 5 and the length variable to the
value 7. It is possible to even mix definitions and initializations:
When you define a variable in C++, you must tell the compiler what kind of variable it is: an
integer, a character, and so forth. This information tells the compiler how much room to set aside
and what kind of value you want to store in your variable.
A computer provides a Random Access Memory (RAM) for storing executable program code as
well as the data the program manipulates. This memory can be thought of as a contiguous
sequence of bits, each of which is capable of storing a binary digit (0 or 1). Typically, the
memory is also divided into groups of 8 consecutive bits (called bytes). The bytes are
sequentially addressed. Therefore each byte can be uniquely identified by its address (see Figure
1.2).
Byte Address
1 1 0 1 0 0 0 1
Bit
The C++ compiler generates executable code which maps data entities to memory locations. For
example, the variable definition
causes the compiler to allocate a few bytes to represent salary. The exact number of bytes
allocated and the method used for the binary representation of the integer depends on the specific
C++ implementation, but let us say two bytes encoded as a 2’s complement integer. The
compiler uses the address of the first byte at which salary is allocated to refer to it. The above
While the exact binary representation of a data item is rarely of interest to a programmer, the
general organization of memory and use of addresses for referring to data items (as we will see
later) is very important.
Several data types are built into C++. The varieties of data types allow programmers to select the
type appropriate to the needs of the applications being developed. The data types supported by
C++ can be classified as basic (fundamental) data types, user defined data types, derived data
types and empty data types. However, the discussion here will focus only on the basic data types.
Basic (fundamental) data types in C++ can be conveniently divided into numeric and character
types. Numeric variables can further be divided into integer variables and floating-point
variables. Integer variables will hold only integers whereas floating number variables can
accommodate real numbers. Both the numeric data types offer modifiers that are used to vary the
nature of the data to be stored. The modifiers used can be short, long, signed and unsigned.
The data types used in C++ programs are described in the next table. This table shows the
variable type, how much room it takes in memory, and what kinds of values can be stored in
these variables. The values that can be stored are determined by the size of the variable types.
As shown above, integer types come in two varieties: signed and unsigned. The idea here is that
sometimes you need negative numbers, and sometimes you don't. Integers (short and long)
without the word "unsigned" are assumed to be signed. Signed integers are either negative or
positive. Unsigned integers are always positive.
Because you have the same number of bytes for both signed and unsigned integers, the largest
number you can store in an unsigned integer is twice as big as the largest positive number you
can store in a signed integer. An unsigned short integer can handle numbers from 0 to 65,535.
Half the numbers represented by a signed short are negative, thus a signed short can only
represent numbers from -32,768 to 32,767.
2: #include <iostream.h>
3:
4: int main()
5: {
6: unsigned short int Width = 5, Length;
7: Length = 10;
8:
9: // create an unsigned short and initialize with result
10: // of multiplying Width by Length
11: unsigned short int Area = Width * Length;
12:
Annotation
Line 2 includes the required include statement for the iostream's library so that cout will
work. Line 4 begins the program.
On line 6, Width is defined as an unsigned short integer, and its value is initialized to 5.
Another unsigned short integer, Length, is also defined, but it is not initialized. On line 7,
the value 10 is assigned to Length.
On line 11, an unsigned short integer, Area, is defined, and it is initialized with the value
obtained by multiplying Width times Length. On lines 13-15, the values of the variables
are printed to the screen.
The fact that unsigned long integers have a limit to the values they can hold is only rarely a
problem, but what happens if you do run out of room? When an unsigned integer reaches its
maximum value, it wraps around and starts over, much as a car odometer might. The following
example shows what happens if you try to put too large a value into a short integer.
1: #include <iostream.h>
2: int main()
3: {
4: unsigned short int smallNumber;
5: smallNumber = 65535;
6: cout << "small number:" << smallNumber << endl;
7: smallNumber++;
8: cout << "small number:" << smallNumber << endl;
9: smallNumber++;
10: cout << "small number:" << smallNumber << endl;
11: return 0;
12: }
Output: small number:65535
A signed integer is different from an unsigned integer, in that half of the values you can represent
are negative. Instead of picturing a traditional car odometer, you might picture one that rotates up
for positive numbers and down for negative numbers. One mile from 0 is either 1 or -1. When
you run out of positive numbers, you run right into the largest negative numbers and then count
back down to 0. The whole idea here is putting a number that is above the range of the variable
can create unpredictable problem.
1: #include <iostream.h>
2: int main()
3: {
4: short int smallNumber;
5: smallNumber = 32767;
6: cout << "small number:" << smallNumber << endl;
7: smallNumber++;
8: cout << "small number:" << smallNumber << endl;
9: smallNumber++;
10: cout << "small number:" << smallNumber << endl;
11: return 0;
12: }
Output: small number:32767
small number:-32768
small number:-32767
IMPORTANT – To any variable, do not assign a value that is beyond its range!
2.6.4. Characters
Character variables (type char) are typically 1 byte, enough to hold 256 values. A char can be
interpreted as a small number (0-255) or as a member of the ASCII set. ASCII stands for the
American Standard Code for Information Interchange. The ASCII character set and its ISO
(International Standards Organization) equivalent are a way to encode all the letters, numerals,
and punctuation marks.
In the ASCII code, the lowercase letter "a" is assigned the value 97. All the lower- and uppercase
letters, all the numerals, and all the punctuation marks are assigned values between 1 and 128.
Another 128 marks and symbols are reserved for use by the computer maker, although the IBM
When you put a character, for example, `a', into a char variable, what is really there is just a
number between 0 and 255. The compiler knows, however, how to translate back and forth
between characters (represented by a single quotation mark and then a letter, numeral, or
punctuation mark, followed by a closing single quotation mark) and one of the ASCII values.
The value/letter relationship is arbitrary; there is no particular reason that the lowercase "a" is
assigned the value 97. As long as everyone (your keyboard, compiler, and screen) agrees, there is
no problem. It is important to realize, however, that there is a big difference between the value 5
and the character `5'. The latter is actually valued at 53, much as the letter `a' is valued at 97.
A value in any of the built-in types we have see so far can be converted (type-cast) to any of the
other types. For example:
As shown by these examples, the built-in type identifiers can be used as type operators. Type
operators are unary (i.e., take one operand) and appear inside brackets to the left of their operand.
This is called explicit type conversion. When the type name is just one word, an alternate
notation may be used in which the brackets appear around the operand:
In some cases, C++ also performs implicit type conversion. This happens when values of
different types are mixed in an expression. For example:
The above rules represent some simple but common cases for type conversion.
Any place you can put a single statement, you can put a compound statement, also called a block.
A block begins with an opening brace ({) and ends with a closing brace (}). Although every
statement in the block must end with a semicolon, the block itself does not end with a semicolon.
For example
{
temp = a;
a = b;
b = temp;
}
This block of code acts as one statement and swaps the values in the variables a and b.
2.7. Operators
C++ provides operators for composing arithmetic, relational, logical, bitwise, and conditional
expressions. It also provides operators which produce useful side-effects, such as assignment,
increment, and decrement. We will look at each category of operators in turn. We will also
discuss the precedence rules which govern the order of operator evaluation in a multi-operator
expression.
The assignment operator is used for storing a value at some memory location (typically denoted
by a variable). Its left operand should be an lvalue, and its right operand may be an arbitrary
expression. The latter is evaluated and the outcome is stored in the location denoted by the
lvalue. An lvalue (standing for left value) is anything that denotes a memory location in which a
value may be stored. The only kind of lvalue we have seen so far is a variable. For example:
An assignment operation is itself an expression whose value is the value stored in its left
operand. An assignment operation can therefore be used as the right operand of another
assignment operation. Any number of assignments can be concatenated in this fashion to form
one expression. For example:
int m, n, p;
C++ provides five basic arithmetic operators. These are summarized in table below
Except for remainder (%) all other arithmetic operators can accept a mix of integer and real
operands. Generally, if both operands are integers then the result will be an integer. However, if
one or both of the operands are reals then the result will be a real (or double to be exact).
When both operands of the division operator (/) are integers then the division is performed as an
integer division and not the normal division we are used to. Integer division always results in an
integer outcome (i.e., the result is always rounded down). For example:
Unintended integer divisions are a common source of programming errors. To obtain a real
division when both operands are integers, you should cast one of the operands to be real:
The remainder operator (%) expects integers for both of its operands. It returns the remainder of
integer-dividing the operands. For example 13%3 is calculated by integer dividing 13 by 3 to
give an outcome of 4 and a remainder of 1; the result is therefore 1.
It is possible for the outcome of an arithmetic operation to be too large for storing in a designated
variable. This situation is called an overflow. The outcome of an overflow is machine-dependent
and therefore undefined. For example:
It is illegal to divide a number by zero. This results in a run-time division-by-zero failure, which
typically causes the program to terminate.
There are also a number of predefined library functions, which perform arithmetic operations. As
with input & output statements, if you want to use these you must put a #include statement at
the start of your program. Some of the more common library functions are summarised below.
Parameter Result
Header File Function Result
Type(s) Type
<stdlib.h> abs(i) int int Absolute value of i
The assignment operator has a number of variants, obtained by combining it with the arithmetic
For example:
m = 100;
m += n = p = 10; // means: m = m + (n = p = 10);
C++ provides six relational operators for comparing numeric quantities. These are summarized
in table below. Relational operators evaluate to 1 (representing the true outcome) or 0
(representing the false outcome).
The operands of a relational operator must evaluate to a number. Characters are valid operands
since they are represented by numeric values. For example (assuming ASCII coding):
The relational operators should not be used for comparing strings, because this will result in the
string addresses being compared, not the string contents. For example, the expression "HELLO"
< "BYE" causes the address of "HELLO" to be compared to the address of "BYE". As these
addresses are determined by the compiler (in a machine-dependent manner), the outcome may be
0 or 1, and is therefore undefined. C++ provides library functions (e.g., strcmp) for the
lexicographic comparison of string.
C++ provides three logical operators for combining logical expression. These are summarized in
the table below. Like the relational operators, logical operators evaluate to 1 or 0.
Logical negation is a unary operator, which negates the logical value of its single operand. If its
operand is nonzero it produces 0, and if it is 0 it produces 1.
Logical and produces 0 if one or both of its operands evaluate to 0. Otherwise, it produces 1.
Logical or produces 0 if both of its operands evaluate to 0. Otherwise, it produces 1.
Note that here 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:
C++ does not have a built-in boolean type. It is customary to use the type int for this purpose
instead. For example:
The Bitwise operators are used to perform bit-level operations on the operands. The operators are
first converted to bit-level and then the calculation is performed on the operands. In C++, bitwise
operators perform operations on integer data at the individual bit-level. These operations include
testing, setting, or shifting the actual bits. These operators are necessary because the Arithmetic-
Logic Unit (ALU) present in the computer's CPU carries out arithmetic operations at the bit-
level.
1. The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it
2. The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on
every bit of two numbers. The result of AND is 1 only if both bits are 1.
3. The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit
of two numbers. The result of OR is 1 if any of the two bits is 1.
4. The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on
every bit of two numbers. The result of XOR is 1 if the two bits are different.
5. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand,
the second operand decides the number of places to shift.
6. The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first
operand, the second operand decides the number of places to shift.
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
The following table demonstrates the working of the bitwise operation of two integers a=5 and
b=9.
The auto increment (++) and auto decrement (--) operators provide a convenient way of,
respectively, adding and subtracting 1 from a numeric variable. They are equivalent to +=1 and
to -=1, respectively. These are summarized in the following table. The examples assume the
following variable definition:
int k = 5;
The order in which operators are evaluated in an expression is significant and is determined by
precedence rules. These rules divide the C++ operators into a number of precedence levels.
Operators in higher levels take precedence over operators in lower levels.
c * d is evaluated first because * has a higher precedence than + and ==. The result is then added
to b because + has a higher precedence than ==, and then == is evaluated. Precedence rules can
be overridden using brackets. For example, rewriting the above expression as
a == (b + c) * d
Operators with the same precedence level are evaluated in the order specified by the last column
of Table. For example, in a = b += c