Wednesday, 11 June 2014

1.3 Single Line Comment



C++ has borrowed the new commenting style from Basic Computer Programming Language (BCPL), the predecessor of the C language.  In C, comment(s) is/are enclosed between /* and */ character pairs.  It can be either used for single line comment or multiple line comment.

Single line comment runs across only one line in a source program.  The statement below is an example of single line comment:

/* I am a single line comment */

Multiple line comment runs across two or more lines in a source program.  The statement below is an example of multiple line comment.

/*  I am a multiple line comment.
     Hope you got it  */

Apart from the above style of commenting, C++ supports a new style of commenting.  It starts with two forward slashes i.e., // (without separation by spaces) and ends with the end-of-line character.  The syntax for the new style of C++ comment is as shown below:

int acc;                            // Account Number
acc = acc + 1;                         // adding new account number for new customer

In C, the above two statements are written as

int  acc;                          /* Account Number */
acc = acc + 1;                         /* adding new account number for new customer */

The above examples of comments indicate that, C++ commenting style is easy and quicker for single line commenting.  Although, C++ supports C style of commenting, it is advisable to use the C style for commenting multiple lines and the C++ style for commenting a single line.

Some typical examples of commenting are listed below:
// this is a new style of comment in C++
/* this is an old style of comment in C++ */
// style of comment runs to the end of a line
/* runs to any number of lines but hard to type and takes up more space and coding
    time also. */
/* Here is a comment followed by an executable statement */  a = 100;
// Here is a comment followed by a non-executable statement a =100;

Large programs become hard to understand even by the original author (programmer), after some time has passed.  Comments will always restate the nature of a line of code. Comments which explain the algorithm are the mark of a good programmer.

The compiler completely ignores comments, therefore they do not slow down the execution speed, nor do they increase the size of the executable program.  Comments should be used liberally in a program and they should be written during the program development, but not as an after-thought activity.

The program simpInt.cpp in example 1.7, for computing the simple interest. It intends to demonstrates how comments aid in the understanding and improving readability of the source code.


Example 1.7

// simpInt.cpp: Simple interest computation
#include <iostream.h>
main()
{
  // data structure definition
  int principle; //principle amount
  int time;   //time in years
  int rate;   //rate of interest
  int SimpInt //simple interest
  int total; /* total amount to be paid back after
                  ‘time’ years */

     // Read all the data required to compute simple interest
  cout << “Enter Principal Amount: “;
  cin >> principle;
  cout << “Enter Time (in years): “ ;
  cin >> time;
  cout << “Enter Rate of Interest: “;
  cin >> rate;
 
    //compute simple interest and display the results
  SimpInt = (principle * rate)/100 * time;
  cout << “Simple interest = “;
  cout << SimpInt;

   // total amount  =  principle amount  +  simple interest
  total = principle + SimpInt;
  cout << “\n Total Amount = “;
  cout << total;
    }





Runing the programe the output will be as shown below:

Enter Principle Amount: 1000
Enter Time (in years): 2
Enter Rate of Interest: 5
Simple Interest = 100
Total Amount = 1100



1.4     Literal – Constant Qualifiers

Literals are constants to which symbolic names are associated for the purpose of readability and ease of handling standard constant values.  C++ provides the following three ways of defining constants:
  • #define preprocessor directive
  • enumerated data types
  • const keyword

The variables in C can be created and initialized with a constant value at the point of its definition.  For instance, the statement

float  PI = 3.1452;

Defines a variable named PI, and is assigned with the floating-point numeric constant value 3.1452. It is known that the constant value does not change.  In the above case, the variable PI is considered as a constant, whose value does not change throughout the life of the program (complete execution-time). 

However, an accidental change of the value of the variable PI is not restricted by C. C++ overcomes this by supporting a new constant qualifier for defining a variable, whose value cannot be changed once it is assigned with a value at the time of variables definition.  The qualifier used in C++ to define such variable is the const qualifier.  The syntax of defining variables with the constant qualifier is shown below figure 1 below. Note that Data Type is omitted; it is considered as int by default.


The following examples illustrate the declaration of the constant variables:

  • const float PI = 3.1452;
  • const int TRUE = 1;
  • const char *book_name = “OOPs with C++”;

Example 1.8 is the program, area.cpp that illustrates the declaration and the use of constant variables.


Example  1.8


// area.cpp: area of a circle
#include <iostream.h>
const float PI = 3.1452;

main()
{
  float radius;
  float area;
  cout << “Enter Radius of Circle:”;
  cin >> radius;
  area = PI * radius * radius;
  cout << “Area of Circle = “ << area;
}

Entering the value of radius as 2 and running the program, the output will be as follows:

Enter Radius of  Circle: 2
Area of Circle = 12.5808

In the above program, the use of the statement such as

PI = 2.3;

to modify a constant type variable leads to the compilation error: Cannot modify a const object.

Thus, the keyword const, is used before a type to indicate that the variable declared is constant, and may therefore not appear on the left side of the assignment operator (=).  In C++, the constqualifier is used to indicate the parameters that are to be treated as read-only in the function body.


No comments:

Post a Comment