C ++ PROGRAM

C ++ PROGRAM 

C ++ is a successful and most widely used Object Oriented Programming language.  It is an extension of C language that not only adds Object Oriented concepts but also readdress some of the weaknesses of C language.  The object oriented features in C ++ helps the programmer to build large and complex programs with greater efficiency, extensibility and ease of maintenance.  It allows a programmer to follow a structured and disciplined approach for making complex computer programs.
  C ++ is a strongly typed language developed by Bjarne Stroustrup in 1983 at Bell Laboratories.  C ++ is a hybrid language as it is possible to write programs in C ++ in either C language style, Object oriented style or both.  It can be used for developing any kind of applications like interactive computer graphics, expert systems, simulation, databases, artificial intelligence, system program application and for making compilers.
  C ++ is a universal language.  It can be used to write programs for MS - Windows, Linux, Macintosh, Unix etc.

CHARACTERISTICS OF C ++ LANGUAGE 

C ++ is a popular and versatile language for developing large and complex applications.  The following are the characteristics of C ++: 
1. C ++ has a rich collection of standard class libraries, inbuilt functions and datatypes which helps in creating large and complex programs very easily.  
2. C ++ is an extension of C with Object Oriented (00) features.  It provides upward compatibility with C language as almost all the valid C programs are valid C ++ programs.  
3. The program written in C ++ is well suited for modeling real world problems as close as possible to the users perspective.  
4. C ++ provides very efficient memory management technique.  The various memory management operators available in C ++ helps to save the memory and improves the efficiency of the program.  These operators allocate and deallocate memory at run time.  Some common memory management operators available in C # are new, delete etc.  
5. In C ++, complex data types called Abstract Data Types (ADTS) can be created using classes.  
6. C ++ is a portable language and programs made in it can be run on different machines.  
7. C ++ compiler is easily available and it requires very less space for storage.  It is very easy to load C ++ compiler into your computer.  
8. Format free Input / Output statements in C ++ helps the beginners to understand the Input / Output operations very easily.  For performing Input / output operations in CH, there is no need for format specification as in case of C language.  
9. C ++ is a flexible language.  It is possible to define several functions with the same name that perform different tasks.  For example, the function abs () is used to calculate the absolute value of integer, float and long integer.  Also, it supports declaration of variables anywhere within the program.
10. C ++ is a strongly typed language.  The list of arguments of every function call are typed checked during compilation.  If there is a type mismatch between actual and formal arguments an implicit conversion is applied if possible.  If an implicit type conversion is not possible or if the number of argument (s) is incorrect, a compile time error is occured.  
11. C ++ allows programmer to redefine the meaning of existing operators such as + -> For example: The "+ 'operator can be used for adding two numbers as well as to concontanate two strings. 
12. It is easier to maintain a  C ++ program as error can be easily located and rectified. It also provides a feature called exception handling to support error handling in your program. 
13 C ++ programs can easily be extended as it is very easy to add new features into the existing  program.
14. New programs can be developed in less time as the existing code can be reused.  
15. The Object Oriented concepts like data hiding, encapsulation and data abstraction can easily be implemented using keywords class, private, public and protected.  
16. C ++ provides a namespace control mechanism for restricting the scope of classes, functions and global objects.  

STRUCTURE OF C ++ PROGRAM 

A program consists of different sections.  Some of them are mandatory but some are optional.  The optional section can be excluded from the program depending upon the requirement of the programmer.


The above figure shows the various parts of C ++ program.  Now we will discuss the various sections briefly.  
1. DOCUMENTATION SECTION: It includes the comments that improves the readability of the program.  A comment is a non executable statement that helps to read and understand a program.  A comment is message that exists only for the programmer and is ignored by the compiler.  A good program should include comments that describe the purpose of the program, author name, date and time of program creation.  The documentation section is optional.  Comments may appear anywhere in the program.  In C ++, there are two types of comments.  
* Single Line Comment: It starts with double slash symbol (//) and terminates at the end of current line.  For example: 
// calculate sum of two numbers.  
* Multi Line Comment: C ++ programmer can also use C comment style that begins with / * and ends with * /.  
The Multi Line Comment can be written as 
                    / * Calculate 
                                        of two numbers * / 
Comments are very helpful in identifying the purpose of the program or its logic at some later stage.  The symbols / *,* / can also be used to comment single line but we normally prefer // symbol for commenting single line.  
RULES FOR WRITING COMMENTS: 
a) Multi Line Comments cannot be nested.  
        / * calculate / sum of * / two numbers * / 
b) A single line comment can be nested within the multi Line comment.  
        / * calculate sum and // product of two numbers * / 
c) Any number of comments can be given at any place in the program.  But a programmer should avoid writing unnecessary comments.  
d) You cannot use comments within the looping and decision making statements.  
          for (i = 0; i <= n // i less than or equal to n; i ++) 
e) Comment should never include special characters such as form feed and backspace.  
2. PREPROCESSOR DIRECTIVE SECTION: It includes various preprocessor directives.  All the preprocessor directives starts with a hash sign (#).  A preprocessor directive is a message to the compiler and it is handled by a part of the compiler known as a preprocessor.  The preprocessor directives are processed by the preprocessor before that program is actually compiled.  The main function of preprocessor is to include other files in the source file, specifying symbolic constants etc.  Some of the most common preprocessor directives are:
a) #include directive: It tells the preprocessor to include the contents of specified file in the source file in place of the directive before compilation.  
         #include <iostream.h> 
