Lesson #2: C++ Programs

 

This lesson covers how to use C++ compiler software to write a simple program.  It also covers the basic parts of a C++ program.

 

Part I: How to write a simple program with a C++ compiler

 

A C++ program is a sequence of instructions for a computer written as numerous lines of text.  The text is mostly located in *.cpp files (source / program files).  Some related definitions are sometimes contained in *.h files (header files).  These text files are normally written using the built in text editor of a compiler software package.  The compiler translates the text files into an executable file (*.exe) which contains low level instructions the computer can execute / run efficiently.  Once a program is compiled the executable file can be run without using the compiler software.  It’s basically a new program / software item you can give to all your friends who use the same operating system / processor type.

 

A program in general can be composed of more than one cpp source file so a “project” is needed to keep track of this group of related files.  Groups of related projects can also be organized into a “workspace”.  However, for the purpose of this course each workspace will only contain one project.

 

In order to write a program you first make a new project.  You can then change the text inside the cpp file of the project to make your own program or cut and paste the text of an example into the cpp file.  To compile your project you select “Rebuild Project” from the IDE.  This produces an *.exe file that can be executed / run inside the compiler IDE or without the IDE directly from a folder.

 

The following video shows how to make a new program and some related tasks with CodeLite:

http://users.encs.concordia.ca/~bwgordon/lesson2_new_program_codelite.mp4

 

The following video shows how to make a new program and related tasks with an online compiler:

http://users.encs.concordia.ca/~bwgordon/lesson2_new_program_online_compiler.mp4

 

Note in order to watch the videos on this web site you should right click (two finger click on Mac) on the link of the video and select “Save Link As …” on Google Chrome or “Download Linked File As ...” on Safari.  The file can then be opened and played with VLC.

 

 

Part II: The basic parts of a C++ program

 

In this part we write a simple “hello world” program and discuss the basic parts of a C++ program. 

 

It should be noted that most compiler software (even for other computer languages) has the capability (normally a wizard or new project option) to produce a “hello world” program.  This program, which basically prints “hello world” to the screen, serves an important role since it provides a working example for a compiler that allows you to see output on the screen.  At this point you can alter the example to make your own program.  Without a hello world program you can spend a lot of time with a new compiler trying to get something that initially works.

 

Here is a video that discusses the basic parts of a C++ program:

http://users.encs.concordia.ca/~bwgordon/lesson2_hello_world.mp4

 

Note that some compilers (such as newer versions of CodeLite) require you to put the following line at the top of your program in order to use the getchar() function:

 

#include <cstdio> // needed for getchar()

 

In the video I didn’t have this line, but normally it’s a good idea or you might get errors with some compilers.

 

Here is the program discussed in the video:

http://users.encs.concordia.ca/~bwgordon/lesson2_helloworld.rar

 

At this point you should now be able to make your own project and C++ program using a compiler.  You are then ready for the next lesson.