A Quick guide for C++ #2

 Review of C++ language 


Structure  of C++ program

and input / output  functions 



Structure  of  C++ program 

Structure

Comments  or documentation  section
Linker section
Definition section
Global  declaration section
main()
{
Declaration  section

Executable statements  section

}
sub routine section
{
function1()
function2()

.................
function_n()
}

Example program 

//Program to find the area and circumference  of a circle//

#include<stdio.h>
#include<conio.h>
#define PI 3.1415

void main()
{
int radius;
float cir, area;
cout<<"Input the value of radius:";
cin>>radius;
area =PI*radius*radius;
cir=2*PI*radius;
cout<<"n"<<"the area of circle is:"<<area<<"n";
cout<<"n"<<"the circumference of circle is:"<<area<<"n";
}

Explanation:

Comments or Documentation section

○This is generally a comment.
○This section allows us to wiite comments  about what each statement is doing. Indeed, the comments can be added at the end of each statement in the program.
○They are not executed by the compiler. But, they definitely provide the required explanations about the program to the programmers.
○Single line comments begin with double slash comments are enclosed bet-ween slash and asterisk ( /* ..... ..*/ ). ulthe

Linker Section

#include<iostream.h>
#include<conio.h>
○The linker section begins with a hash (#) symbol and #include. . is a preprocessor directive and these statements are processed first by the compiler and then the rest of the statements are compiled.
○These statements tells the compiler to include the header files iostream.h and conio.h into the program.


Definition Section 

#define P1 3.14
○In this section, we can define constants, expressions, structures, functions, classes and objects.
○After the linker section gets executed, the definition section is executed by the compiler and this section is optional.
○In the above example,
we find that PI is a Constant identifier which takes the value 3.14 and wherever this identifier is used the compiler replaces the value with 3.14 in the program.

Global declaration section

□We can declare variables here and those variables which are declared before the main function or any other function then the life or scope of such variables remain throughout function and can be used by other functions of the program.
□This section is also optional and depends on the needs of the application that is being developed. Even in the above example we have no variables declared under this section.

main() function

void main()
□ This instruction marks the beginning of the main function and it contains the statements that perform the required task.
□ This statement is executed first by the compiler when the program starts. It is also important to note that there will be only one main()  function in a program.


Braces {}

{       Opening braces

}       Closing braces

The statements inside any function including the main ( ) function is enclosed with the opening and closing braces.

Variable declaration section

In the above example program, we have declared variables and also its data types. This helps the compiler to allocate the memory space inside the computer memory. Those variables declared inside the function block, the life or scope of such variables remains till the end of that block only.

Executable statements section

○Generally, in C++the statements are written inside the main( ) function or any other function.
○These statements can be expressions, input-output functions, conditional statements, looping statements, function call and so on.
○we have seen the example to compute the area and circumference of the circle.
○The cout function statement is a user prompt which tells the user to input the value for the radius and the text present inside the double quotes " input the value of radius: " is displayed on the screen.

○The next is the cin function statement which takes the input that is given by the user , next two statements are C++ expressions that calculate the area and circumference of the circle respectively.

○Finally, the next two output statements display the computed area and circumference respectively.

Importance of' iostream.h

○As discussed earlier, we have included some other files to our source programs using the include<______ .h>'directive in the linker section of the program.
○The preprocessor is an instruction to the compiler itself so that it deals with these directives before starting with the real compilation process for example the first statement in the structure


#indude directive

○The #include directive instructs the compiler to read and include another file in the current file.
○The compiler compiles the entire code.
○A header file may be included in one of two ways.
#include<iostream.h>
 or
 #include "iostream.h"

The header file in angle brackets means that file reside in standard include directory. The header files in double quotes means that file reside in current directory.

Points  to be noted 

◇Include files
◇Class declarations
◇Member function declaration
◇main() function

Include files:

This section is used to include all the preprocessor directives that are necessary for the program being written.

Class declaration:

A class is a blueprint for the objects. It describes the data and functions that the object is going to use.

Member function declarations:

This section defines or declares the user-defined functions that other functions or the main() function may use.

main() function: 

○This is also a function that integrates all the other functions of the program. It contains the body of the function. The body should be enclosed within curled braces {and).
○The body contains two parts: Local declarations and executable statements
○The local declaration refer to the declaration of all those variables that are used within the main() function.
○The executable statements are the statements that perform the required operations to solve the problem.

Libaray functions


C++ provides many built-in functions that save the programming time.
They include mathematical functions, character functions, string functions, console input-output functions and some general standard library functions. Some of the are given below

◇ Character Functions


All the character functions require ctype.h header file. The following table lists the function.

Some functions of character manipulation are given
○isdigit()
○isupper()
○isalpha()
○toupper()
○toascii()
○islower()
○isspace()
○tolower)
○toupper()
○ispunct()

