# Plus Two Computer Application Notes Chapter 1 Review of C++ Programming

Kerala State Board New Syllabus Plus Two Computer Application Notes Chapter 1 Review of C++ Programming.

## Kerala Plus Two Computer Application Notes Chapter 1 Review of C++ Programming

It is developed by Bjame Stroustrup. It is an extension of C Language.

Character set: To study a language first we have to familiarize the character set. For example to study English language first we have to study the alphabets. Similarly here the character set includes letters (A to Z & a to z), digits (0 to 9), special characters(+, -, ?, *, /, …….) white spaces(non printable) etc….

Token: It is the smallest individual units similarto a word in English or Malayalam language. C++ has 5 tokens
1. Keywords: These are reserved words for the compiler. We can’t use for any other purposes.
Eg: float is used to declare variable to store numbers with decimal point. We can’t use this for any other purpose

2. Identifier: These are user defined words.
Eg: variable name, function name, class name, object name etc…

3. Literals (Constants): Its value does not change during execution
i. Integer literals : Whole numbers without fractional parts are known as integer literals, its value does not change during execution. There are 3 types decimal, octal and hexadecimal.
Eg. For decimal 100, 150, etc
For octal 0100, 0240, etc

ii. Float literals: A number with fractional parts and its value does not change during execution is called floating point literals.
Eg. 3.14157, 79.78, etc..

iii. Character literal: A valid C++ character enclosed in single quotes, its value does not change during execution.
Eg. ‘m’, ‘f’, etc…

iv. String literal: One or more characters enclosed in double quotes is called string constant. A string is automatically appended by a null character (‘\0’)
Eg. “Mary’s”, “India”, etc

4. Punctuators: In English or Malayalam language punctuation marks are used to increase the readability but here it is used to separate the tokens. Eg: {,}, (,),

5. Operators: These are symbols used to perform an operation (Arithmetic, relational, logical, etc…)

Integrated Development Environment(IDE): It is used for developing programs

• It helps to write as well as editing the program.
• It helps to compile the program and linking it to other (header files and another user) programs
• It helps to run the program

Turbo C++ IDE
Following is a C++ IDE

a) Opening the edit window
Method I: File → Click the menu item
New Method II: Press Alt and F simultaneously then press N
b) Saving the program:
Click File → Save or Press Function key F2 or Alt + F and then press S
Then give a file name and press ok.
c) Running/executing the program
Press Alt + R then press R OR Click Run → press R, OR Press Ctrl + F9
d) Viewing the output: Press Alt + F5
e) Closing Turbo C++ IDE
Click File → then press Quit menu Or Press Alt + X

Geany IDE

Step 1: Take Geany Editor and type the program(source code)
Step 2: Save the file with the extension.cpp
Step 3: Compile the program by Click the Compile Option
Step 4: After successful compilation, Click the Build option
Step 5: Then click on the Execute option

Important Notes while using Geany Editor

1. For the header files .h extension need not given
2. Header files are given below

3. Add a new statement using namespace std; after the preprocessor
4. Instead of void main() use int main();

Let us see the following to distinguish the editor’s Turbo C++ and Geany

1. Write a C++ program to read a number and print.

Using Turbo C++ editor

#include
#include
int main()
{
clrscr();
int a;
cout<<“Enter a number”; cin>>a;
cout<<“The number you entered is ”<<a;
getch();
}

To compile and press Ctrl+F9.

Using Geany editor

#include
using namespace std;
int main()
{
int a;
cout<<“Enter a number”; cin>>a;
cout<<“The number you entered is ”<<a;
}

Type the above and save it as a file name with .cpp.
Compile, Build and then Execute

Concepts of data types: The nature of data is different, data type specifies the nature of data we have to store.

C++ data types

Fundamental data types: It is also called a built-in data type. They are int, char, float, double and void
i) int data type: It is used to store whole numbers without fractional (decimal point) part. It can be either negative or positive. It consumes 2 bytes(16 bits) of memory.i.e. 216 = 65536 numbers. That is 32768 negative numbers and 32768 positive numbers(0 is considered as +ve ) So a total of 65536 (32768+32768) numbers. We can store a number in between -32768 to + 32767.

ii) char data type: Any symbol from the key ‘ board, eg. ‘A’, ‘?’, ‘9’,…. It consumes one byte (8 bits) of memory. It is internally treated as integers, i.e. 28 = 256 characters. Each character is having an ASCII code, ‘a’ is having ASCI I code 97, and zero is having ASCII code 48.

iii) float data type: It is used to store real numbers i.e. the numbers with a decimal points. It uses 4 bytes(32 bits) of memory.
Eg. 67.89, 89.9 E-15.

iv) double data type: It is used to store very large real numbers. It uses 8 bytes(64 bits) of memory.

