Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

1.7.2Practical Learning: Creating a Source File

  1. To create a source file, on the main menu, click Project -> Add New Item...

  2. In the Templates list, click Source File (.cpp)

  3. In the New box, type Property

  4. Click Add

1.7.3Header Files

C++ is one of the few languages that have or use the concept of header files. A header file is one that contains code that source files or other header files of a project use. In the strict sense, there is no rule as to what type of code you can put in the header file. Traditionally, programmers put in a header file the items that are not defined. For example, you can put the "skeletons" of your functions or the "skeletons" of your classes. One reason you would do this is to only show to other people what your functions or classes look like, not how they behave. In some cases, if you are creating a library (a DLL) that you want to sell or distribute to other programmers, a header file allows you to show these skeletons. You can then use a source file to define these skeletons. When you sell or distribute such a library, you don't include the source file. You distribute only the header file and the library itself. Since your customers or your audience cannot "read" what is in a library, they cannot access your hard work source file.

Because C# and Visual Basic don't use header files in the strict sense, some programmers don't encourage you to create them. In this ebook, we will adhere to C++ techniques. Therefore, we will create header files whenever we "feel like it".

A header file is a C++ document with the extension .h and sometimes .hpp. As mentioned for source files, you can create a header file at any time, whether a project is opened or not. In Microsoft Visual C++ 2005, to create a header file:

  • If you are not working on a project, on the menu, you can click File -> New -> File... In the New File dialog box, click Header File (.h) in the Templates list and press Enter. A file with the default name of Header1.h would open. After entering your code, to save the file, on the main menu, you can click File -> Save Header1.h. Save the file in any directory of your choice and give it a valid name

  • If you are working on a project already, on the menu, you can click Project -> Add New Item... In the Add New Item dialog box, click Header File (.h) in the Templates list. In the Name edit box, enter a valid name for the file and press Enter. You can then enter your code

If a header file exists somewhere in your computer in a network drive and you want to use it in your project, on the main menu, you can click Project -> Add Existing Item... In the Look In combo box of the Add Existing Item dialog box, display the folder that contains the file. Select the desired file and click Add.

If the foundation of a class had been specified in a header file and you want o use such a file in your source file, you must include it using the formula:

#include "FileName.h"

When we do more detailed studies of classes, we will see how the header and source files are used in these scenarios.