◇ Sting Funcions


The string functions are present in the string.h header file. Some string functions are given below:

some of the functions are:
○strlen()     strlwr()    strcmp() 
○strcat()     strcpy()    strlwr()
○strrev()     strupr()    strcmpi()

Console I/O functions

The following are the list of functions is in stdio.h are:

getchar()   putchar()   gets()   puts()

Variables

variable is an identifier whose value is allowed to change during the execution of the program. A variable actually represent the name of the memory location.

Declaration of a variable


The syntax for declaring a variable is

datatype variable_name;

Example: int n;

Initializing a variable

The syntax to initialize a variable is:

datatype variable_name = value;

Example:
Let b be a variable declared of the type int. Then,
int b = 100;

C++ compiler allows us to declare a variable at run time. This is dynamic initialization. They can be initialized anywhere in the program before they are used
Example
int a, b;
int temp = a;
a = b;
b = a;

Data types

C++ support the following data types.

Data types classification
There are two types of data types.
○Simple or fundamental data types
○Complex or Derived data types

□The simple or fundamental data types are the primary data are not composed of any other data types.
□The simple data types/fundamental data types include int,char, float,double and void.

Modifiers

A modifier is used  to alter the meaning  of the base type so that it more  precisely  fits  the need of various situations

The data type modifiers are listed here: signed, unsigned, long and short

Example: unsigned   int b;

Derived data types

These data types are constructed using simple or fundamental data types.

They include arrays, functions, pointers and references.

User defined data types

These data types are also constructed using simple or fundamental data types.

Some user defined data types include structure, union, class and enumerated


Input and output operators

Input and output operators are used to perform input and output operations

Input Operator ">>"

KEYBOARD ----> cin ------> >>  ------> variable 

○ The standard input device is usually the keyboard. Input in C++ is done by using stream extraction operator (>>) on the cin stream.
○ The operator must be followed by the variable that will store the data that is going to be extracted from the stream.
Example:
int age;
cin>>age;

Output Operator "<<"

The standard output device is the screen (monitor) and outputting in C++ is done by using the object followed by the stream insertion operator which is written as <<. cout stands for Console output.

MONITOR ------> cout-----> << --------> Variable

Example:
cout<<"Let us learn C++";
// prints Let us learn C++
//on the screen.

Cascading of I/O operators:

○ C++ supports the use of stream extraction (<<) and stream insertion >> operators many
times in a single input (cin) and output (cout) statements.
○If a program requires more than one input variable then it is possible to input these variables in a single cin statement using multiple stream extraction operators. ○Similarly, when we want to output more than one result then this can be done
using a single cout statement with multiple stream insertion operators. This is called as cascading of input output operators.

Example:
 cout<<" enter the value for x
 cin>> x;
 cout<<" enter the value for y";
 cin>>y;

Instead of using cin statement twice, we can use a single cin statement and input the values for the two variables x and y using multiple stream extraction operator as shown below.

cout<<" enter the value for x and y";
cin>>x>>y;

Similarly, we can even output multiple results in a single cout statement using cascading of stream insertion operator as shown below.

cout<<" the sum of" <<x<<" and "<<y<<"="<<x+y;

Comments

Post a Comment

Popular Posts

Translate