When you edit a file with one of the common C++ extensions cc, cpp, or C, emacs guesses that you are editing a C++ file. You can tell this feature is turned on if you notice the (C++) editing mode indicator in the caption bar at the bottom of the buffer window.
There are a number of useful features, including automatic indentation, a built-in parenthesis balancing test, and color-coded highlighting.
It is important to write code that is easy to read. It helps to prevent mistakes, and others who are not familiar with your code will find it easier to read. Experienced C++ programmers are in the habit of indenting code to make it more legible. Generally speaking, a code block enclosed in curly braces is indented. The braces themselves are not indented, but are aligned with the beginning of the block. If you are nesting code blocks, the inner blocks are indented still more. The example codes in this class follow these standard rules.
Fortunately, you don't need to learn these rules. Emacs will help you. In C++ mode emacs uses the tab key for this purpose, since actual tabs are not necessary in C++ code. To indent a line of code, simply hit the tab key at any time while typing or editing the line. Emacs figures out how to indent based on your previous lines. So it is a good idea to use the tab key in every line of the code from top to bottom as you write it.
When you become accustomed to the appearance of properly indented code, automatic indenting can help you spot some common errors in syntax. For example, suppose you forget a semicolon at the end of a statement. When you type the next line of code and hit tab, the indentation will look strange. That is an indication something is wrong. For another example, if you forget a closing brace and continue typing, the next line will indent to the same level as the previous line, instead of indenting less. That might help you notice something was missing. And when you come to the end of your code, if the last brace is indented, so it is not in column 1, you must have forgotten a closing brace somewhere in your code.
If you have already written your code without using the tab key on each line, don't despair. You can still beautify your code by starting at the top line of the code, hitting tab, hitting the down arrow, then tab again, etc., working your way to the end of the code. As you go along the indentation will be adjusted. This is a particularly good way to find an unbalanced brace that is causing the common compiler message ``parse error at end of input.''
An unbalanced parenthesis is one of the most common sources of a programmer's grief. So emacs has a couple of useful features for guarding against them.
You may have noticed that when you are writing C++ code with emacs and type a closing parenthesis, bracket, or brace, the cursor does a brief hop to the matching opening character and then returns to its working position. If there is a problem finding the match, emacs flashes the message ``Mismatched parentheses'' in the minibuffer. If the match is so far back in the code that it isn't visible, emacs flashes the context of the matching parenthesis in the minibuffer.
Parenthesis checking can also be done with the mouse. Double left click on any parenthesis, brace, or bracket. Emacs then shows the matching symbol and highlights the code between them.
This is automatic with our setup, but in case you don't get it, The pull-down menu ``Options'' has a number of radio buttons. If you click on the ``Active Region Highlighting'' feature, your code will come alive in color. Since there really isn't a compiler behind the highlighting engine, there is only so much it can do. But it is very useful for spotting unbalanced quotes in strings and for distinguishing comments from code.