In this example, the #include directive tells the preprocessor to include the contents of iostream.h file into the source file.  The iostream.h is a header file that contains the declaration of all the standard Input / Output functions of C ++.  The compiler will generate errors if this file is not included in the source code.  
b) #define directive: The other most often used preprocessor directive is #define directive.  The symbolic constants can be specified using #define.  The symbolic constants allows the programmer to define a name for constant and use that name throughout the program as shown in the following example 
           #define PI 3.14159 
It replaces every occurring of symbolic constant PI with a value 3.14159 before the program is compiled.  The main advantage of using it is that if you want to change the value of symbolic constant PI, it needs to be modified only once in #define directive.  #define can also be used to generate macro functions.  It makes the following form:
      #define MACRO_NAME (argi, arg2, ..., arg4) 1 / macro definition.  
There should not be any space between macro name and the left parentheses, to define a function like macro definition.  All subsequent occurrences of MACRO_NAME with specified number of arguments will be replaced with the macro definition.  This directive is very popular in C but not recommended in C ++.  
The general rules for defining a preprocessor are as follows: 
1) All preprocessor directives must begin with hash sign (#).  
2) Preprocessor directive should not end with semicolon because they are not C ++ statements.  
3) Only one preprocessor directive can occur in a line.  
4) Preprocessor directive may appear at any place in the source file but mostly it is given at the beginning of programs.  
5) There should be no space between the hash sign (#) and the directive in most CH compilers.  
6) Don't use absolute path names when including header files using #include directive.  
3. CLASS SECTION: It describes information about user - defined classes present in the program.  A class is a collection of data (variable) and functions that operate on that data.  A class declaration or definition includes variable declaration, function declaration or the definition (code) of the function.  The definition of the function may also be defined outside the class declaration i.e.  in the class function definition.  
The function declaration provides information to the compiler regarding the name of the function, required arguments and return type of the function.  The function definition is that part of the function where the code of the function is defined.  More details about classes will be covered later on.
 4. MAIN PROGRAM SECTION: The execution of the program always starts from this part of the program.  It includes local declaration part and executable part.  The local declaration part includes the declaration of variable that can only be accessed within the main ().  The executable part consists of statements that are executed in a given order.  
5. SUBPROGRAM SECTION: It includes various user-defined functions.  These user defined functions are not the part of any class.  To illustrate different parts of the program, let us consider a program to add the numbers.  
To calculate the sum of two numbers.  
     //To add two numbers
 #include <iostream.h>   // preprocessor directive 
#include <conio.h> 
int main ()     // main () function heading 
{
 int a, b, sum;  // variable declaration 
cout << "Enter Number a ="; // Output statement 
cin >> a;  // Input Statement 
cout<<"Enter Numbers b =";
cin>>b;
sum=a+b;
cout<<"sum is "<<sum; //Output statement
getch();
return 0;
}

Algorithm: 
Step 1 - Declare the required program variables a, b and sum.  
Step 2 - Read the values ​​of a and b.  
Step 3 - Calculate their sum i.e.  sum = a + b Step 
4 - Write the calculated result i.e.  write sum.  
Step 5 - Stop 
Program's explanation: 
1. The first line specifies the comment describing the purpose of the program.  It is non executable statement.  
2. The second line contains a preprocessor directive that instructs the compiler to include the file iostream.h (Input / Output Header File) at this point.  This file must be included in the program to perform any Input / output operation.  Similarly, the header file conio.h is handled.  
3. The fourth line main () is a entry point of a program execution.  The parentheses after main () indicates that it is a function.  Every C ++ program must contain only main () function.  When a program is executed, the compiler searches for the main () function to start execution from that point.  It is necessary to specify the return data type of every function used in C ++.  The return type of main () is always int.  Thus main () retuns an integer value to the calling environment (i.e. operating system) that invokes it.  The last statement return 0;  tells main () to retun the value o to the calling environment, from where the calling environment comes to know that the program has executed successfully.  Therefore, every main () in C ++ should end with return 0 ;.  Any other value specified with return statement indicates that the program has not run properly as per operating system convention.  If you do not specify the retun statement, the compiler generates a warning.  In some older versions of C ++, the return type of main can be specified as void in order to compensate for removal of return statement but it is not recommended in newer versions. 
4. The remaining lines comprises the set of statements which includes variables declaration, input statements, output statements and assignment statements enclosed in braces where the beginning is specified by starting brace (1) and end is specified by closing brace ().  
cin statement is used to input any value from the keyboard and cout statement is used to display any message or value on the screen.  
On executing this C ++ program, we get 
Enter Number a = 5 
Enter Number b 10 
Sum is 15

No comments

Powered by Blogger.