Handout 4 (Remarks)

  1. Every C++ must include the following (usually at the top).
  2. #include <iostream>

    using namespace std;

    Sometimes we may need to include other stuff also. That depends on our needs.

  3. C++ is case sensitive. So, you cannot replace any letter in the above two lines by its upper case.
  4. Comments in C++ are non-executable commands. They are used to explain steps in order to make the program more readable. But, they have no effect on the output of the program. The program simpky ignores them during execution.
  5. Comments can be included after // or between /* and */
  6. Every C++ program must include a function called main. This is how to declare it:
  7. int main()

    If you declare the function main this way, then you must include the statement

    return 0

    at the end of the function. Notice that returning 0 tells the computer that everything went ok. When that statement is executed control returns to the computer's operating system. If the return value is different than 0, then that means something went wrong. Notice that if you don't include the return statement, you do not get any error messages and you end up with the same desired output, because the compiler will assume that you wanted the function to be of type void. So, you should include the return statement.

    Note: You can declare the function main to be of type void. In that case, there should be no return statement.

  8. The body of the function must be included within braces. So, you start the body of the function main (or any other function) by { and you end it with }
  9. Notice that we usually prefer to write { and } on separate lines. It is not wrong to write other statements on the same line, but people don't do that.

  10. To jump to a new line, we usually use
  11. cout << endl;

    But you can also use

    cout << "\n";

    Or

    cout<<'\n';

    \n is the newline character in C++.

    Notice also that << is called the insertion operator

  12. You can include more than one statement on the same line. But, they must be separated by semicolons.
  13. To leave k empty lines in the output, you must have k+1 endl
  14. The following
  15. cout << "Welcome to";

    cout << endl;

    cout << endl;

    cout << "New York.";

    is the same as

    cout << "Welcome to" << endl << endl << "New York.";

    and is the same as

    cout << "Welcome to"

    << endl

    << endl

    << "New York.";

  16. When you compile and run a C++ program, say the name of the file is MyProgram.cpp, several files and a directory called Debug are created. All of them are created in the same directory where you're MyProgram.cpp is. In the Debug directory (whose size is at least 3 MB which means it cannot fit a 3 1/2 floppy disk), you'll find the object file which is called MyProgram.obj and the executable file which is called MyProgram.exe. Notice that you can create a shortcut to the .exe file and use it whenever you want without having to refer to the original .cpp file and without having to go through the compilation and e execution process.
  17. Reminder: bit is either the digit 0 or the digit 1. 1 byte=8 bits, 1 KB=210 bytes=1024 bytes, 1 MB=220 bytes, 1 GB=230 bytes.
  18. To read a user input from the keyboard, you use cin in combination with the extraction operator >>

Notice that the read value must be stored in a variable (a variable is a location in memory refrenced by an identifier that contains a data value that can be changed). We'll explain what we mean by

The format of the InputStatement is

cin >>Variable >>Variable >>….;

For example, suppose you want to read two real values and store the results in two variables whose names are a and b. Then, you can do this

cin >> a >> b;

OR you can do this

cin >> a;

cin >> b;

During program execution, you can enter the value of a, then space (one space or more it makes no difference, but people usually leave a single space), then the value of b (on the same line). Or, you can enter the value of a on a line, then the value of b on the next line.

Notice that what follows the extraction operator >> must be a variable. So, it is not quite the same as the insertion operator <<.

14. What follows the insertion operator << can be a variable, it can be a constant identifier (we'll explain it later), it can be an expression, it can be a sentence included within double quotes, it can be a single character included within single or double quotes, etc.