Wednesday, January 17, 2007

Introduction to C++ Programming [Part 2]

Previous Related Posts: Part1

Compiling a C++ Source

In general under Unix, compiling a C++ source MySource.C is done as follows.

g++ MySource.C
This produces a binary program (a.out). Compilation and linking is done in one go.

If the default name is not wanted, the name of the executable can be specified using the -o flag:

g++ -o MySource MySource.C

If only a compilation is required, the compiled module can be generated using the -c flag:

g++ -c MySource.C

This produces the file MySource.o, which can be linked to other modules later on.

First Program

#include <iostream>

using namespace std;

int main()


cout << "My First Program";

cout << "\n";

return 0;


Once you compile and run the program, you should see the following output:

My First Program



If you are using an older compiler, you might need to use #include <iostream.h >; instead of #include <iostream>; in that case, you also would omit the using namespace std; line.

Some windowing environments run the program in a separate window and automatically close the window when the program finishes. You can make the window stay open until you strike a key by adding the statement cin.get(); before the return statement.

How this works:

Every C++ program should have a main() function. When you run a C++ program, execution always starts at the beginning of main() function. In general, a function is a block of code that performs one or more actions. Usually functions are invoked or called by other functions, but main() is exceptional. main(), like all functions, must state what kind of value it will return. The return value type for main() in this example is int, which means that this function returns an integer.

C++ uses a preprocessor. As the name suggests, this is a program that processes a source file before the main compilation takes place. You don’t have to do anything special to invoke this preprocessor. It automatically operates when you compile the program. Preprocessor processes directives that begin with #.

For example, #include is a preprocessor directive. This directive causes the preprocessor to add the content of iostream file to your program. This data is used by the cout in your program. Your original file is not altered, but a composite file formed from your file and iostream goes to the next stage of compilation.

Header files

Files such as iostream are called include files or header files. The C tradition has been to use the h extension with header files as a simple way to identify the type of file by its name. C++ header files have no extension.

E.g.: iostream.h in C and just iostream in C++

There are also C header files that have been converted to C++ header files. These files have been named by dropping the h extension and prefixing the file name with a c.

E.g.: math.h in C changed to cmath in C++


Namespacing support is a new C++ feature designed to simplify the designing of programs that combine pre-existing code from several vendors.

One potential problem is that you might use two prepackaged products that both have the functionality, say update(). If you then use the update() function, the compiler won’t know which version you mean. The namespace facility solves this problem. It allows a vendor to package the functionality into a container called namespace. The namespace should be unique among vendors. If the two vendors place their functionality in namespaces A and B, then the full name for their update() function would be A::update() and B::update() respectively.

cout and many other predefined objects are placed under the standard namespace called std. So the full name for cout is std::cout. You don’t need to prefix each cout with std::, if you use the directive using namespace std; at the beginning of your program. Any predefined function/object, which does not have a prefix, is then assumed to be under std namespace.


cout is the standard output stream of C++. Data is written to the output stream using the insertion operator <<. It can display variety of things including string, numbers, etc. The stream cout is actually an object of a given class. This is defined in the iostream header file. “\n” (newline character) is a special formatting character used to move the cursor to next line. You could use the keyword endl instead of “\n” in the above example.

cout << “My First Program”;

cout << endl


cout << “My First Program” << endl

Coding style

C++ identifies the end of a statement by a semicolon. Carriage return or any other special character does not have any meaning. This means you can spread a single statement over several lines or place several statements on one line. Although C++ gives you much formatting freedom, your program will be easier to read if you follow a sensible style. Most of the C++ programmers follow this style;

· One statement per line

· An opening and closing brace for a function, each of which is on its own line

· Statements in a function indented from the braces

· No white spaces around the parentheses associated with a function name

In the next lesson, we'll be looking at C++ data types. Until than, Happy coding!

No comments: