The following topics are covered in this section:
• Introduction
• Integer
• Floating Type
• Double
• Character
• Boolean
• Data Type Ranges and determining the ranges
• More on Binary Numbers
________________________________________
Every piece of data has to belong to some basic category. Consider a simple example in real life: every number has to be of a particular type. The number 5 is a natural number (or it can be called as a whole number). 6.5 is a real number (it has a decimal point). Similarly, in programming we have what are called as data types. When a variable is declared, the programmer has to specify which data type it belongs to. Only then will the compiler know how many bytes it should allocate for that particular variable. Or in other words, each data type occupies a different memory size and if a variable is declared as belonging to one particular data type it cannot be assigned a different data type value. In simpler terms, suppose the variable ‘x’ is declared such that it can hold only whole numbers; then it cannot (and should not) be assigned some alphabet.
There are two categories of data types: fundamental data types and user-defined data types. The second category of data types will be dealt with later.
The fundamental (or built-in or primitive) data types are:
• Integer
• Floating Point
• Character
• Double
• Bool
The first three data types: integer, floating point and character are used frequently.
________________________________________
Integer (int):
An integer can contain only digits (numbers) from 0 to 9. Examples of integers are:
• 0
• 10
• 345
• 6789
• -23
• -600
It includes positive and negative numbers but the numbers have to be whole numbers. It does accept the decimal point. Hence the following numbers are not integer data types:
• 3.5
• 4.8
• 0.23
These numbers come under the second category (floating point type) and not under integers. If the program has to accept such values from the user do not declare the variable as an integer. If a variable is declared as an integer and the user enters a value of 2.3, the program will assign 2 as the value for that integer variable. Similarly, if the user enters 3.2, the program will assign 3 to the integer variable.
Remember: Once a variable is declared as an integer, it will only store whole numbers (if the user types a value with the decimal point, the program will ignore everything that is typed after the decimal point).
How to declare a variable as belonging to the type integer? The syntax is:
int variable-name;
Each data type occupies a certain amount of memory space. An integer will occupy 2 bytes of memory (which means 16 bits). From this it is possible to calculate the maximum and minimum values that an integer can store. 2^16 = 65536 (hence 65536 different combinations of 16 bits are possible). Divide this by 2 because integers (by default) range from negative to positive values. We have a 0 in between and so subtract one from this to get 32,767. Hence an integer can take values from –32,768 up to +32,767 (a total of 65536 different values).
A natural question springs to mind, "What would happen if a value greater than 32,767 is entered?" Since this value cannot be accommodated within the allocated two bytes, the program will alter the value. It’s not exactly altering the value; it will basically change your value into something different. The user might enter 123456 as the integer value but the program will store it as –7623 or something like that. Whenever you use variables ensure that you have declared them as belonging to the correct data type.
This restriction on maximum range might seem to be a problem. In C++ ‘qualifiers’ can be used to vary the range of fundamental data types. Qualifiers are only supplements to the basic data types and they cannot be used separately on their own. They work only with a basic (or fundamental) data type. The 4 qualifiers available in C++ are:
1. Short
2. Long
3. Signed
4. Unsigned
Signed and unsigned integers were discussed in the first chapter. When an integer is specified as signed, then automatically the most significant bit of the number is used as a sign bit (to denote the sign of the number). Hence it can be used if the programmer needs positive and negative number values for the variable. By declaring a variable as an integer, by default you can specify both positive and negative values. By default an integer is a signed integer. In other words,
int variable-name;
is the same as
signed int variable-name;
In the second form, ‘signed’ is the qualifier and it is used to explicitly state that the variable is a signed integer. For an unsigned integer the syntax will be:
unsigned int variable-name;
An unsigned integer can hold a value up to 65,535 (a signed integer can hold only up to 32,767). Of course, in an unsigned integer you cannot assign a negative value. The range is from 0 to 65,535. To go beyond 65,535 and make use of both positive and negative values as well, the qualifier long should be used.
long int variable-name;
Long integers occupy 4 bytes of memory (32 bits). Remember, long int actually means signed long int (you can give positive and negative values).
If you specify
unsigned long int variable-name;
you can only assign positive values to the variable. Thus, two qualifiers can be used together with a basic data type.
What about the ‘short’ qualifier? Short integer is the same as a signed integer. It occupies two bytes and has the same range of positive and negative values as the normal integer case.
int x;
is usually the same as
short int x;
Compilers (depending on the operating system) will assume ‘int’ as a ‘long int’ or a ‘short int’. VC++ (since it works in the Windows OS) will default to ‘long int’ if you specify a variable as type ‘int’ (i.e. it will allocate 4 bytes to an ‘int’ variable). Turbo C++ (which is a DOS based compiler) will default to ‘short int’ when you specify a variable as type ‘int’. Thus the statement:
int var;
will allocate ‘var’ 4 bytes if you are using VC++ but the same statement will allocate 2 bytes if you are using Turbo C++ compiler.
Programmers sometimes prefer to explicitly state what type of integer they want to use by making use of the ‘short’ and ‘long’ qualifiers. ‘short int’ always occupies only 2 bytes (irrespective of whether the OS is Windows or DOS) while a ‘long int’ always occupies 4 bytes.
Two qualifiers can be used together, but do not try using:
short long int variable-name;
This will cause a compile-time error. So be careful with what qualifiers you use. And remember that the default for int is equivalent to short signed integer.
Floating Types (float):
Floating type data include integers as well as numbers with a decimal point. It can also have an exponent. Exponent means 10 to the power of some integer value (whole number). 20000 = 2 x 10^4 = 2e4 = 2E4.
If you specify decimal numbers, floating point data type will store up to a precision of 6 digits after the decimal point. Suppose 0.1234567 is assigned to a floating-point variable, the actual value stored would be 0.123457 (it will round up to the sixth digit after the decimal place). Valid floating-point numbers are:
• 0.1276
• 1.23
• 1.0
• 10.2
• 2e5 (this will be typed in your code as 2e5)
Do not use an exponent with a decimal point. For example: 2e2.2 is an invalid floating point because the exponent has to be an integer. Floating point numbers use 4 bytes of memory and has a much greater range than integers because of the use of exponents. They can have values up to 10^38 (in positive and negative direction). The same qualifiers used for an integer can be applied to floating point numbers as well. To declare a floating variable, the syntax is:
float variable-name;
Double (double):
This is similar to the floating-point data type but it has an even greater range extending up to 10308. The syntax to declare a variable of type double is:
double variable-name;
Beware: Visual C++ (VC++) usually uses its default as ‘double’ instead of ‘float’. Suppose we type:
float x=31.54;
you will get a warning message saying that a ‘double’ (i.e. 31.54) is being converted into a floating point. It is just to warn you that you are using a ‘float’ and not a ‘double’. (Even if there are warnings, there won’t be any problem in running your program).
Character (char):
A character uses just one byte of memory. It can store any character present on the keyboard (includes alphabets and numbers). It can take numbers from 0 to 9 only. The following are valid characters:
• A
• B
• 3
• a
• :
• ‘
• /
If the number 13 is entered as the value for a character, the program will only store 1 (i.e it will store the first character that it encounters and will discard the rest). A character is stored in one byte (as a binary number). Thus whatever the user enters is converted into a binary number using some character set to perform this conversion. Mostly all computers make use of the ASCII (American Standard Code for Information Interchange). For example, according to the ASCII coding, the letter ‘A’ has a decimal value of 65 and the letter ‘a’ has a value of 97.
There is another form of coding called the EBCDIC (Extended Binary Coded Decimal Information Code) which was developed by IBM and used in IBM computers. However, ASCII remains the most widely used code in all computers. The table at the end of the book gives a listing of the ASCII values and their equivalent characters. The syntax to declare a variable which can hold a character is:
char variable-name;
Boolean Type (bool)
This data type will only accept two values: true or false. In C++, ‘true’ and ‘false’ are keywords. Actually a value of true corresponds to 1 (or a non-zero value) and a value of false corresponds to 0.
#include
int main( )
{
bool check;
check = true;
cout<
int main( )
{
float PI = 3.14; // variables can be initialized during declaration
int rad;
cout<< "Enter the radius" ; cin>>rad;
cout<< "Area of the circle is "<< PI * rad * rad; return 0; } The preprocessor has only one directive and it will include the iostream.h header file into the source code. The compiler will start reading the code from the main ( ) function onwards. Remember: Whatever is typed within the main ( ) function will be executed. The main ( ) function is used as the entry point for a C++ program. PI is a variable name and is declared as a float quantity (because the value of PI has a decimal point). At the point of declaration, PI is initialized to a value of 3.14. This means that whenever PI is used in the program, the compiler will use 3.14 instead of PI. The line: cout<<"Enter the radius"; will cause Enter the radius to be displayed on the screen. This is because "Enter the radius" is typed within double quotes following ‘cout’ and the insertion operator. Anything between double quotes, along with cout<< will be displayed on the screen just as it appears within the double quote. The value entered by the user will be stored in the variable ‘rad’. Then the statement "Area of the circle is " will be displayed on the screen. The compiler will calculate the value of ‘PI * rad * rad’ and display it at the end (* is the multiplication operator in C++). The output for the above program is: Enter the radius 9 Area of the circle is 254.14 Bold indicates that the user entered the value. In this case 9 was entered as the radius. Suppose we type cout<< "rad"; the output will be just the word rad The value of the variable ‘rad’ will not be displayed. Remember: When you want to display some variable’s value on the screen, DO NOT ENCLOSE IT IN DOUBLE QUOTES; just mention the name of the variable after the insertion operator. ________________________________________ Initializing variables: It is a good idea to initialize variables at the time of declaration. Even if you are unsure of the value you can still initialize it to 0. If you are wondering why, just consider the example below: #include
int main( )
{
int correct, choice;
cout<<"\nEnter your guess of the lucky number: "; cin>>choice;
if (choice= =correct)
{
cout<<"\nCongrags. You are correct!"; } else { cout<<"\nSorry. Wrong guess"; } cout<
int main( )
{
char check;
int i;
cout<<"Enter the character that you want to convert to ASCII : "; cin>>check;
i = check;
cout<<"The ASCII value for "<
int main( )
{
int num1, num2;
cout<<"Enter the two numbers : "; cin>>num1>>num2;
cout<<"The product is : "<
This is a method of obtaining multiple inputs using a single statement. The above statement is equivalent to writing:
cin>>num1;
cin>>num2;
The two numbers, when entered by the user, can be separated by a space or by a new-line (i.e. the first number is typed and then after pressing the ‘enter’ key the second number is typed).
When you run the program you would get the following on your screen:
Enter the two numbers : 8 4
The product is : 32The sum is : 12The difference is : 4The quotient is : 2The remainder is : 0
Something is not right in this output; the results are correct but the display is on a single line. To display the output in an organized manner, the program should print each output on a new line. For this purpose of formatting the output C++ provides us with ‘escape sequence’.
________________________________________
Escape Sequences/ Backslash Character Constants
If you remember, whatever you type within double quotes following cout<<, will be printed as it is on the screen. There is a problem in case you want to print a new line, or you want to use tabs (because whatever you type within double quotes will be displayed directly on the screen). To solve this problem, escape sequences were developed. Just as the name implies, these escape sequence characters are used to escape from the normal sequence of events. An escape sequence always begins with a backslash ( \ ). For a new line, the escape sequence is \n (n for new line). If you want to push the tab setting then \t should be used (t for tab). The modified program for doing simple arithmetic operations is as follows: #include
int main( )
{
int num1, num2;
cout<<"Enter the two numbers : "; cin>>num1>>num2;
cout<<"\n The product is : "<
int main( )
{
char ch;
double db;
float f;
short int i;
db=55e4;
ch = i = f = db;
cout<
2>3 will return a value of False (0)
In programs that you write, comparisons will usually be made between one variable and a constant or between two variables. For example:
x>y
z>10
> means ‘greater than’ while >= stands for ‘greater than or equal to’.
x>=y
will yield a true value even if x = y whereas x>y will yield a value of false when x = y. Be clear as to what relation you want to test when using these operators.
Suppose you want to test whether two variables are equal, you have to make use of the equality operator. The equality operator is denoted by = = (double equal to signs).
Remember: Many beginners in programming use the equality operator and assignment operator interchangeably. The assignment operator is a single ‘equal to’ sign and it is meant only for assigning values to variables. The equality operator (a double ‘equal to’ sign) is used to check whether two values are equal.
Relational Operator Operation Performed Result of Operation
x>y Is x greater than y? True/False
x
x<=y Is x less than or equal to y? True/False x==y Is x equal to y? True/False x!=y Is x not equal to y? True/False We’ll write a simple program for comparing two numbers and displaying the appropriate result. #include
int main( )
{
float num1, num2;
cout<<"Enter the two numbers : "; cin>>num1>>num2;
if (num1>num2)
{
cout<<"The number "<
#include
int main ( )
{
int num;
cout<< "Enter the number"; cin>>num;
cout<<(num>5); //Legal?
return 0;
}
Always remember that the result of a comparison yields a value of TRUE (1) or FALSE (0). Hence the above program is perfectly correct. In case you enter a value that is greater than 5 you will get the output as 1 else you will get 0. 0 is considered as false and all other values are considered to be true.
C++ Operators - IV
The following topics are covered in this section:
• Logical Operators
• Unary Operators
5. Logical Operators - AND ( && ) OR ( || ) NOT (!)
These operators are used to combine two or more expressions. The way in which they combine the expression differs depending on the operation used. They are used when we need to test multiple conditions. For example you may write a program that has to check whether the marks scored by a student is greater than 70 and less than 80. If it is so then you will want the program to display a ‘B’ grade. To check whether the average mark is greater than 70 you have to use one expression and to check whether the average is less than 80 you should use another expression. Thus in simple English your statement will be:
If (average mark is greater than 70 AND average mark is less than 80)
Print "B grade"
AND: it combines two conditional expressions and evaluates to true only if both the conditions are true.
First Condition Second condition Result of AND operation
False False False
False True False
True False False
True True True
If the first condition and the second condition are both true then the result of the AND operation will also be true.
Example:
// To check whether the given number is even
# include
int main ( )
{
int num;
cout<< "Enter the number"; cin>>num;
if ( (num!=0) && ((num%2)= =0) ) // Two conditions have to be true
{
cout<<"\n Even Number"; } return 0; } In this program we need to check for two conditions (the number entered should not be zero and the number when divided by 2 should not produce a remainder). Only if both these conditions are satisfied should the program display that the number is even. The AND operator is used to combine the two conditions that are to be tested and if both are true then the message is displayed. OR: operator combines two conditions and evaluates to true if any one of the conditions is fulfilled (i.e. only one of the conditions need to be true). It is designated by using two parallel bars/pipes ( | | ). First Condition Second condition Result of OR operation False False False False True True True False True True True True The ‘AND’ and ‘OR’ operators can be used on a sequence of conditions (i.e. at a time you can check for multiple conditions). For example the following code is legal: if ( (x>y) && (y>5) && (z>y) && (x>4) )
{
//body of the ‘if’ condition…
}
In this case only if all the four conditions are true will the body of the ‘if condition’ be executed.
NOT: NOT is a unary operator. Unlike ‘AND’ and ‘OR’, NOT operates only on one operand. The logical value of the operand is reversed. If the operand is true, then after the NOT operation it will be become false. You might be thinking that the NOT operator can operate only on 1 and 0. Actually, any number greater than 0 will be considered as a true value. Hence the following would give:
• !5 will give 0
• !0 will produce 1
• !1 is equal to 0.
Condition Result of NOT operation
False True
True False
Basically, any number other than zero is considered as true. ‘Not’ of any number (other than 0) will give you FALSE (or zero). Check out the following program that illustrates the NOT operator.
#include
int main ( )
{
int num, result;
cout<< "Enter the number"; cin>>num;
result = (!num);
cout<
int main ( )
{
int a;
cout<
>=
Equality operator ==
Inequality operator !=
Logical Operator &&
||
Conditional operator ?:
Assignment =
Arithmetic assignment *=, /= , %= , += , -=
Beware: It is better to write long expressions using parentheses otherwise it could lead to a lot of confusion and also to potential logical errors.
Associativity
If two operators have a different priority level then their execution order depends on the operator precedence. What will happen if two operators have the same priority level?
When 2 operators in an expression have the same priority, the expression is evaluated using their associativity. Consider the statement:
net = basic + allowance – tax;
Both + and – have the same priority level. Almost all of the operators except the assignment (and arithmetic assignment) operators have associativity from left to right. The assignment operator has associativity from right to left. Since the + and – binary operators have an associativity of left to right the expression for ‘net’ is the same as:
net = (basic + allowance) – tax;
When you perform multiple assignments:
x = y = 0;
the associativity of the assignment operator (=) is taken into account and thus the order of evaluation will be:
y = 0 (which will give ‘y’ a value of 0) followed by x = y (which will assign 0 to ‘x’).
Comma Operator
The comma operator can accept two expressions on either side of the comma. When executed, the left side expression is first evaluated followed by the right side expression. Ultimately it is the expression on the right side that will be the value of the entire expression.
int x,y;
cout<<(x = 1, y = 5); // 5 will be displayed First the ‘x’ will be assigned 1 and then ‘y’ is assigned a value of 5. The comma operator is equivalent to saying "do this task and do this also". In this case the compiler will do: x = 1 and then y = 5 The rightmost expression is y = 5 and hence the value of the entire expression (x=1,y=5) is 5. Be careful while assigning the value of a comma separated expression to a variable. The comma operator has lower operator precedence than the assignment operator. If we type: y = (x = 1 , x = 5 , x + 10); x will have a value of 5 while y will be 15. If we type: y = x = 1, x = 5 , x+10; the value of ‘y’ will be 1 and that of ‘x’ will be 5. This is because the compiler will perform the following operations: y = x = 1; // y and x are set to 1 x = 5; //x value is 5 x + 10; //the value of the entire comma separated expression is 15 The comma operator will be used usually in ‘for’ loops. Bitwise Operator: The bitwise operators available in C++ are tabulated below: Bitwise Operator Symbol Function & AND individual bits | OR individual bits ^ EXOR individual bits ~ NOT of individual bits (or complement operator) >> Right-shift operator
<< Left-shift operator These operators will operate on individual bits of the operand (i.e. on the binary representation of data). These operators are dealt with in detail in Chapter 13. Remember: Do not confuse the && and & operators or the || and | operators (logical operators are entirely different from bitwise operators). Recap • Arithmetic operators are used to perform mathematical operations. • The modulo/remainder operator is applicable only on integer operands. • Escape sequences are character constants that are used to format the output displayed on the screen. • = is the assignment operator and the Rvalue is assigned to the target (or the Lvalue). • Information will not be lost when converting from a smaller data type to a larger data type. • Shorthand operators are a combination of one of the arithmetic operators and the assignment operator. • Relational operators are used to compare two values or expressions. They will return a value of true or false. • Logical operators are used to combine two or more expressions. • The operator precedence determines which operator has a higher priority. • When 2 operators have the same priority the expression is evaluated based on their associativity. • Bitwise operators are used to operate on individual bits. For Loops in Depth The following topics are covered in this section: • Introduction • For Loop Statement • More of For loops • Body of For loops • Nesting For loops Statement and Expression A statement in C++ refers to a part of the code that is terminated in a semicolon. It is the smallest part of the program that can be executed. For example: sum = a + b; is a statement. A C++ program will consist of a set of statements. An expression is a grouping of variables, constants, function calls and operators (arithmetic, logical, relational) to return a value. In the above example, a + b is an expression. While dealing with control mechanisms you will come across blocks of instructions. A block of instruction refers to a set of statements grouped together within a block. The block is identified by using the curly braces ‘{‘ to start the block and ‘}’ to end of the block. Program flow and control: What is program flow? Whenever we write a program we have to decide on the sequence of operations that the program has to perform. Generally this is represented in the form of an algorithm. Writing algorithms for simple programs might seem trivial but when you write complex programs, the algorithm helps reduce programming errors. Let’s take a look at a simple algorithm to calculate the average weight of 3 persons: Step 1: Start the program. Step 2: Obtain the weight of all 3 persons (in weight1, weight2, weight3). Step 3: Calculate the average weight as average = (weight1 + weight2 + weight3)/3 Step 4: Display the result Step 5: Stop the program By looking at the algorithm a programmer can easily write the entire program. When you have a complex program, if you have written an algorithm then you can eliminate potential logical errors in this stage itself. In the algorithm we define the way in which the program control should flow. First the program should get the 3 inputs from the user, then it should calculate the value for average and finally it should display the result. Thus we can say that the program flow has been clearly defined. But program flow needn’t always be so simple. You might need to take some decisions and alter program flow. Control refers to that part of the program which is currently being executed. Let’s write an algorithm to divide 2 numbers obtained from the user. Step 1: Start the program. Step 2: Obtain the 2 numbers (num and den) from the user. Step 3: Check if den is zero. If it is then go to step 4 else go to step 5. Step 4: Display “Denominator is zero. Cannot perform division”. Go to step 7. Step 5: Calculate the quotient by dividing num by den. Step 6: Display the result. Step 7: Stop the program. In this example, there are 2 routes the program can take. If the user enters the denominator as 0 then the error should be produced else normal division should be performed. The pictorial representation of an algorithm is called a flowchart. Loops Suppose you want to add the numbers from 1 to 10, what would you do? Of course you could write a long statement that would calculate 1+2+3+4+…+10. What if 10 were changed to 100 or 1000? Whenever there are statements to be repeated you can make use of loops (in fact you should make use of loops). When using loops, some condition should be specified so that the loop will terminate (otherwise the set of statements within the loop will keep executing infinitely). For loop statement For loop is used for performing a fixed number of iterations (or repetitions). The syntax is: for (initialize-variables; condition-to-test ; assign-new-values-to-variables) { //statements to be repeated (or the body of the loop) } The ‘for’ loop has three expressions that have to specified. Initialize variables: This expression is executed only once (when the program flow enters the loop for the first time). It is usually used to assign the loop variable an initial value. Condition to test: involves relational operators. It is executed each time before the body of the loop is executed. Only if the condition is true will the loop body be executed. If it is false then the loop is terminated and the control passes to the statement following the ‘for’ loop body. Assign new value to variable: It is executed at the end of every loop after the loop body. Usually it is used for assigning a new value to the loop variable. Example: // To find square of numbers from 1 to 10 #include
int main( )
{
int var;
for (var = 1 ; var<=10; var++) { //beginning of ‘for block’ cout<< "Square of "<
int main( )
{
int i = 2;
for ( ; i<8 ; i++ ) // No initialization of loop variable! { cout<
int main( )
{
int i;
for ( i = 1; i != 2 ; i++ )
{
cout<
int main( )
{
int x,y;
for (x=1,y=1; x<10,y<5 ; x++,y++) { cout<<"\n"<
If you want to check whether the user has entered a ‘y’ or an ‘n’ you can do the following:
if (letter = = ‘y’)
{
//body of if statement
)
You should not compare characters as shown below:
if (letter = = y) //WRONG
This method of comparing a character variable with a character constant is WRONG. It will yield an error. The character constant should always be enclosed in single quotes.
Beware: Beginners often forget the single quotes while using character constants.
//To find the square of a given number
# include
int main( )
{
char reply;
int num, square;
cout<< "Do you want to find the square of a number(y/n)? "; cin>>reply;
while (reply = = 'y') //No blank-space between the two equal signs. See note.
{
cout<<"\nEnter the number : "; cin>>num;
square = num*num;
cout<< "The square is : " <
}
return 0;
}
Note: A blank space has been left between the two ‘equal to’ symbols just to make it clear that there are two equal signs. Do not leave a space between the two = symbols when you write your program.
At the start of the program, if the user types a ‘y’ the program will execute the loop body. At the end of the while loop, the program will prompt the user to enter the value for ‘reply’. If the user again types ‘y’, then the program will execute the while body a second time. Hence, at the end of each loop the program obtains the value for ‘reply’ and checks whether the test condition is true. As long as the test condition is true, the while loop is executed over and over again.
The difference between the ‘while’ loop and the ‘for’ loop is that the ‘while’ loop does not have a fixed number of repetitions of the loop body. As long as condition is true it keeps executing the loop body. Usually the ‘while’ loop is used when the programmer is not sure about the number of iterations that need to be performed. The ‘for loop’ is used when the programmer knows how many iterations are to be performed. Of course, the use of both can be interchanged as well. In fact, a ‘for loop’ can be modeled into a ‘while loop’. The equivalent ‘while loop’ for a ‘for loop’ will be as below:
Initialize a variable value;
while (condition involving the variable)
{
//body of the loop
//assign a new value to the loop variable;
}
If we use the coding:
while(1= = 1)
{
//body of loop
}
the while loop becomes an infinite loop (because 1 is always equal to 1).
Do…While loop
The ‘do…while’ loop is a modification of the ‘while’ loop. If you noticed in the earlier program for the ‘while’ loop, initially we have to ask the user whether he/she wants to find the square of a number. Only if the user types a ‘y’ will the program enter into the ‘while’ loop. Hence in the program we have to ask the user twice whether he/she wants to square a number (once outside the loop and once inside the loop). The ‘do while’ loop, eliminates this repetition.
When you make use of the ‘do-while’ loop, the body of the loop will be executed at least once. After the first iteration, if the loop condition holds true then the loop is repeated again. Thus the first iteration is compulsory in the ‘do…while’ loop.
The program to find the square of a number can be re-written as follows:
#include
int main( )
{
char reply;
int num, square;
do
{
cout<<"\nEnter the number : "; cin>>num;
square = num*num;
cout<< "The square is : " <
}while (reply = = 'y');
return 0;
}
In this program, the body of the loop will be executed once for sure. After the first iteration, the user has the option of terminating the program or continuing to use the program for squaring another number.
Beware: when using the ‘do…while’ loop, make sure that you put a semicolon at the end of the while statement:
while (reply = = 'y');
Decision Statements (IF)
So far we have seen statements that help you in repeating a particular task as long as you desire. Another important form of program flow control is to be able to make a decision as to how you want the program to continue. Statements of this kind are referred to as decision statements (sometimes also called selection statements).
The following topics are covered in this section:
• If else
• Nested If
• Conditional Operator
• If…Else If…else…
‘if-else’ is one of the decision statements available in C++. This enables the programmer to provide different paths for the program flow depending on certain conditions. For example: consider a program for dividing two numbers. Division by zero will lead to an error. To avoid this from happening, after obtaining the two numbers from the user the programmer will want to ensure that the denominator is not zero. Hence there needs to be two different program flow options. If the denominator is zero a message saying, "Division not possible" should be displayed. Otherwise the program should carry out the division and display the results. In simpler terms this can be stated as:
If the denominator is zero, then display a message and do not divide
else perform division and display the output.
Syntax:
if (condition)
{
//statements to be executed;
}
else
{
//statements to be executed;
}
The program flow is as follows: If the condition being tested within the ‘if’ statement is true, then the body of ‘if’ will be executed otherwise the body of ‘else’ will be executed. Thus the program takes a decision as to what it should do next.
You can also make use of the ‘if’ statement alone (‘else’ is not compulsory) or you can even make use of a series of ‘if...else’ statements as follows:
if (condition1)
{
//body
}
else if (condition2)
{
//body
}
else
{
//body
}
Example:
// To find the greater among two numbers
#include
int main( )
{
int a, b;
cout<< "Enter the two numbers you want to compare : "; cin>>a>>b;
if (a = =b)
{
cout << "The two numbers are equal"; } else if (a>b)
{
cout <b)
Even if this is not satisfied then only will it go to the next statement, which is an ‘else’ statement. Since ‘a’ was not greater than ‘b’, and equality was also tested earlier the only possibility is that ‘b’ is greater than ‘a’. Thus if the first two conditions have failed then the program flow will go to the body of the ‘else’ block.
Remember: Using the ‘if…else..if’ format, only one of the bodies will be executed (not all). If one condition is satisfied the rest of the ‘else..if…else’ is ignored.
When we use the ‘if..else if’ construct, if one of the conditions is satisfied that corresponding body will be executed and the rest will be ignored. But what would happen if we use a series of ‘if’ statements alone?
#include
int main( )
{
char letter;
cout<<"\n Enter the alphabet 'a' or 'A': "; cin>>letter;
if (letter= ='a')
{
cout<<"\n You entered an 'a'"; } if (letter= ='A') { cout<<"\n You entered an 'A'"; } return 0; } In the above program, the compiler will check for ‘a’ first and then it will check for ‘A’ also. Even if the first condition is satisfied, it will still check for the second ‘if’ condition. In such cases it would be better to use the following: if (letter= ='a') { cout<<"\n You entered an 'a'"; } else if (letter= ='A') { cout<<"\n You entered an 'A'"; } Now if the user enters an ‘a’ then the program will not enter the ‘else if’ statement to check whether the letter is an ‘A’. Remember: It is better to make use of ‘if-else-if’ instead of a series of ‘if’ statements because in that way your program need not check all the conditions unnecessarily. And whenever you use a series of ‘if-else-if’ statements, test the condition that is most likely to be true first (so that the program need not waste time in checking more conditions). Nested If You can have an ‘if’ statement within another ‘if’ statement. This is known as nested ‘if’. The program flow will enter into the inner ‘if’ condition only if the outer ‘if’ condition is satisfied. In general form nested ‘if’ will be of the form: if (condition1) { //code to be executed if condition1 is true if (condition2) { //code to be executed if condition2 is true } } There can be more than one ‘if’ condition (i.e. you can nest as many ‘if’ statements as you want). An example code fragment is given below (‘month’ and ‘year’ are integer variables whose values are obtained from the user): if (month= =2) { cout<< "The month is February"; if ( (year%4) = = 0) { cout<< "This month has 29 days."; } else { cout<< "This month has 28 days."; } } In the above code fragment, only if the variable ‘month’ is equal to 2 will the program enter into the ‘if’ block. Within this block it will print: The month is February Then it will check as to whether the given year is a leap year or not. If it is a leap year then it will display that the month has 29 days else it will say the month has 28 days. Empty ‘if’ statement: We have seen the use of empty ‘for’ statements but empty ‘if’ statements might not be useful. In fact empty ‘if’ statements are usually logical error (because the programmer places the semi colon by mistake at the end of the ‘if’ statement). Example: if (num1
The value of y will be 20 because ‘a’ is not greater than ‘b’. This sort of expression can be written using the ‘if’ statement as:
a=5;
b=6;
if (a>=b)
{
y = 10;
}
else
{
y = 20;
}
Beware: It’s just that you could reduce the amount of coding by using the ternary operator. Be careful that you don’t confuse the logic while trying to reduce the length of the code!
Decision Statements (Switch Case)
There are many instances wherein you may want to test for a series of conditions one after the other. For example: Suppose you obtain the input of month from the user in as a number and you want to display the corresponding name of the month; what would you do? You could write a series of twelve ‘if….else if….else if…else if…’ statements.
The ‘switch…case’ format provides a convenient alternative to using multiple ‘if-else’ statements when you are testing for a single variable alone. The switch statement successively tests the value of a variable against a list of integer or character constants. If a match is found then the statement associated to that particular case will be executed.
First of all, before entering the switch case statement, we have to obtain the value of the switch variable from the user. The switch variable refers to the variable whose value you want to check. In the case of converting numbers into corresponding months, the switch variable will be the month number.
Syntax:
switch (variable/expression that evaluates to an integer)
{
case char/integer-constant :
{
//body
}
case char/integer-constant :
{
//body
}
}
For example:
# include
int main ( )
{
int month;
cout<< "Enter the month of the year: "; cin>>month;
switch (month) // month is the switch variable
{
case 1 : // if month is 1 then the statements are executed
{
cout<<"The month is January"; break; } case 2 : { cout<<"The month is February"; break; } //write case statements for 3 to 10 just as shown above case 11 : { cout<<"The month is November"; break; } case 12 : { cout<<"The month is December"; break; } default : // If value of day is something other than 1 to 7 { cout<<"You entered an invalid number"; break; } } return 0; } As you can see, the compiler gets the value of the variable ‘month’ from the user. This is called the ‘switch variable.’ If the value of ‘month’ is 1, then the compiler performs what is specified under case 1. If the user enters 2, then the output will be February and so on. Since a year has only 12 months, if the user types 0 or 14 then the program should display that the user has typed the wrong number. The ‘default’ statement is used for this purpose. The ‘default’ statement provides the program with an option to do something in case the switch variable does not match any of the case constants. In this program, ‘month’ is called the switch variable and the integer constants from 1 to 12 are called case constants (i.e. they are the values for which the switch variable is tested). Break: It causes an exit from the switch construct (body). For instance, after printing that "The month is January" you don’t want the compiler to go into the other cases. Hence you ask it to break out of the ‘switch…case’ body. If there is no break for each case then the program will perform all the remaining cases as well. Try it: Remove all the break statements from the above program and execute your program. If you now type a value of 1, the program will print: The month is January The month is February …and so on… Beware: A mistake that beginners commit is that they tend to forget the ‘break’ statement. Always use ‘break’ after each case and also make sure that you have a ‘default’ option in your ‘switch…case’ body. The ‘default’ case is executed only if the user enters a value other than the case constants. It does not matter whether you place the default case at the starting of the switch-case body or at the end (but usually programmers prefer to place the default case at the end of the switch-case construct). Suppose you want to test the switch variable against a set of character constants then ensure that you enclose your case constants within single quotes. Suppose the switch variable ‘month’ is a character then the program would be: switch (month) { case 'a' : //‘a’ is within single quotes because it is a character constant { //body of the case break; } //the remaining cases } Beware: There can be an expression in the switch part but it should evaluate to an integer. The switch variable and the case constants should be integers (or characters). You should not use other data types. Controlling flow within a loop statement We’ve seen a few ways of looping within a program. There are a few occasions when you might want to break out of a loop when some particular condition occurs. Or in other words you may not want to go through the all the iterations within a ‘for loop’. Or you may want to break out of the loop for just one particular value of the loop variable. C++ provides a mechanism to break from loops using the ‘break’ and ‘continue’ statements. These are also called as ‘jump statements’. The following topics are covered in this section: • Break • Continue • Go To • Return • Apply what you've learnt • Recap of the entire Unit Break We’ve already seen the use of the ‘break’ statement in the ‘switch…case’ construct. A ‘break’ statement can also be used to terminate (or break out) from a loop (like the ‘for’ and ‘while’ loops). Suppose you are using nested loops, then a ‘break’ specified in the body of the inner loop will lead to breaking out from the inner loop alone. #include
#include
int main ( )
{
int x, i;
for (i = 0; i<15; i++) { x = rand( ); if (x>500)
{
cout<<"\n Iteration number "<
int main ( )
{
int i;
i = 1;
LOOP : if (i<10) { cout<
int main( )
{
char ans;
cout<<"Enter y/n : "; cin>>ans;
if (ans=='n')
{
goto done;
}
int x=5; //ERROR
done:
cout<
int main( )
{
int d,m,y,days;
cout<<"\nEnter the Date (DD MM YYYY): "; cin>>d>>m>>y;
switch(m) //To print the month
{
case 1:
cout<<"\nJanuary"; days=31; break; case 2: cout<<"\nFebruary"; break; case 3: cout<<"\nMarch"; days=31; break; //…write the remaining cases case 11: cout<<"\nNovember"; days=30; break; case 12: cout<<"\nDecember"; days=31; break; default: cout<<"\nInvalid month"; return 0; //quit the program! break; } cout<<" "<
void add ( ); // Function Declaration
int main( )
{
add( ); // Function Call
return 0;
}
void add( ) // Function header
{
int num1,num2,sum; // Body of the function
cout<< "Input the two numbers : "; cin>>num1;
cin>>num2;
sum = num1 + num2;
cout<<"The sum is "<
void add( )
{
int num1,num2,sum;
cout<< "Input the two numbers : "; cin>>num1;
cin>>num2;
sum = num1 + num2;
cout<<"The sum is "<
void add (int var1, int var2) // Parameters var1 and var2
{
int var3;
var3 = var1 + var2;
cout<<"The sum of the two numbers is "<
cin>>num2;
add(num1,num2); //Arguments num1 and num2
return 0;
}
Two arguments have been passed to add ( ) from the main ( ) function. Observe the function ‘declarator/header’. The parameter specified is ‘int var1’ and ‘int var2’. This means that in the function named ‘add’, ‘var1’ and ‘var2’ are integers that are used within the body of the function. Within the function body we have declared a third integer called ‘var3’.
var1 and var2 are added and stored in var3 using the statement :
var3=var1 + var2;
Now the question might arise, what are the values for var1 and var2? In the main ( ) function the value for two integers ‘num1’ and ‘num2’ are obtained from the user. After obtaining the values, the following statement is encountered:
add(num1,num2);
This statement is the function call. Two arguments namely ‘num1’ and ‘num2’ are passed through this function call. Since the function was declared and defined as having two parameters (and since the data types of the parameters and the arguments are the same), the program will execute the code of the add ( ) function with the value of ‘var1’ being that of ‘num1’ and value of ‘var2’ being equal to ‘num2’. If the data types or number of arguments passed do not match with the parameters, the compiler will produce an error message.
Effectively, var1=num1 and var2=num2. The body of the function ‘add’ is now executed with the values of ‘var1’ and ‘var2’. Thus the arguments are passed from the program (which is the main ( ) function) to the parameters of the function ‘add’.
The variables used to hold the argument values are known as parameters. The function declarator in the function definition specifies both data type and name of parameters. In our example, the parameters were ‘num1’ and ‘num2’. Their data types were int (integer).
Remember: The values of ‘num1’ and ‘num2’ are passed to ‘var1’ and ‘var2’. The function is operating only on ‘var1’ and ‘var2’ (it does not operate on ‘num1’ and ‘num2’).
If we had declared the add ( ) function in the program, it would have been as follows:
void add (int, int);
In the function declaration you don’t have to specify the parameter names (just specifying the data types of the parameter is sufficient).
Remember: You pass arguments to parameters.
A closer look at Functions
The following topics are covered in this section:
• Default Arguments
• Return Values
• Returning void
• int main and void main ( )
• exit( )
• using return to break out from loops
Default Arguments
While using functions with parameters, you can specify default argument values (i.e. in case arguments are not passed to the function then the function will assign the default values to the parameters).
#include
void add (int var1=5, int var2=10) // default values 5 and 10
{
• int var3;
var3 = var1 + var2;
cout<
int add (int , int); // Declaration
int main ( )
{
int sum, var1, var2;
cout<< "Enter the two numbers"; cin>>var1>>var2;
sum = add(var1,var2); //Function call
cout<< "The sum of the two numbers is "<
void convert(int dollar)
{
if (dollar<0) { cout<<"\nCan't convert negative amount!"; return; } cout<<"\nThe equivalent money in Rupees is : "<<46*dollar; } int main( ) { int amount; cout<<"\nEnter the amount in dollars you want to convert: "; cin>>amount;
convert(amount);
return 0;
}
The output when you type a positive value is:
Enter the amount in dollars you want to convert: 5
The equivalent money in Rupees is : 230
The output when you enter a negative value is:
Enter the amount in dollars you want to convert: -1
Can't convert negative amount!
As can be seen from the output in the second case, once the return statement is encountered the program will stop executing the function (thus it does not calculate the value of 46*dollar if the value of dollar is negative).
void main ( ) and int main ( )
The main( ) function can be written in one of two ways. Either you can use:
void main ( )
or you can use:
int main ( )
{
//code
return 0;
}
int main ( ) returns an integer value while void main ( ) doesn’t return anything. Consider the following program:
# include
int main ( )
{
int test;
cout<<"Enter a value :"; cin>>test;
if (test= =1)
{
cout<<"You typed 1"; return 1; } cout<<"This line is displayed if you typed a value other than 1"; return 0; } What do you think happens when the user types 1 as the value of ‘test’? Since ‘test’ is equal to 1, the compiler will go into the body of if. It will display "You typed 1" on the screen. Then the compiler will return a value of 1. Generally a return value of a non-zero number is used when errors are encountered. You can't use this return value anywhere else in your program (Why? Because the caller for the main ( ) function is your operating system). In other functions (functions other than ‘main’ ), you can make use of the return value. After returning the value, the program exits. This means that the compiler will not even read the remaining few lines and hence nothing else will display on the screen. The compiler will quit once any integer has been returned by the main ( ) function. If you type anything other than 1 for test, the compiler will skip the ‘if’ body, display the last statement and then return 0. Better to use int main ( )? We could now delve one step further into the topic. Which one is better? Or which one should we use and why? Void is generally used to declare functions that do not return values. When you use int main ( ), the main function returns an integer to the operating system (since the OS is what calls the program and in turn the main function). Returning a value from main ( ) is like an exit function. The value is returned to the operating system and the program terminates. What will the OS do with your returned value? Actually, the OS never wants to know what you return. The program which started your program might need to know. In any OS, a program when running is called a "process". When booting up, the operating system starts the first process (called "init" in UNIX systems). Thereafter the first process starts other processes such as a shell (shell is just a program, which reads commands from the user and converts it to system calls). So when you write a program and execute it in the shell, the shell starts your program. So now, the shell is the parent process of your program (and your program is the child of the shell). Now, in the same way, suppose you want one of your programs to load another program to do a particular job and you want to know just whether the job was successful or not, then the OS gets the exit code of the child process and gives it to the parent process; just like returning from a function. So it is a standard to give the exit code as '0' for a success and any non-zero integer for an error. When programming in C/C++ you can give this exit code when you return from the main function. So you've to declare main as 'int main( )' to do that. If you declare it as 'void main( )' C++ wont allow you to set a value to be returned to your parent program. So the variable which should contain the return code will be filled by nothing, which means that memory can be in any state (unpredictable). Hence you have no control on what value your parent process gets when your child program exits. This is not just for UNIX, it holds good for MSDOS and Windows too. In the UNIXes the shell is usually 'sh' or 'bash'. In MSDOS the shell is called 'command.com'. In Windows the shell is 'explorer.exe'. Hence it's always better to use int main ( ) along with a return value at the end of the main ( ) function. A return value of zero indicates a normally terminated program. A non-zero value is used in case of errors. Of course this does not mean that a program written with void main ( ) won’t work; but it is better to avoid writing such programs. exit() There is a function called exit ( ) which can be used to terminate (or break out from) the program. The function is: void exit (int r); where ‘r’ is an integer value returned to the OS. Usually a value of 0 is used to indicate normal termination while a non-zero number is used in the case of abnormal termination (due to errors). Thus, the syntax (or function call) will be: exit (0); or exit (1); Instead of using integers we can also use two predefined macros: EXIT_SUCCESS (which is equivalent to 0) or EXIT_FAILURE (which is a non zero number). The exit ( ) function is declared in the stdlib.h library file. Hence you should type: #include
in case you want to use this function.
________________________________________
Using ‘return’ to break out from loops
The ‘return’ statement will return program flow control to the caller and this feature can be used to break out from loops. An example program is given below:
#include
int display( )
{
int i;
for (i=0;i<10;i++) { if (i!=5) { cout<<"\n"<
int main( )
{
int choice;
cout<<"\nWelcome to prog1"; cout<<"\nEnter 1 to skip 2nd program:"; cin>>choice;
if(choice==1)
{
return 5;
}
else
{
return 0;
}
}
Our second program just displays a statement to indicate that we are executing the second program.
//prog2.cpp – This program simply displays a statement on the screen
#include
int main( )
{
cout<<"\nCongrags.You are in the second program!"; return 0; } Build the two executable files: prog1.exe and prog2.exe. Note: If you are using VC++, the exe files will be created in a folder called “Debug” within your project. Turbo C++ will create the exe files in the same folder itself. So, now we have 2 programs but how do we conditionally execute the 2nd one depending on the return code of the first? The answer lies in batch programming (if you are using DOS) or in shell programming (if you are using Unix/Linux). We’ll deal with batch programming. This is basically the creation of *.bat files (so far we’ve been creating *.exe files). I won’t go into this topic deeply but I’ll cover a bit of it so that you can appreciate return codes from programs. Before starting to write a *.bat file, you can copy the 2 exe files we created (prog1.exe and prog2.exe) into the same folder (I’ve copied them into my C:\). I assume that you have used a bit of MS-DOS (at least you should be familiar with the command prompt, changing directories etc.). Command prompt: To get to this from Windows go to START->RUN and type “command” in the pop-up box. You’ll be taken to the DOS prompt (this is the place from where you can give commands to DOS). The prompt on my system is:
C:\MYWIN\Desktop>
Now type cd\ to go to C:\
C:\>
Let’s create a batch file named combo.bat. To do this simply type:
C:\>edit combo.bat
You’ll be taken to an MS-DOS text editor (similar to Notepad in Windows). Type the following in the file, save it and return back to DOS.
@ECHO OFF
ECHO **** WELCOME TO BATCH PROGRAMMING ****
ECHO Executing the prog1
prog1
IF errorlevel 5 GOTO end
prog2
:end
ECHO End of batch file
Perhaps everything seems weird?
Remember: MS-DOS command.com is not case-sensitive (i.e. typing DIR or dir is the same).
The first 3 lines of our batch file are used for the purpose of displaying something on the screen (equivalent to cout<< in C++). On the 4th line we say: prog1 This is equivalent to executing the program ‘prog1.exe’ on the command prompt. Batch files are used basically to execute a set of instructions/programs in a particular sequence. If every time you log into the system, you want to perform 10 commands, then each time you’ll have to keep typing the 10 commands on your prompt. You can save time (and also needn’t worry about remembering the sequence of commands) if you write those 10 command in a batch file. Now, each time you log into the system, you just need to type the name of the batch file and voila! (all your 10 commands will be executed faithfully). Coming back to our program, prog1 in the batch file will execute our first program. You’ll see the display: Welcome to prog1 Enter 1 to skip 2nd program: 1 Let’s assume the user types 1. According to our program: if(choice==1) { return 5;| } Now prog1 returns a value of 5 to the caller. The caller is our batch program combo.bat. The next line of the batch program checks for this return code. IF errorlevel 5 GOTO end The IF condition becomes true if the previous step had a return value of 5 or greater. In our case the previous step was the execution of ‘prog1’ and this returned a value of 5. Thus the condition is true and the combo.bat will go to the section labeled ‘end’. Here we just display a statement saying that it’s the end of the batch program (echo is used to display on the screen). If the user had entered some other value then the batch file would have executed prog2 since the return value would have been less than 5 (in our case it was 0) and so the IF condition would be false. The output if you entered 1 would be: C:\>combo
**** WELCOME TO BATCH PROGRAMMING ****
Executing the prog1
Welcome to prog1
Enter 1 to skip 2nd program:1
End of batch file
C:\>
The output if you entered some other number would be:
C:\>combo
**** WELCOME TO BATCH PROGRAMMING ****
Executing the prog1
Welcome to prog1
Enter 1 to skip 2nd program:3
Congrags.You are in the second program!End of batch file
C:\>
Just follow the above steps, create the batch file and try it out on your system. To execute batch files you don’t need to compile the file (i.e. combo.bat can be directly executed). This is because the command prompt is an interpreter (it executes commands one at a time and does not require compilation as we do for C++ programs).
In Unix/Linux, this is called shell programming (and instead of batch files we call them shell scripts). In large applications, you would need to execute a series of programs everyday and these are written in shell scripts. The execution of programs would depend on the return code of the previous program (if the previous program failed because of some error then you might not want to continue execution of the remaining programs). By now you should have understood about the difference between void main( ) and int main( ).
Note: If you are using Unix/Linux then refer to the Appendix for the above section.
A closer look at Functions
The following topics are covered in this section:
• Types of Functions
• Function Overloading
• Apply what you've learnt
Types of Functions
There are two types of functions namely:
1. Library Functions
• The declaration of library function is in the header file specified at the beginning of the program.
• The definition is in a library file that is automatically linked to the program
• Declaration and definition are not required. We will only call the function. Example : rand( ), clrscr ( ), exit( ) etc.
2. User Defined Functions
• Declaration and definition are part of the source file (*.cpp file).
• Function definition and declaration have to be written by the programmer.
• Example of user defined functions is the add ( ) function that we used in the previous section.
There are many functions which are provided by the compiler. These functions that come with the compiler are called as library functions. The prototype for the library functions will be present in some header file. To use a library function, you only need to include the header file (where the function prototype exists) and should know the name of the function.
It is common for programmers to write coding for functions and then use them later in some other program when needed. In fact in C programming, this was quite common. You can do the same in C++ as well. You could define a set of general-purpose functions in a header file and include the header file in the programs where you want to use those defined functions (by specifying a function call statement). But using functions in this way can lead to some problems, which will be discussed later (the main problem is when there is a clash of function names). In C++ we make use of ‘classes’ and reuse classes, rather than functions directly.
Using library functions: rand( ), srand ( ), time( )
There may be instances wherein you will want to generate numbers randomly. For example if you are simulating a game played with dice then you should be able to produce numbers between 1 to 6 randomly (corresponding to the 6 faces of a dice). Or if you want to simulate the tossing of a coin you should be able to retain the randomness of the event (i.e. these are events which you can’t predict. It could be a head or a tail).
We can make use of the rand ( )library function to perform such tasks. The rand ( ) function is defined in the stdlib.h library. Let us write a program to toss a coin and ask the user for his/her choice. If the user’s choice and the result of the toss are the same then the user wins the game.
#include
#include
int main( )
{
int move;
char choice;
do
{
cout<<"Enter 1 for head and 0 for tail: "; cin>>move;
if (rand( )%2= =move)
{
cout<<"You win."; } else { cout<<"You lose."; } cout<<"\n\nDo you want to play again? "; cin>>choice;
}while(choice= ='y');
return 0;
}
The rand ( ) function will generate integers upto a maximum of 32767. But in the case of tossing a coin we have only two possibilities (a head or a tail). To scale down 32767 to two cases we divide the rand ( ) value by 2 and use the remainder (the remainder will either be 1 or 0). Thus we assume that 1 is head and 0 means tail in the above program. The output for the above program will be:
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.
Do you want to play again? n
If you run the program again a second time the result will be:
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.
As you might have noticed the sequence is the same (i.e. each time the program is run the same set of random numbers are produced). This is because the random number is generated in a sequence and unless you ask the computer to start the sequence from a different position it will always keep starting at the same place. We have another function called srand( ) than can be used to alter the start of the sequence. To do this just add the satement:
srand( time(0) );
before the ‘do’ statement. The function time ( ) is defined in ‘time.h’ header file. The output will now be:
Enter 1 for head and 0 for tail: 1
You win.
Do you want to play again? y
Enter 1 for head and 0 for tail: 1
You lose.
If you run the program there is a good chance of getting a different result for the same inputs. This is because we are setting the random generator differently each time the program is run. The function time (0) will give the present system time in seconds. Each time you run this the time (0) value will be different.
Suppose you want to simulate the throw of a dice then you should scale down the outcomes of the rand( ) function to 6. For this (instead of rand( )%2, you could use the statement:
( ( rand( ) % 6 ) + 1 )
We have to add 1 because rand ( )%6 will produce values between 0 and 5 but we need values from 1 to 6.
Function Overloading
Can two functions have the same name?
More than one function can have the same name but they should have different number of parameters or the types of the parameters should be different. This is known as function overloading. The name of a function along with its parameter data types forms the function signature. The signature helps differentiate between two functions. In function overloading the signatures of the functions with the same name will be different.
In general, overloading is the process of assigning several meanings to a function (or an operator as in operator overloading, which we will discuss in a later chapter). Consider the example given below:
// A PROGRAM TO ILLUSTRATE FUNCTION OVERLOADING
void display( ); // Function declaration – This function has no arguments
void display (char); // One argument
void display (char, int); // Two arguments
int main( )
{
display ( );
display (‘=’);
display (‘+’, 30);
return 0;
}
void display( )
{
cout<< "Hi!"; } void display (char ch) { cout <
#include
int menu( ); //provide a menu for the user
void fibo( ); //function for generating fibonacci series
void fact( ); // to find the factorial of a number
int main ( )
{
int ch;
while (1= =1) //an infinite loop which can be broken
{
system("CLS"); //to clear the screen before the menu is displayed
ch=menu( );
switch (ch)
{
case 1:
fibo( );
break;
case 2:
fact( );
break;
case 3:
cout<<"\n Program Terminated."; break; default: cout<<"\n Invalid choice."; break; } if (ch= =3) { break; //break from while loop } } return 0; } //All the function definitions below: int menu( ) { int choice; cout<<"\n\n\n Welcome to my Program"; cout<<"\n 1.) Generate a Fibonacci Series."; cout<<"\n 2.) Find the factorial of a number."; cout<<"\n 3.) Exit."; cout<<"\n\n Enter your choice : "; cin>>choice;
return choice;
}
void fibo( )
{
int max, sum, a1, a2;
cout<<"\n\nHow many terms do you want in the series? "; cin>>max;
a1=1;
a2=1;
cout<<"1,1"; for (int i=2;i
for (i=num;i>0;i--)
{
result = result*i;
}
cout<<"\n\n The factorial of "<
int fact(int n)
{
int result;
if (n= =1)
{
return 1;
}
else
{
result = n * fact(n-1);
return result;
}
}
int main( )
{
int num;
cout<<"\nFor what function do you want to find the factorial : "; cin>>num;
cout<<"\n\n The result is : "<
using namespace std;
void backwards( )
{
char ch;
ch=getchar( );
if (ch!='\n')
{
backwards( );
}
cout<
inline double dollartors (double d) // The function is inline.
{
return 47*d; // Inline functions usually contain only one/two body lines
}
int main ( )
{
double dollar;
cout << "How many dollars :"; cin>>dollar;
cout<< "Rupees : "<
cout<< "Rupees : "<< 47*d; //Inline function body substituted return 0; } The advantage is that the program needn’t save its present memory address, go to the function’s memory address, execute the function and return back to the original address. Instead the function code is brought into the main program and it is executed just like a normal C++ statement. Thus processing time can be reduced significantly. Remember: It is useful to make a function inline if its body consists of just one or two lines. Recap • A function is a group of statements written for a specific purpose and grouped within a single block. • A function has to be declared if it is called before being defined. • Arguments are passed to a function’s parameters. • The data type of the arguments should match the data type of the parameters. • If a function is declared as returning a value to the caller then it should return the corresponding data type value (unless it returns void). • The return statement is used to return a value to the caller as well as give back program control to the caller. • The main ( ) function returns value to the OS. • Overloaded functions should have the same name but different parameters. • Return data type cannot be used as a basis for function overloading. • Inline functions are used when the body of the function is very small (one or two lines). • Recursive functions are functions that call themselves. Some provision has to be provided for them to break out of recursivity. • Recursive functions have more overheads and are generally not used. More On Data Types and Variables The following topics are covered in this section: • Scope of Variables • Storage Classes Scope of variables Scope refers to the region where something is valid or the region where something can exist. Variables in C++ have a defined scope, which depends on the way they are declared. There are three places where a variable can be declared: as local variables, formal parameters and global variables. Remember: In C we can declare variables only in the starting of the function. In C++, we can declare variables anywhere within the program. Local Variables Variables declared within a function are called local variables (sometimes called automatic variables). These variables can be used only within the block (or function) in which they are declared. A block starts with an opening curly brace and ends in a closing curly brace. A local variable is created upon entry into the block and destroyed when the program exits that block. If you create a variable in the main ( ) function then it can be used only within the main ( ) function. That variable cannot be accessed by some other function that you may have created. For example: void test ( ) { // Start of block int q; q = 2; } // End of block void test2 ( ) // Start of another block { int q; q = 5; } The two q's declared in the two functions (test and test2) have no relationship with each other. Each q is known only within its own block (since it is a local variable). The main advantage is that a local variable cannot be accidentally altered from outside the block. Try it: Compile the following piece of code in your compiler and check the results int main( ) { int outer; { int inner; cout<<"enter the outer variable value : "; cin>>outer;
}
cout<<"\n Enter inner variable value : "; cin>>inner;
return 0;
}
What do you think will happen? The above program will lead to a compile-time error. Variables are visible only within the block of code where they are declared (unless they are global variables). The coding enclosed within the two braces is called as a ‘block’ of code. Thus:
{
int inner;
cout<<"enter the outer variable value : "; cin>>outer;
}
is a block of code within which we have declared an integer variable ‘inner’. This variable ‘inner’ is not visible outside of this block. Thus the statement:
cin>>inner;
will lead to an error because ‘inner’ is only known inside this block and not outside the block.
Note the difference between the two codes given below:
int main( )
{
int i=5;
{
int i; //This ‘i' is only visible within this block.
i=6;
}
cout<
int count; // count is a global variable
void increment( )
{
count=count+2;
}
int main ( )
{
count=1;
• increment( ); //count is now 3
count=count+1; //count is now 4
}
The above program is not complete but you can see that two functions (increment and main) can access the same variable ‘count’.
count=1;
The increment ( ) function increases the same count value by 2. Now count is 3 and finally the main ( ) function increases count by 1. The final count value is 4. This is a case of more than two variables accessing the same global variable.
Remember: Global variables will take up more memory because the compiler has to always keep it in memory. Avoid using too many global variables. Use it only if the variable is going to be used by a number of functions.
What would happen if a program uses an identifier as both local and global variable name? Consider the program below:
#include
int var=55; //Global variable
int main( )
{
int var=20; //Local variable with same name
cout<<"\nGlobal variable value is : "<<::var; var=var+1; cout<<"\nLocal variable value is : "<
#define PI 3.14 //Macro definition
int main( )
{
cout<
int main ( )
{
int test[20]; //we assume that the maximum limit is 20 numbers.
int size,i,j;
int temp;
cout<<"How many numbers do you want to compare : "; cin>>size;
cout<<"Enter the numbers you want to check : "; for(i = 0; i
}
for(i = 0; i
{
temp = test[ i ];
test[ i ] = test[ j ];
test[ j ] = temp;
}
}
}
cout<<"The numbers in ascending order are : "; for (i = 0; i
int main ( )
{
char name[15];
cout<< "Enter your name : "; cin>> name;
cout<< "Your name is "<
int main ( )
{
char name[20];
cout<<"Enter your full name : "; cin>>name;
cout<<"You entered your full name as : "<
int main( )
{
char name[10];
cout<<"Enter the name: "; cin.get(name,10); cout<
int main( )
{
char name[80];
cout<<"Enter the name: "; cin.get(name,80,'*'); cout<
}
}
The outer ‘for’ loop starts with a value of 0.
i = 0
Corresponding to i=0 we will have two values for j (0 and 1). Hence with this you can get the values for a[0][0] and a[0][1]. This corresponds to the first row of the matrix. For the second row, the ‘i’ loop will execute a second time with a value of 1. Hence you can get the values for a[1][0] and a[1][1].
In the cout statement we have mentioned ‘i + 1’ and ‘j + 1’. This is just for the purpose of display. Remember that the compiler will start numbering from zero. The first element for the compiler will be the 0 x 0 element. For the user it is better if you refer to the first element as 1 x 1 rather than referring to it as 0 x 0.
Similarly, two ‘for’ loops can be used to display the values of a two-dimensional array.
Initializing multi-dimensional arrays:
Initializing a 2-D array is similar to that of a 1-D array.
int marks[2][3]={ 40,50,60,
70,80,90};
The above notation is used for readability. You might as well initialize the array as:
int marks[2][3]={ 40,50,60, 70,80,90};
Thus if you use the following initialization (hoping that marks[0][2] will be 0):
int marks[2][3]={ 40,50,
70,80,90};
the compiler will treat it as:
int marks[2][3]={ 40,50,70,80,90};
and marks[0][2] will be 70 while marks[1][2] will be 0. Only trailing elements will be automatically initialized to 0. There is a better way to initialize multi-dimensional arrays. The following initialization:
int marks[2][3]={
{40,50},
{70,80,90}
};
actually produces the result we were looking for earlier. Now, marks[0][2] is zero. The additional pair of parentheses makes a big difference. Readability is improved further and now the compiler will set the trailing elements (which haven’t been initialized) in each row to 0. In our case, only the 3rd element of the first row is missing and hence this is initialized to zero.
You might recollect that in one dimensional arrays we could specify:
int a[] = {1,2,3};
and the compiler would translate this into:
int a[3] = {1,2,3};
The question arises as to whether we can extend this to 2-D arrays as well:
int marks[ ][ ]={
{40,50},
{70,80,90}
};
This will give a compile-time error. The compiler wouldn’t know how many columns you want to specify for the array (you’ll understand this concept when we deal with pointers and 2-D arrays in the next chapter). But for the time being remember that you can forget the 1st dimension but shouldn’t leave out the subsequent ones.
Let’s go one step further. What is a 3-D array?
int a[5];
int ab[2][5];
int abc[3][2][5];
‘ab’ is a 2-D array which consists of 2 one dimensional arrays (each of which can hold 5 elements). ‘abc’ is a 3-D array which consists of 3 two dimensional arrays (each of the 2-D arrays contains a 1-D array which can hold 5 elements). The concept can be extended to higher dimension arrays as well (but generally we wouldn’t use more than 3 dimensions).
How do we initialize a 3-D array?
int abc[3][3][2]={
{
{40,50},
{10,70},
{20,30}
},
{
{45,55},
{15,75},
{25,35}
}
};
The parentheses make the initialization pretty clear. Of course you can remove all the braces but the declaration wouldn’t be easy to understand. Again in the case of a 3-D arrays, you can drop the first dimension but should mention the other 2.
The following declaration is legal:
int abc[ ][3][2]={
{
{40,50},
{10,70},
{20,30}
},
{
{45,55},
{15,75},
{25,35}
}
};
You’ll have to take care of the braces. In the above example:
abc[0][0][0] = 40
abc[0][0][1] = 50
abc[0][1][0] = 10
abc[0][1][1] = 70
abc[0][2][0] = 20
abc[0][2][1] = 30
What happens in the following declaration?
int abc[ ][3][2]={
{40,50},
{10,70},
{20,30},
{45,55},
{15,75},
{25,35}
};
All that we’ve done is removed the braces which were used to denote that the 1st dimension was 2. But the compiler isn’t smart enough to know what’s on our mind and now it would create the array as: int abc[5][3][2].
abc[0][0][0] = 40
abc[0][0][1] = 50
abc[0][1][0] = 0
abc[0][1][1] = 0
abc[0][2][0] = 0
abc[0][2][1] = 0
abc[1][0][0] = 10
abc[1][0][1] = 70
abc[1][1][0] = 0
abc[1][1][1] = 0
abc[1][2][0] = 0
abc[1][2][1] = 0
and so on.
Moral of the story is that you should do your best to make things explicit when dealing with computers rather than assume that the computer would think the way you’re thinking.
Passing an Array to a Function
This topic is dealt with in depth when discussing about pointers. A simple method of passing arrays to functions is illustrated below: #include
void disp(int a[ ] )
{
for (int i=0;i<3;i++) { cout<
#include
int main( )
{
int i , j , r1 , r2 , c1 , c2 , a[20][20] , b[20][20] , c[20][20];
cout<<"Enter the number of rows and columns of first matrix :"; cin>>r1>>c1;
cout<<"Enter the number of rows and columns of second matrix :"; cin>>r2>>c2;
// If the matrix orders are not equal then addition is not possible
if ( (r1! = r2) || (c1!=c2) )
{
cout<
}
}
for (i = 0; i
}
}
cout<
cout<<"Enter the city : "; cin>>a1.city;
cout<<"Enter the telephone number : "; cin>>a1.tel;
cout<<"\nThe size of the structure variable is : "<
cout<<"Enter the city : "; cin>>a1.city;
cout<<"Enter the telephone number : "; cin>>a1.tel;
cout<<"Enter the date of birth (day, month and year) : "; cin>>a1.birthday.day>>a1.birthday.month>>a1.birthday.year;
cout<<"\nThe size of the structure variable is : "<
union shirt
{
char size;
int chest;
int height;
};
int main( )
{
shirt mine;
cout<<"\nSize of the union is : "<
cout<<"\nThe size is : "<
cout<<"\nThe size is : "<
cout<<"\nThe size is : "<
int main ( )
{
int var=100;
cout<<"Value : "<
int main( )
{
int marks[3];
int* p;
p = &marks[2]; // Pointer points to the third element of array.
marks[0]=58;
marks[1]=61;
marks[2]=70;
p = p-1; //pointer decrements by one, goes to the previous integer address
cout<
void clear(int *point, int size)
{
for (int i=0;i
int square (int x)
{
return x*x;
}
int main ( )
{
int num = 10;
int answer;
answer = square(num);
cout<<"Answer is "<
void square (int *x)
{
*x = (*x) * (*x);
}
int main ( )
{
int num = 10;
square(&num);
cout<<" Value of num is "<
int main( )
{
int x;
int &ref = x; //ref is a reference variable
x=5;
cout<
void swap (int &x, int &y) //pass by reference
{
int t;
t = x;
x = y;
y = t;
}
int main ( )
{
int a , b;
cout<<"Enter the value for a : "; cin>>a;
cout<<"Enter the value for b : "; cin>>b;
cout<<"a and b before swap are : "<
int* create( )
{
int marks[3];
int *pt=marks;
for (int i=0;i<3;i++) { marks[i]=80; } return pt; } int main( ) { int *p; p=create( ); cout<
int main ( )
{
int size,i;
cout<<"Enter the size of the array : "; cin>>size;
int marks[size]; //WRONG
cout<<"\nEnter the marks: "; for (i = 0; i
}
cout<
with:
size=4;
Now everything should be fine. Is it so? Try it and you’ll get the same compiler error. The compiler reads and stores the value of 4 for ‘size’ but it still will not substitute that value in:
int marks[size];
The compiler assumes that ‘size’ is a variable (since it was declared like that) and since a variable’s value can change in the program, it will not compile the code. One way to correct this is by declaring the maximum size of the ‘marks’ array by saying:
int marks[4]; //program will compile
There is no need for the variable ‘size’ and the user can enter only a maximum of 4 values because that is the space allocated for the array ‘marks’.
Another way to correct the program is to declare the variable ‘size’ as a constant.
const int size=4;
Now you can use:
int marks[size];
The reason that this is valid is because the compiler makes note of the fact that ‘size’ is a constant and has been given a constant value 4. Since this value will never change in the program and space will be allocated for 4 elements in the array ‘marks’. C programmers made use of macros (instead of ‘const’):
#define SIZE 4
to define constants. When the compiler comes across the term ‘SIZE’ anywhere in the program it will simply substitute the value of 4 for ‘SIZE’.
But whatever you do, the compiler limits you to fixing the size of the array at compile-time. Can you decide the array size at run-time? Dynamic allocation comes to the rescue.
Dynamic allocation means allocating (or obtaining) and freeing memory at run-time. There are certain cases where run-time decisions are better. For example, deciding the size of an array. Similarly you can free up allotted memory in your program when you don’t need a particular array by deleting the entire array itself. The two operators used for this purpose are: ‘new’ and ‘delete’.
‘new’ is used to allocate memory while ‘delete’ is used to free the allocated memory (memory which was allocated by ‘new’). The free memory available is sometimes referred to as the heap. Memory that has been allocated by ‘new’ should be freed by using ‘delete’. If you don't use ‘delete’ then the memory becomes a waste and cannot be used by the system. This is called memory leak (i.e. when allocated memory is never returned to the heap). You could go on taking memory from the heap till it gets exhausted. This will lead to memory leak and can cause problems to your program and to other programs which attempt to take memory from the heap.
The syntax is:
data-type * name = new data-type;
delete name ;
Beware: Both data types should be the same.
Example:
int * p = new int;
delete p;
Remember: delete p;
means that the data pointed to by ‘p’ is deleted. The pointer ‘p’ will not get deleted. The following coding is correct:
int m = 20;
int *p = new int;
*p=5;
cout<<*p<
int *marks = new int[size];
cout<<"\nEnter the marks: "; for (i = 0; i
}
cout<
int sum(int a, int b)
{
return (a+b);
}
void func(int d, int e, int (*p1)(int,int))
{
cout<<"The result is : "<<(*p1)(d,e); } int main( ) { int (*p)(int,int); p=sum; func(5,6,p); return 0; } The output is: The result is : 11 Though the program is simple a few statements might appear confusing. The statement int (*p) (int,int); declares a pointer to a function that has a return value of integer and takes two arguments of type integer. sum( ) is a function with return data type of integer and also with two arguments of type integer. Hence the pointer ‘p’ can point to the function sum ( ). Thus we assign the address of the function sum ( ) to ‘p’: p = sum; We’ve also defined another function called as ‘func ( )’ which takes two integer arguments and a third argument which is a pointer to a function. The idea is to pass the function sum ( ) to the function ‘func( )’ and call the sum ( ) function from func ( ). func(5,6,p); will call the func ( ) function and it will pass pointer ‘p’ to func ( ). Remember that p is a pointer to sum ( ). Hence in reality we are actually passing a function as argument to another function. Let’s expand the program one step further by creating another function called product( ) which will return the product of the two arguments. #include
int sum(int a, int b)
{
return (a+b);
}
int product(int x,int y)
{
return (x*y);
}
void func(int d, int e, int (*p1)(int,int))
{
cout<
p->tel=23451;
p=p+1; //Now points to record[1]
p->pin=50023;
p->tel=89732;
p=record;
cout<
cout<
p=p+1; //Points to record[1]
cout<
cout<
return 0;
}
The output is:
The pincode is : 60004
The tel. no. is : 23451
The pincode is : 50023
The tel. no. is : 89732
You’ll notice that to access the individual elements we have made use of different operators. When you use pointers, you should not use the dot operator. Instead we make use of the arrow operator (->).
p->pin=60004;
Actually, the dot operator (or the member operator) can be used but you have to be careful about operator precedence. To use the member operator we’ll have to dereference the pointer and then use it. The following expression:
*p.pin
would be wrong. The dot operator is a post-fix operator and it has higher precedence over the dereferencing operator (which is a pre-fix operator). So to set it right we will have to use:
(*p).pin
Thus we could also have used the following code in our program:
cout<
cout<<”Salary of that employee is:”<
#include
/* This function can accept varying number of integer arguments.
It will sum the arguments and return the result.
*/
int sum(int a, ...)
{
va_list args;
va_start(args,a);
int result=a;
for(; ;)
{
int temp=va_arg(args,int);
if (temp==0) //can also check for NULL
{
break;
}
else
{
result+=temp;
}
}
va_end(args);
return result;
}
int main( )
{
cout<<"\nThe sum is : "<
The division operator (/) when operating on two integers will produce an integer as the result of division. But if one of the operands are floating point numbers then the result will also be a floating-point number.
In all these cases, the same operator performs different functions depending on the situation. This is called polymorphism.
Inheritance:
Just as the name implies, inheritance refers to children inheriting property from their parents. In C++, the parents are called the parent classes and the children are called the derived (or child) classes. The idea of inheritance is to prevent classes from being redefined over and over again. If a programmer has already created a class and you want to make use of the same class with some additional features, then you needn’t re-write the entire class description again. Instead, you can derive a class from the original one (hence all the existing features of the class will be available in your class also) and you can add the extra features you need to your class. This is called re-usability of code. Instead of re-writing, we can re-use through the concept of inheritance. Let’s take the example of animals: a lion belongs to the cat family; the cat family comes under the mammals’ category and the mammals category will come under the general group called animals. Using inheritance, if a lion is being described then only the unique features of a lion need to be defined (you needn’t define the features of animals, mammals and cats). Thus the class ‘lion’ will be inherited from the class ‘cat’ which will in turn be inherited from the class ‘mammals’ which is inherited from ‘animals’.
OOP Languages:
Object Oriented Programming did not originate in C++. In fact it was already existing and OOP was combined with C programming to develop C++. A few of the OOP languages are:
Simula
Modula
SmallTalk
Ada
C++
Java
Some of these languages are said to be ‘pure OOP’ while others are ‘hybrid OOP’. ‘Pure OOP’ means that everything in a program has to be tied with classes (and you cannot use separate functions). Java is an example of pure OOP. C++ comes under hybrid OOP because you can use OOP as well as the normal C style coding (involving separate functions and data).
A closer look into OOP:
The world can be considered to consist of many objects. Objects will have attributes and behaviours. A water-heater is a simple example of an object. It has certain attributes or properties (like colour, size, maximum and current temperatures etc.) and there are certain behaviours associated with the water-heater (like switching on the heater, increasing the temperature or heating for a specified time interval, switching off the heater etc.). These are actions that can be performed on the heater. Or in other words they are actions which can modify certain properties of the heater (for instance by switching on the heater the current temperature of the heater will change).
A car is another example of an object. It has a lot of attributes such as fuel capacity, current speed, top speed, number of wheels, type of gearbox etc. There are also a lot of operations which you can perform on this object. For example: you can accelerate the car, apply brakes etc. The attributes of a car will have some values at any given instance of time. Once the car is in motion, you can say that at a particular time the speed of the car is 30 km/hr (thus current speed will be 30km/hr). Similarly, the color of the car is red or the car has four wheels. The values for the attributes at any given instant of time define the state of the object. There are two types of states an object can have: static and dynamic. Some attributes of the car will not change over a period of time. The number of wheels in the car is always going to be four (unless you are making a new prototype!). The colour of the car would also remain the same for a long time. These attributes contribute to the static state of the car. The current speed of the car is a dynamic property which will change frequently depending on the actions performed upon the car. In OO terminology you will encounter the following terms frequently:
State
Behaviour
Identity
Behaviour of an object refers to the set of operations (or actions) that can be performed on an object.
Every object will have some attribute that can be used to uniquely identify the object. For example let’s take the example of a car as an object. All cars have colour as an attribute. But can you distinguish two cars based on their colours? Definitely not. But you can distinguish two cars based on their registration number. Hence registration number is the attribute which can be used to uniquely identify a car. If you take a banking example then the account number is a unique way to identify an account (no two accounts can have the same account number).
An object will have two parts:
1. Interface
2. Implementation
In a car, the interface is the acceleration and braking actions which can be performed on the car (there are many more but lets just limit ourselves to these two actions). The driver is going to be the user of the car. When the driver presses the accelerator pedal, there are a lot of things that happen within the car which actually cause the rpm (rotations per minute of the wheel) to increase. Is the driver concerned about what actually happens within the engine? No. The driver just wants the car to accelerate on pressing the pedal and is least bothered about the underlying mechanisms used by the manufacturers to achieve this. He doesn’t care about how the engine is designed or as to how the piston is moving to achieve acceleration. All he knows (and wants to know generally) is that the car should accelerate when he presses the pedal. These internal mechanisms are called implementation details in OOP terminology. One of the central features of OOP is to separate the interface from the implementation. The person using an object should not know/worry about the implementation. This is what is termed encapsulation.
Classes and Objects in C++:
In C++ classes are used implement OOP. A class will contain two types of members: member data and member functions. The member functions can be used to operate on the member data within the class. The data members correspond to the attributes while the member functions correspond to the behaviour. Instead of the term ‘function’, some programmers use the term ‘method’.
The term ‘class’ and ‘object’ might seem confusing at first. Basically you cannot directly use a class (we need to create an instance of the class and we call this an object). In our fundamental data types we have int, double, char etc. But are we using them directly? For example, do we say:
int = 5;
No. If we were to do this then we would never be able to create different integer variables. We create an instance of an integer when we say:
int x = 5;
Since classes are also data types (user defined data types), they also follow the same principle. You have to create instances of a class to do anything useful. An object is an instance of a class, i.e. only when you define an object, will the compiler allocate memory for the object. Class is like a model (or a template) from which you can create many objects of the same type. A template can be compared to the plan of a building. When the plan is drawn, we have not yet allocated the area on land for construction. We only know about how the building structure will be. But when construction work begins, the area will be allocated. Similarly, the compiler allocates memory space for every object that is created. This is why a class is called an abstraction (in other words a class is a generality while an object is a specific instance of the class). Let’s say we have a class called student, with the attributes:
id
name
age
We can create two students by saying:
student Watson, Hastings;
Now, Watson and Hastings are 2 students. Each of them will have an id, name and age (we can modify their attributes separately). You will be able to distinguish between a class and an object clearly when we write a few programs.
Everything in a class (data and functions) is private, protected or public. They are called access-specifiers (because they decide how the class members can be accessed).
private:
As the name suggests, whatever is in the private area of a class can only be accessed from within the class.
If the data is made private then it can be accessed only through member functions of the class.
If a function is made private then it can be called from within another member function.
Data/function is made private by default (i.e. if you don’t mention any specifier).
protected:
The specifier ‘protected’ is used in inheritance and will be dealt with later.
public:
Public members of a class are accessible from outside the class. Hence when objects are created, they can access the public members directly. Usually, data is made private while the functions are made public (this is done to ensure data encapsulation). These public functions can operate on the private data.
The syntax for a class is:
class name-of-class
{
private :
data-type variable-name; //these are private
public :
functions; //these are public
}; // End of class- make a note of the terminator.
It is not a must that data should be private and functions should be public. You can have private functions as well public data.
Remember: No member of your class can be declared as ‘register’ or ‘extern’.
Note: Before getting into classes, you should know that there are two types of programmers who work with classes: the class designer and the class user. The designer (or creator) creates a class and the user makes use of the class in his/her programs.
The term ‘user’ usually represents the person who will use an application developed by a programmer. But if the term ‘user’ is used while explaining classes, then it refers to a ‘class user’ (a class user is another programmer).
In our example codes, we will be the designers as well as the users of the class (of course if you provide the code to someone else, then we will be considered the designers while they will be the users). A class creator will try to hide from the user whatever is not required by the user (why provide more options to the user and lead to complications!). Hide whatever is possible so that the class user cannot tamper with things that they are not supposed to use.
Demonstration of a Class
A program to demonstrate Classes
Before getting into the nuances of classes let’s take a look at a few simple examples to illustrate classes and objects.
Let’s say that we want to create a timer, something like a stopwatch. We should be able to set the timer to a start value, pause it, stop it or start it. Thinking in terms of OOP we would have one member data:
count (let’s keep it as an integer)
Being the first example, we’ll implement some simple functions:
initialize( )
display( )
increment( )
The function names are self-explanatory and the functions would operate on the member data (i.e. someone who uses our timer shouldn’t be able to change the value of count directly. If this were allowed then the user might set count to a negative value or might misuse it). The user of our timer object should be able to access our timer in a controlled manner.
#include
class Timer
{
private:
int count;
public:
void initialize( )
{
cout<<"timer!"; count=0; } void display( ) { cout<<"remaining:"<
counter c2(y);
return 0;
}
The statement:
counter c1(2);
means that the constructor with one parameter is invoked. The argument passed to the parameter is 2. This value is now assigned to the ‘count’ of object ‘c1’.
count (x)
is equal to saying
count = x.
Hence ‘count’ of object c1 is two. You could also write the constructor as:
counter (int x)
{
count=x;
}
Similarly,
counter c2 (y);
means that ‘count’ of c2 will be initialized to the value of y (i.e. the value the user types in). You could also perform some validations within the constructor to ensure that the user doesn’t initialize the member data to an invalid value.
Remember: Constructors cannot return values.
Overloaded Constructors:
Constructors being similar to functions can also be overloaded. To overload a constructor the parameter type or the number of parameters in the constructors should be different. It is possible that in our program we might want to have one constructor with no arguments and one parameterized constructor to initialize the member data to some other values. This is illustrated below:
class rect
{
private:
int length, breadth;
public:
rect( ) //constructor with no parameter
{
length=breadth=0;
}
rect(int x, int y) //constructor with parameters
{
length=x;
breadth=y;
}
};
int main( )
{
rect a; //executes ‘no parameter constructor’
rect b(1,2); //invokes the parameterized constructor
return 0;
}
The above program will create an object ‘a’ whose length and breadth will be initialized to 0. The object ‘b’ will have its length and breadth initialized to 1 and 2 respectively. Another way to write the above program would be as follows:
class rect
{
private:
int length, breadth;
public:
rect(int x=0, int y=0) //if no argument is passed, x=0 and y=0.
{
length=x;
breadth=y;
}
};
int main( )
{
rect a;
rect b(1,2);
return 0;
}
The result will be the same as earlier. In this program we do not explicitly specify the default constructor. We make use of a single constructor to deal with both situations. If the program creates an object without any arguments then the constructor function will be executed with the default parameter values (specified to be 0 in this case). If an argument is passed to the constructor, then the parameter will be assigned the values of the argument.
Default Constructors and Arrays of Objects:
Just like creating an array of integer data type, we can also create an array of objects belonging to a particular class. When you create an array of objects the class should have a no argument constructor (in other words the class should have a default constructor).
Consider the following program:
# include
class rect
{
private:
int length, breadth;
public:
rect(int x, int y)
{
length=x;
breadth=y;
}
};
int main( )
{
rect ob[3]; //ERROR
return 0;
}
In the above program we have a parameterized constructor but no default constructor. Thus the statement:
rect ob[3];
will cause a compile time error. The reason why this does not work is because when you create an array of objects there is no way in which you can pass arguments to the parameterized constructor. The compiler will not know what to pass for each of the objects. Thus if you want to create an array of objects then the class should have a default constructor.
The program can be corrected as shown below:
class rect
{
private:
int length, breadth;
};
int main( )
{
rect ob[3]; //can be compiled without error
return 0;
}
In the above program there is a default constructor provided by the compiler itself. But usually it is better to provide our own default constructor (so that we can initialize the data) as below:
class rect
{
private:
int length, breadth;
public:
rect( ) //default constructor
{
length=0;
breadth=0;
}
};
int main( )
{
rect ob[3]; //can be compiled
return 0;
}
Let’s recall the main points about default constructors:
If no constructor is provided, the compiler provides a default constructor (but this constructor does nothing).
If any constructor (even if only a parameterized constructor) is provided, the compiler will not provide a default constructor.
A constructor with no parameters is called a default constructor.
A constructor with default arguments is also a default constructor. For example: rect(int x=0, int y=0) //if no argument is passed, x=0 and y=0.
{
length=x;
breadth=y;
}
This is a default constructor.
Classes continued
The following topics are covered in this section:
• Scope Resolution Operator
• Destructor
• Dynamically creating objects
• Objects in Functions (returning and passing them)
• Initializing an object using an existing object
Scope Resolution Operator (::)
In the earlier section on classes, we have defined the member functions within the class itself. Hence there was no need to declare the function. But is it possible to define the member function of a class outside the class?
We have already discussed inline functions. Even in classes you can explicitly declare functions to be inline using the ‘inline’ keyword.
Remember: When you declare and define a function within the class it is made an inline function. If you define a function outside a class (using scope resolution operator), it will be treated like a normal function (not inline).
Usually when using classes, any function that is just one or two lines long will be defined within the class itself. Longer functions are defined outside the class.
Member functions can be defined outside the class by using the scope resolution operator. Using this we can declare a function within the class and define it outside the class.
Let’s take a look at the syntax:
return-data-type name-of-class-to-which-it-belongs :: function-name (parameters)
Consider the example below:
class Timer
{
private:
int count;
public:
void display( ); //declared inside the class
void increment( );
Timer( );
};
//Function defined outside the class using scope resolution operator
void Timer::display( )
{
cout<<"remaining:"<
class car
{
private:
int speed;
char color[20];
public:
car( ) //Constructor
{
cout<<"\nCreating a Car"; } ~car( ) //Destructor { cout<<"\nDestroying the Car"; } }; //End of class declaration int main( ) { car mine; return 0; } The output would be: Creating a Car Destroying the Car Dynamically creating objects: In the chapter on pointers we discussed how we can allocate memory dynamically. This concept can be extended to allocate memory for user-defined data types also (like objects). C programmers would be familiar with the dynamic allocation operators ‘malloc’ and ‘free’ (the C++ equivalent are ‘new’ and ‘delete’). One of the most significant features of ‘new’ and ‘delete’ is that these operators are aware of constructors and destructors. Let’s try out a simple example: class car { private: int speed; char color[20]; public: car( ) //Constructor { cout<<"a Car"; } ~car( ) //Destructor { cout<<"the Car"; } }; int main( ) { car *mycar=new car; delete mycar; return 0; } The output will be: Creating a Car Destroying the Car As you can deduce from the output, creating an object using ‘new’ ensures that the constructor for that object is called. Similarly delete calls the destructor of the object. This won’t happen if we were to use ‘malloc’ and ‘free’. Objects in Functions: Objects can be used in functions just like other data types are used. Objects can be passed as argument to a function and an object can be returned from the function. These two topics will be dealt below separately. Passing Objects to Functions: An object can be passed to a function as shown in the program below: #include
class rect
{
private:
int length, breadth;
public:
rect(int x, int y)
{
cout<
class rect
{
private:
int length, breadth;
public:
rect(int x, int y);
void area( )
{
cout<
class batsman
{
private:
int player_number;
int best_score,worst_score;
void update_best(int);
void update_worst(int);
public:
batsman(int n, int b, int w) //constructor
{
player_number=n;
best_score=b;
worst_score=w;
}
void update(int);
void display( );
};
void batsman::update(int x)
{
update_best(x); //private function is called
update_worst(x);
cout<<"\n\nThe scores have been updated\n"; } void batsman::display( ) { cout<<"\nHighest score : "<
{
best_score=y;
}
}
void batsman::update_worst(int z)
{
if (z
class car
{
private:
int speed;
char color[20];
public:
void input( )
{
cout<<"\nEnter the color : "; cin>>color;
cout<<"\nEnter the speed : "; cin>>speed;
}
friend void display(car); //Friend of the class 'car'
};
void display(car x)
{
cout<<"\nThe color of the car is : "<
class virus; // forward declaration of ‘virus’ class
class bacteria
{
private:
int life;
public:
bacteria( )
{
life=1;
}
friend void check(bacteria, virus);
};
class virus
{
private:
int life;
public:
virus( ):life(0)
{}
friend void check(bacteria, virus);
};
void check (bacteria b,virus v)
{
if (b.life= =1 || v.life= =1)
{
cout<<"\nSomething is alive"; } if (b.life = = 1) { cout<<"\nA bacteria is alive."; } if (v.life = = 1) { cout<<"\nA virus is alive."; } } int main( ) { bacteria fever; virus cholera; check(fever, cholera); return 0; } In the second line of the program we have the statement: class virus; This is a forward declaration of the class ‘virus’. This is done because when the compiler reads through the lines in the ‘bacteria’ class, it encounters the word ‘virus’ in the friend function declaration. Until this point it doesn't know what ‘virus’ is because we will be defining the ‘virus’ class later on. So we tell the compiler in advance that ‘virus’ is a class by declaring it in the starting itself. If you don't declare it, you will get errors. Just try it out. One more note: You should declare the friend function in both the classes where you want it to be a friend. In this program we want to check whether any organism is alive at present by testing the value of ‘life’. Of course you can write individual member functions in each class but the use of a friend function makes it simpler and easier to understand the logic. Friend Classes Just like functions are made friends of classes, we can also make one class to be a friend of another class. In this way, the friend class will have access to all the private members of the other class. #include
class add
{
private:
int x,y;
public:
add( )
{
x=y=4;|
}
friend class support; //support is now a friend of add
};
class support
{
public:
void sum(add ob) //it can access private members of class 'add’
{
cout<<"The sum of the 2 members is : "<<(ob.x+ob.y); } }; int main( ) { add ad; support sup; sup.sum(ad); return 0; } The output will be: The sum of the 2 members is : 8 In this program, the class ‘support’ is a friend of the class ‘add’. Thus the class ‘support’ can access all the private members of the class ‘add’ (i.e. ‘x’ and ‘y’). Friend classes are rarely used. Static Class Members A class is just an empty area. No area is allocated when you create a class. So, only when you create an object of that class will the compiler allocate space to the object. Again, this means that: private: int x; does not allocate space for an integer x. When you create objects belonging to this class, required space is allocated for holding the integer. Each new object that you create belonging to this class will have its own version of the integer ‘x’. The keyword ‘static’ helps you to create a single copy for all objects belonging to the same class. #include
class bacteria
{
private:
static int count;
public:
void modify( )
{
count=count+1;
cout<
class bacteria
{
private:
int life,count;
public:
bacteria( )
{
life=1;
}
void display( )
{
cout<
return 0;
}
The output is:
Life is : 1
You cannot attempt to access any private members of a class using a pointer to an object (data hiding is retained irrespective of whether you create an object or a pointer to an object).
How are objects stored in memory?
We know that classes are just a framework- they do not occupy memory space. Instances of the class (objects) will occupy memory space. But a class has member data as well as member functions. It is obvious that every instance of the class should have its own copy of the member data; but is it necessary that every object should have a copy of all the member functions? The member data values will vary from object to object but the member functions are going to be the same.
Member functions are implemented by taking advantage of this fact and hence only a single copy of the member functions is stored in memory. For example:
#include
class heater
{
private:
int temp;
public:
heater( )
{
temp=0;
}
void set_temp(int x)
{
temp=x;
cout<<"temperature is now:"<
cout<<"\nEnter the speed : "; cin>>speed;
}
This is a member function belonging to the class called ‘car’. ‘color’ and ‘speed’ are member data of that class. In reality, the above function is equivalent to:
void input( )
{
cout<<"\nEnter the color : "; cin>>this->color;
cout<<"\nEnter the speed : "; cin>>this->speed;
}
In fact the compiler will convert our code into something similar (since it has to use the ‘this’ pointer to access the member data of the correct object). The ‘this’ pointer is useful when a member function of a class has to pass the address of the current object to another part of the program (or another function).
Beware: The ‘this’ pointer is not present in static member functions. It is also not present in friend functions (because friend functions are not members of a class).
Copy Constructors
It’s time to go back to the problem we discussed earlier (the mystery of the default copy constructor). A copy constructor is invoked when you initialize a new object of a class using an existing object. This will happen when:
• You pass a copy of an object as argument to a function (i.e. when passing by value).
• When you return an object from a function
• Or initialize an object during declaration using another object.
If we don’t specify a copy constructor, the compiler already has a default copy constructor. This default copy constructor will simply perform a bit-by-bit copy of the original object to the new object (i.e. it will blindly copy everything to the new object). This can lead to problems (especially if you use the ‘new’ and ‘delete’ operators in the constructor and destructor of the class). An example was discussed earlier in this chapter (using the ‘rect’ class).
We’ll start by considering the same example:
class rect
{
private:
int length, breadth;
public:
rect( )
{
cout<
class virus;
class bacteria
{
private:
int *life;
public:
bacteria( )
{
life=new int;
cout<<"\nCreated Bacteria"; *life=1; } ~bacteria( ) { cout<<"\ndestroyed bacteria."; delete life; } friend void check(bacteria, virus); }; class virus { private: int *life; public: virus( ) { life=new int; cout<<"\nCreated virus"; *life=0; } friend void check(bacteria, virus); ~virus( ) { delete life; cout<<"\ndestroyed virus"; } }; void check (bacteria b, virus v) { if ( (b.life[0]==1) || (v.life[0]==1) ) { cout<<"\nSomething is alive"; } } int main( ) { bacteria fever; virus cholera; check(fever,cholera); return 0; } The output will be: Created Bacteria Created virus Something is alive destroyed bact. destroyed virus And then an error is generated when using VC++ (because the same memory area is being deleted twice)…. In Visual C++ compiler it generates an error during run time and other compilers will also produce something similar (or in the worst case your program could freeze). Anyway, this can lead to drastic effects so you have to program in such a way that this problem does not occur. We’ll add our own copy constructors to both the classes. Add the following to the bacteria class: bacteria(const bacteria &ba) { cout<
class circle
{
private:
int radius;
public:
circle( )
{
cout<<"No argument constructor invoked"; radius=0; } circle(int x) { cout<<"Single argument constructor invoked"; cout<<"is:"<
class counter
{
private:
const int count;
public:
counter( )
{
count=0;
}
};
int main( )
{
return 0;
}
Compiling this code you would get at least a couple of errors:
'count' : must be initialized in constructor base/member initializer list
l-value specifies const object
The second error is produced because of the line:
count=0; //A const object cannot be assigned a value.
Or you might get the error: assignment of read-only member `counter::count'
This program clearly indicates that the constructor we’ve used is simply performing an assignment operation rather than initialization of member data. Let’s modify the code:
#include
class counter
{
private:
const int count;
public:
counter( ):count(0)
{}
void display( )
{
cout<<"count is:"<
};
int main( )
{
counter c1;
c1.display( );
return 0;
}
This code will compile and execute correctly.
Thus a constructor is of the general form:
class-name(arguments) : member(value), member(value)…
{
//body of constructor for any assignments
}
To summarize:
• All const and reference member data types have to be initialized using an initializer list.
• Anything within the body of a constructor is only an assignment and not an initialization.
• If one class contains another class (i.e. one object contains an object of another type), then you’ll have to initialize the other object in the initializer list.
Beware: When using an initialization list the order in which the members are initialized depends on the order in which you declare them within the class (does not depend on the order specified in the initializer list). The first member in the class will be the first member to be initialized. If your initializations depends on the sequence then be careful while using initializer lists.
Declaration and Definition revisited:
Now we are in a better position to understand these 2 terms (which were introduced in the first chapter itself).
Declaration:
We tell the compiler that “this is the name of an object of this type which I might use later in my program.” Example:
class virus; //class declaration
We tell the compiler beforehand that ‘virus’ is a class.
int getHealth ( ); //function declaration
extern int error_no; //declaration of error_no
The last example is used when you are writing code in multiple files (where one part of the code uses variables defined in another file). This will be dealt with later.
But the point to note is that in the above 3 cases, we are only declaring something to the compiler (a function, a class and an object). The compiler does not allocate any memory space for them.
Definition:
Let’s start of with the examples:
//Defining a class
class virus
{
private:
int life;
//rest of the class
};
//defining a function
int getHealth ( )
{
return health;
}
In a definition we provide the compiler with details. What about the next case?
//defining and declaring
int error_no;
This is what we’ve been using frequently so far. This statement defines error_no (the compiler allocates storage space for it) and also declares error_no as type integer.
Recap:The main concepts of OOP are: data abstraction, data encapsulation, inheritance and polymorphism.
• Classes form the basis for OOP. Using classes, data and the functions that can operate on the data can be bundled together.
• An object is an instance of a class.
• The 3 access specfiers used in classes are private, protected and public. These specifiers determine as to who can access the class members.
• The constructor is a special function invoked when an object is created and the destructor is invoked when the objected is deleted (or destroyed).
• Every class is provided with a default constructor by the compiler if the programmer doesn’t define any constructor.
• A constructor without parameters is a default constructor.
• Constructors can be overloaded using different parameters.
• When creating an array of objects, the class should have a default constructor.
• When an object is passed to a function or when an object is returned from a function, a temporary object is created (the temporary object will only invoke the destructor but not the constructor).
• Friend functions are not members of a class but they can access the private members of the class.
• Objects can be made constant but they can only access constant functions and they cannot alter the value of member data (unless the member is declared to be ‘mutable’).
• Static members can be accessed even without using an object.
• Copy constructors should be defined in a class in case the dynamic memory allocation operators are used in the constructor and destructor.
• Copy constructors are not invoked in assignments.
• The ‘explicit’ keyword is used with constructors to prevent implicit conversions.
• Initializer lists are used to initialize the member data of an object.
It is clear from the above graph that there will be 100*100 (=10,000) possible coordinates (in other words 10,000 possible combined values for x and y). The simplest way to find the minimum value for a function involving x and y would be to substitute all the 10,000 combinations, find the cost for each combination and then choose the minimum value. This seems to be a straightforward and simple method, but you can imagine the computational time needed when the solution space increases in size. And if you include floating point values, you are going to end up with a major problem. This is why we need techniques for optimization. We need to use algorithms that can determine the best point in a given solution space as quickly as possible (i.e. the algorithm should not calculate values for each and every combination).
This is where bacterial movement comes into the picture. You might have heard of the E.Coli bacteria. This bacterium lives in the human body and has a particular method of movement. It keeps searching for areas where there is more food (or nutrition). Assume that the bacteria is initially at some place in your intestine. It will take a small step in one particular direction. If the new place has more nutrition than the previous position, then it will continue to move in the same direction. Otherwise, it will take a different direction and proceed. Suppose the new position has more nutrition, it will take another step in the same direction. Again if the next position is favourable, it will proceed in the same direction. This movement is a very simplified version of the actual movement (there are more complicated steps involved in bacterial movement but we will not deal with them here). Now you can apply the same bacterial movement algorithm to the problem of optimization. The advantage is that you won’t be computing values for each and every point in the solution space (i.e. we will program in such a way that after a particular number of iterations the program will terminate). There are many other organisms in nature which have different ways of movement and this could also be applied to the problem of optimization (in fact algorithms relating to ants, bees and snakes have been developed already).
So, where do we make use of classes and objects?
Bacteria is a general category and it forms the class. All types of bacteria will have some stepsize (stepsize refers to the length of the stride taken by the bacteria in moving once) and some particular direction as well (bacteria are in three-dimensional space and they are free to take any direction). Thus, these features are common to all bacteria. We can then model bacteria as a class. This class could contain the following functions:
• To initialize the starting position of the bacteria. Every bacterium when it is created will have a particular position in 3 dimension. This has to be specified by three variables: x,y and z.
• To generate the direction in which the bacteria should move (in mathematics this is called the unit vector).
• To find the new position after the bacteria moves.
• An algorithm to decide whether to move to the new position or not (move only if new place is better than the present one).
Every instance created from the class ‘bacteria’ is an object. Each object will have a different starting position (decided randomly) and each bacteria will move in different directions (i.e. each one will try to find the best point).
Thus, we can create 10 objects of type ‘bacteria’ and make each bacteria search the solution space. After some particular number of iterations, we can stop the bacterial movement and find out the positions of each of the objects.