Creating a New C++ Source File

Started by sukishan, Aug 13, 2009, 12:49 PM

Previous topic - Next topic

sukishan

Creating a New C++ Source File
In your project you will want to have at least one C++ source file containing the code for your program. If you have just created a new project, that project will probably already be active. Select Project, Set Active Project to see what project is currently the active one. If it is not the one to which you want to add your C++ source file, click on the correct one.

Once you are sure that you have the correct project active, select File, New. Click on the Files tab and click on C++ Source File. Then fill in the desired file name. The .cpp extension will be added automatically; there is no need to type that.

The project name and location should be correct, but glance at them to be sure. The Add to Project box should be checked and the correct project name should be shown below it. See this picture of these choices to have a better idea of what this looks like. Finally, click on OK.

You should then be in the editor, editing a blank .cpp document with the correct name. Type in your desired code. Select File, Save to save your file. You can also copy in code from another file by selecting Insert, File as Text. It is also possible to add code to a new file by using Cut, Copy, and Paste (found under Edit) to bring in code from another file.

Sometimes you will want to use more than one C++ source file. It is typical to place the main function in one source file, other stand-alone functions in another source file, and functions belonging to a user-defined class in their own source file. All of these files that belong to the same program must be added to the same project.

Hint: Make sure that all of your project's .cpp and .h files (and any data files as well) are all located under your project directory. You can use My Computer to check on the locations of these files. Many students run into problems with their projects because they have several versions of their files in various directories and don't know which ones are being used by their project.
A good beginning makes a good ending