(v) void data type: void means nothing. It is used to represent a function that returns nothing.

User-defined Data types: C++ allows programmers to define their own data type. They are Structure(struct), enumeration(enum), union, class, etc.

Derived data types: The data types derived from fundamental data types are called Derived data types. They are Arrays, pointers, functions, etc

Variables:
The named memory locations are called variables. A variable has three important things

1. variable name: A variable should have a name
2. Memory address: Each and every byte of memory has an address. It is also called the location (L) value.
3. Content: The value stored in a variable is called content. It is also called Read(R) value.

Operators: An operator is a symbol that performs an operation. The data on which operations are carried out are called operands. Following are the operators
1) Input (>>) and output (<<) operators are used to performing input and output operations. Eg. cin >> n;
cout << n;

2) Arithmetic operators: It is a binary operator. It is used to perform addition(+), subtraction(-), division (/), multiplication(*) and modulus(%-gives the remainder) operations.
Eg. If x = 10 and y = 3 then

x/y = 3, because both operands are integer. To get the floating-point result one of the operands must be float.

3) Relational operator: It is also a binary operator. It is used to perform comparison or relational operation between two values and it gives either true (1) or false(0). The operators are <, <=, >, >=, == (equality) and != (not equal to)

4) Logical operators: Here AND(&&), OR(||) are binary operators, and NOT(!) is a unary operator.
It is used to combine relational operations and it gives either true (1) or false (0).
If x = 1 and y = 0 then

Both operands must be true to get a true value in the case of AND (&&) operation.
If x = 1 and y = 0 then

Either one of the operands must be true to get a true value in the case of OR(||) operation
If x = 1 and y = 0 then

5) Conditional operator: It is a ternary operator hence it needs three operands. The operator is ?:.
Syntax: expression ? value if true : value if false.
First evaluates the expression if it is true the second part will be executed otherwise the third part will be executed.
Eg. If x = 10 and y = 3 then
x > y ? cout<<x; cout<<y
Here the output is 10

6) sizeof(): This operator is used to find the size used by each data type.
Eg. sizeof(int) gives 2.

7) Increment and decrement operator: These are unary operators.
a) Increment operator (++): It is used to increment the value of a variable by one i.e., x++ is equivalent to x = x + 1;
b) Decrement operator (–): It is used to decrement the value of a variable by one i.e., x– is equivalent to x = x – 1.

8) Assignment operator (=): It is used to assign the value of the right side to the left side variable.
eg. x = 5; Here the value 5 is assigned to the variable x.

Expressions: It is composed of operators and operands

Arithmetic expression: It is composed of arithmetic operators and operands. In this expression the operands are integers then it is called the Integer expression. If the operands are real numbers then it is called Floating point expression. If the operands are constants then it is called constant expression.

Relational expression: It is composed of relational operators and operands.

Logical expression: It is composed of logical operators and operands

Statements: Statements are the smallest executable unit of a programming language. Each and every statement must end with a semicolon(;).

Declaration statement: Each and every variable must be declared before using it.
Eg: int age;

Assignment statements: The assignment operator is used to assign the value of RHS to LHS.
Eg: x = 100;

Input statements:
input(>>) operator is used to perform input operation. Eg. cin>>n;

Output statements
output(<<) operator is used to perform output operation.
Eg. cout<<n;

The multiple uses of input or output operators in a single statement is called Cascading of i/o operators.
Eg: To take three numbers by using one statement is as follows
cin>>x>>y>>z;
To print three numbers by using one statement is as follows
cout<<x<<y<<z;

Structure of a C++ program:
A typical C++ program would contain four sections as shown below.
Include files(Preprocessor directives)
Function declarations
Function definitions
Main function programs
Eg:

# include
using namespace std;
int sum(int x, int y)
{return (x+y);}
intmain()
{
cout<<sum(2, 3);
}

Preprocessor directives: A C++ program starts with the preprocessor directive i.e., # include, #define, #undef, etc, are such a preprocessor directive. By using #include we can link the header files that are needed to use the functions. By using #define we can define some constants.
Eg. #define x 100. Here the value of x becomes 100 and cannot be changed in the program.
No semicolon is needed.

header files: A header file is a pre-stored file that helps to use some operators and functions. To write C++ programs the header files are a must. Following are the header files used in Geany editor.
iostream
cstdio
cctype
cmath
cstring
cstdlib
The syntax for including a header file is as follows
Eg. #include<iostream>

The main function: The main function is the first function which is invoked at the time of execution and the program ends within main(). The other functions are invoked from main().

Programming tips:
The identifier name must be clear, precise, brief, and meaningful
Use clear and simple expressions.
To give tips in between the program comments are used. A comment is not considered as the part of the program and cannot be executed. There are 2 types of comments single line and multiline. Single line comment starts with //(2 slashes) but multi-line comment starts with /* and ends with */
indentation: Giving leading spaces to the statements is called indentation. It is a good programming practice.

