Page 26 - Modular_V2.0_C++_Flikpbook
P. 26

3                                                       INPUT AND OUTPUT



                                                                                                        IN C++








                                   Your Aim


                                   to learn about:
                                           Output                                       Input

                                             Escape Sequence Characters                    Comments


                  Every program requires some input to work and displays some output after processing the input.
                  C++ also allows you to give input to a program and get the output from the program. Every
                  input/output in C++ is considered a stream of characters. C++ provides two objects to take the

                  input and to display the output, which are cin and cout. Let us discuss these in detail.

                     OUTPUT

                  The  cout object is used to display output  on a computer  screen (monitor).  It is part  of the
                  ostream class and is also known as the Standard Output Stream. The cout object works with
                  the stream insertion operator (<<) to show messages or values.

                  For example:

                       cout<<"Output on the screen";
                  The above statement displays the message Output on the screen on the monitor. The stream
                  insertion operator can be used multiple times in a single statement to combine and display
                  multiple outputs. For example:

                   cout<<"Hello"<<" from "<<" C++";
                  You can also print the variable value using the cout object. For example:

                   cout<<x;
                   cout<<120;
                  Chaining  insertions  is particularly  useful when  combining literals and  variables  in a  single

                  statement. For example:
                   cout<<"I am"<< name<<". I am"<<age<<" years old.";
                  In the above statement, the values of the variables name and age are printed along with text,
                  producing an output like:

                   I am Chirag. I am 18 years old.

                   24
                          Touchpad MODULAR (Ver. 2.0)
   21   22   23   24   25   26   27   28   29   30   31