Tuesday, December 29, 2015

Cout, Cin



   >>> Using the standard input and output library, we will be able to interact with the user by printing messages on the screen and getting the user's input from the keyboard.

   >>> The standard C++ library include the header file iostream, where the standard input and output stream objects are declared.


  Standard Output ( Cout )

   >>> By default, the standard output of a program is the screen and the C++ stream object defined to access it is cout.

   >>> Cout is actually an object, predefined in C++ to correspond to the standard output stream. A stream is nothing but the flow of data.

   >>> Cout is used in conjunction with the insertion operator, which is written as << ( two "less than" signs). It directs the contents of the variable on its right to the object on its left.

         cout <<"hello"; // prints hello on screen
         cout << 300;     // prints number 300 on screen
         cout << x;         // prints the content of x on screen

   >>> The << operator inserts the data that follows it into the stream preceding it.

   >>> In the examples above it inserted the constant string Hello, the numerical constant 300 and variable x in the standard output stream cout.

   >>> Notice that the sentence in the first instruction is enclosed between double quotes (") because it is a constant string of characters.

   >>> Whenever we want to use constant strings of characters we must enclose them between double quotes (")  s that they can be clearly distinguished from variable names. For example, these two sentence have very different results:

           cout << "Hello"; // Prints Hello
           cout << Hello;     // Prints the content of Hello variable.

   >>> The insertion operator ( << ) may be used more than once in a single statement:

           cout << "Hello, "<<" I am " << " a C++ statement";

   >>> This last statement would print the message Hello, I am a C++ statement on the screen. The utility of repeating the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more than one variable:

   cout << " Hello, I am " << age << " years old and my zip code is " << zip code;

   >>> If we assume the age variable to contain the value 24 and the zip code variable to contain 90064 the output of the previous statement would be:

                        Hello, I am 24 years old and my zip code is 90064

   >>> It is important to notice that cout does not add a line break after its output unless we explicitly indicate it therefore, the following statements:

              cout << "This is a sentence.";
              cout << "This is another sentence.;

   >>> Will be shown on the screen one following the other without any line break between them:

   >>> Even though we had written them in two different insertions into cout. In order to perform a line break on the output we must explicitly insert a new line character into cout.

   >>> In C++ new line character can be specified as \n (backslash,n);

                               cout << "First sentence. \";
                               cout << "Second sentence. \nThird sentence.";

            output :
                               
                               First sentence.
                               Second sentence.
                               Third sentence.

   >>> Additionally, to add a new line, you may also use the endl manipulator.

   >>> For Example:

                                  cout << "First sentence." << endl;
                                  cout << "Second sentence." << endl;

              output :
                               
                               First sentence.
                               Second sentence.

   >>> The endl manipulator products a newline character, exactly as the insertion of  '\n' does, but it also has and additional behaviour when it is used with buffered streams: the buffer is flushed.

Monday, December 28, 2015

Header Files



   >>> Some of these instructions come in computer files that you simply "put" in your program. Theses instructions or files are also called libraries.

   >>> To make your job easier, some of these libraries have already been written for you so that as you include them in your program you already have a good foundation to continue your construction.


 >>> There are libraries previously written for you.

   >>> The libraries are files that you place at the beginning of your program as if you were telling the computer to receive its preliminary instructions from another program before expanding on yours.

   >>> The libraries are computer files they have the extension ".h". An example would be house.h or person.h. As you see, they could have any name; when you start creating your own libraries you will give your files custom and recognizable names.

   >>> The first library we will be interested in is called iostream. It asks the computer to display stuff on the monitor's screen.

    #include iostream.h

   >>> There are usually two kinds of libraries or filers you will use in your programs: libraries that came with c++ and those that you write. To include your own library you would enclose it between double quotes like this

       #inlclude "book.h"

   >>> When you include a library that came with c++, you enclose it between < and > as follows:

        #inlucde <iostream.h>

   >>> Following this same technique, you can add as many libraries as you see fit.

   >>> Before adding a file, you will need to know what that file is and why you need it. This will mostly depend on your application. Suppose you want to write a program in which you implement mathematical operations that time you have to add Math.h header file.

Saturday, December 26, 2015

Example 2: Demo Of Macro Functions.



     >>> Demo Of Macro Functions.

             #include<iostream.h>
             #include<conio.h>

             #define Sqr(x)(x*x)
             #define Three (x)(x*x*x)

             main()
             
                   {
                      int a = 5, b = 0;

                      clrscr();

                      b = Sqr(a);
                      cout<<"\n\n Square = "<<b;

                      b = Three(a);
                      cout<<"\n\n Three = "<<b;

                      getch();
                   }



   >>> Macros suffer from four problems in C++> The First is that they can be confusing if they get large because all macros must be defined on one line. You can extend that line by using the backless character (\), but large macros quickly become difficult to manage.

   >>> This second problem is that macros are expanded in-line each time they are used. Thus means that if a macro is used a dozen times the substitution will appear 12 times in your program rather than appear once as a function call will.

   >>> On the other hand, they are usually quicker than a function call because the overhead of a function call is avoided.

   >>> The fact that they are expanded in-line leads to the third problem which is that the macro does not appear in the intermediate source code used by the compiler and therefore is unavailable in most debuggers.

Macro Functions


   >>> The #define directive can also be used to create macro functions.

   >>> A macro function is a symbol created using #defines and that takes an argument much like a function does. The preprocessor will substitute the substitution string for whatever argument its given.


   For Example, You can define the macro TWICE AS

        #define TWICE(x)                ((x) * 2)

        and then in your code you write
     
         TWICE(4)


       The entire string TWICE(4) will be removed and the value 8 will be substituted!

   >>> When the pre compiler sees the 4, it will substitute ((4) * 2), which will then evaluate  to 4 * 22 or 8.

   >>> A macro can have more than one parameter and each parameter can be used repeatedly in the replacement text. Two common macros are MAX and MIN:

    #define MAX(x,y) ((x) > (y) ? (x) : (y))
    #define MIN(x,y) ((x) > (y) ? (x) : (y))

   >>> Note that in a macro function definition the opening parenthesis for the parameter list must immediately follow the macro name with no spaces.

   >>> The preprocessor is not as following of white space as in the compiler.

   >>> If you were to write
 
           #define MAX (x,y) ((x) > (y) ? (x) : (y))

  and then tried to use MAX like this,

        int x = 5, y = 7, z;

        z = MAX (x,y);

  the intermediate code would be
        int x = 5, y = 7, z;
        z = (x,y)  ((x) > (y) ? (x) : (y)) (x,y)


   >>> A simple text substitution would be done rather than invoking the macro function.

   >>> Thus, the token MAX would have substituted for it (x,y)  ((x) > (y) ? (x) : (y)) and then that would be followed by the (x,y) which followed MAX.

   >>> By removing the space between MAX and (x,y), however, the intermediate code becomes:

           int x = 5, y = 7, z;
           z = 7;








Monday, December 21, 2015

Preprocessor



   >>> The preprocessor looks for preprocessor construction, each of which begins with a pound symbol (#).


   >>> Using #define 


   >>> The #define command defines a string substitution. If you write #define BIG 512.

  >>> You have instructed the pre-compiler to substitute the string 512 wherever it sees the string BIG. This is not a string in the C++ sense. The characters 512 are substitute in your source code wherever the token BIG is seen. A token is a string of characters that can be used wherever a string or constants or other set of letters might be used.

   >>> With the use of above code you are going to define a Constant Value.

What is a compiler?


   >>> A computer cannot understand the spoken or written language that we humans use in our day to day conversations and likewise, we cannot understand the binary language that the computer uses to do it's tasks.

   >>> It is therefore necessary for us to write instructions in some specially defined language  in this case C++, which we can understand then have that very precise language converted in the very terse language that the computer can understand. This is the job of the compiler.

   >>> A C++ Compiler is itself a computer program whose only job is to convert the C++ program from our to form to a form the computer can read and execute.

   >>> The original C++ program is called the "Source code", and the resulting compiled code produced by the compiler is usually called an "object file".

   >>> One or more object files are combined with predefined libraries by a linker sometimes called a binder, to produce the final complete file that can be executed by the computer.

   >>> A library is a collection of pre - compiled "object code" that provides operations that are done repeatedly by many computer programs.

   >>> Any good compiler that you purchase will provide a compiler but an editor a debugger, a library and a linker. On-line documentation and help files are usually included and many compilers have tutorial to walk you through the steps of compiling, linking and executing your first program.

Comment



   >>> The comments indicate that the compiler should ignore everything following it.

   >>> This allows you to add English explanations to what might otherwise be confusing code.

   >>> You have the freedom to comment your code as much as you like some programmers write code with no comments at all; others write several lines of comments for each line of C++ code. It's all up to you.

   >>> There are  types of comments. Single line comment and Multi line comment.

   >>> To write a single line comment on one line, type two forward slashes // and type the comment. Anything on the right side of both forward slashes would not be read by the compiler. T

   >>> To write a a multi line comment you can start it with a forward slash / followed by an asterisk *, write the comment. To end the comment, type and asterisk * followed by a forward slash /.