Variable initialisation: Giving value to a variable at the time of declaration.
Eg: intage=16; Here the OS allocates 4 bytes memory for the variable age and it stores a value 16.

const-The access modifier: By using the keyword const we can create symbolic constants its value does not change during execution.
Eg: const int bp=100;

Type modifiers: With the help of type modifiers we can change the sign and range of data with same size. The important modifiers are signed, unsigned, long and short

Shorthands in C++

Arithmetic assignment operators: It is faster. This is used with all the arithmetic operators as follows.

a) Increment operator(++): It is used for incrementing the content by one.
++x(pre increment) and x++ (post increment) both are equivalent to x = x + 1.

b) decrement operator (–): It is used for decrementing the content by one.
–x (pre decrement) and x– (post decrement) both are equivalent to x = x – 1.

Prefix form: In this, the operator is placed before the operand and the operation is performed first then use the value.

Postfix form: In this, the operator is placed after the operand and the value of the variable is used first then the operation is performed
Eg: Post increment a++
Here first use the value of ‘a’ and then change the value of ‘a’.
Eg: if a=10 then b=a++.
After this statement b=10 and a=11

Pre increment ++a
Here first change the value of a and then use the value of a.
Eg: if a=10 then b=++a.
After this statement b=11 and a=11.

Precedence of operators: Consider a situation where an expression contains all the operators then the operation will be carried in the following order(priority)

Type conversion: Type conversions are of two types.
1) Implicit type conversion: This is performed by the C++ compiler internally. C++ converts all the lower sized data type to the highest sized operand. It is known as type promotion. Data types are arranged lower size to higher size is as follows, unsigned int(2 bytes), int(4 bytes), long (4 bytes), unsigned long (4 bytes), float(4 bytes), double(8 bytes), long double(10 bytes)

2) Explicit type conversion: It is known as typecasting. This is done by the programmer. The syntax is given below.
(data type to be converted) expression
Eg. int x=10;
(float) x;
This expression converts the data type of the variable from integer to float.
These are classified into two decision making and iteration statements

Decision-making statements:

if statement:

Syntax: if (condition)
{
Statement block;
}

First, the condition is evaluated if it is true the statement block will be executed otherwise nothing will be happened.

if… else statement:

Syntax:

if (condition)
{
Statement block1;
}
Else
{
Statement block2;
}

Nested if
An if statement contains another if statement completely then it is called nested if.

if (condition 1)
{
if (condition 2)
{
Statement block;
}
}

The statement block will be executed only if both the conditions evaluated are true.

The else if ladder: The syntax will be given below:

if (expression 1)
{
statement block1;
}
else if (expression 2)
{
statement block 2;
}
else if (expression 3)
{
statement block 3;
}
..............
else
{
statement block n;
}

Here firstly, expression 1 will be evaluated if it is true only the statement block1 will be executed otherwise expression 2 will be evaluated if it is true only the statement block2 will be executed and so on. If all the expression evaluated is false then only statement block n will be executed.

switch statement:
It is a multiple branch statement. Its syntax is given below.

switch(expression)
{
case value: statements;break;
case value: statements;break;
case value: statements;break;
case value: statements;break;
case value: statements;break;
...............................
default: statements;
}

The first expression evaluated and selects the statements with the matched case value. If all values are not matched the default statement will be executed.

Conditional operator: It is a ternary operator hence it needs three operands. The operator is ?:.
Syntax: expression ? value if true : value if false.
First evaluates the expression if it is true the second part will be executed otherwise the third part will be executed.

Iteration statements: If we have to execute a block of statements more than once then iteration statements are used.

while statement: It is an entry controlled loop. An entry controlled loop first checks the condition and executes (or enters into) the body of the loop only if it is true. The syntax is given below:

Loop variable initialised
while(expression)
{
Body of the loop;
Update loop variable;
}

Here the loop variable must be initialised before the while loop. Then the expression is evaluated if it is true then only the body of the loop will be executed and the loop variable must be updated inside the body. The body of the loop will be executed until the expression becomes false.

for statement
The syntax of for loop is

for(initialization; checking; update loop variable)
{
Body of loop;
}

The first part, initialization is executed once, then checking is carried out if it is true the body of the for loop is executed. Then the loop variable is updated and again checking is carried out this process continues until the checking becomes false. It is an entry controlled loop.

do-while statement: It is an exit controlled loop. Exit control loop first executes the body of the loop once even if the condition is false then checks the condition.

do
{
Statements
}while(expression);

Here the body executes at least once even if the condition is false. After executing the body it checks the expression if it false it quits the body otherwise the process will continue.