Plus One Computer Application Notes Chapter 6 Introduction to Programming

Kerala Plus One Computer Application Notes Chapter 6 Introduction to Programming

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);
}
int main()
{
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 #inciude we can link the header files that are needed to use the functions. By using #define we can define some constants.
Eg. #define × 100. Here the value of x becomes 100 and cannot be changed in the program.
No semicolon is needed.

Header files:
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
iostream
iomanip
cstdio
cctype
cmath
cstring
The syntax for including a header file is as follows #include
Eg. #include

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.
Use comments wherever needed.
To give tips in between the program comments are used. A comment is not considered as part of the program and can not 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: int age=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 the same size. The important modifiers are signed, unsigned, long and Short.
Plus One Computer Application Notes Chapter 6 Introduction to Programming 1
Shorthands in C++

Arithmetic assignment operators: It is faster. This is used with all the arithmetic operators as follows.
Plus One Computer Application Notes Chapter 6 Introduction to Programming 2
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=10then 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)
Plus One Computer Application Notes Chapter 6 Introduction to Programming 3

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.

Plus One Computer Application Notes