Home

A C++ Application and its Files

 

Source Files

C++ is one of those languages that can use different files for a program. The most usual file used in a C++ program is called a source file. It is a file with the .cpp extension. This type of file simply contains code that the compiler would need to know what the program is supposed to do. When using Microsoft Visual C++, you can create a source file at any time, whether you are working on a project or not. You can even create a source file to use in another project.

To create a source file in Microsoft Visual C++ 2005:

  • If you are not working on a particular project, for example if you have just opened Microsoft Visual C++, on the menu, you can click File -> New -> File... In the New File dialog box, click C++ File (.cpp) in the Templates list and click Open. A file with the default name of Source1.cpp would display. You can then enter your code. To save the file, on the main menu, you can click File -> Save Source1.cpp. 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 C++ File (.cpp) in the Templates list. Give the file a name in the Name edit box and click Add. A new empty file would be added to the current project. You can then enter your code

If you already have a source file somewhere on a drive or on the network and you want to use that file in your project, on the main menu, you can click Project -> Add Existing Item... In the Add Existing Item dialog box, in the Look In combo box, display the folder that contains the file, locate the file you want to use, select it, and click Add.

 

Practical LearningPractical 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

Header 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 our lessons, 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.

Practical LearningPractical Learning: Creating a Header File

  1. To create a header file, on the main menu, click Project -> Add New Item...
  2. In the Templates list, click Header File (.h)
  3. In the New box, type Property
     
    Add New Item
  4. Click Add
  5. In the empty file, type the following:
     
    class CProperty
    {
    	int ShowWelcome();
    };
  6. Click the Property.cpp tab and type the following in its file
     
    #include "stdafx.h"
    #include "Property.h"
    
    int CProperty::ShowWelcome()
    {
    	return 0;
    }
  7. Click the RealEstate1.cpp tab and change it to have the following contents:
     
    // RealEstate1.cpp : main project file.
    
    #include "stdafx.h"
    #include "Property.h"
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
        Console::WriteLine(L"Hello World");
        return 0;
    }
  8. To save all files, on the main menu, click File -> Save All.
    Except when creating the files, everything we wrote was for experimental purposes. Don't be concerned with any section you may not understand. We will come back to everything as it becomes necessary

Assemblies

After creating the header and source files, when you execute the project, the compiler will find and "synchronize" them to create an executable. The result that the compiler produces by unifying these and other necessary files of a project is called an assembly. If you are creating projects that you intend to use solely in C++, you usually don't care about this and it is done transparently.

Applications

We mentioned that the most fundamental function used in C++ was main(). The functionality of a program starts with main() and ends in main(). For this reason, main() is considered the entry and the exit point. We also mentioned that, with Microsoft Visual C++, you could create libraries that you can then sell or distribute to other programmers. One of the particularities of a library, as compared to the traditional projects we will mostly create, is that a library doesn't have an entry point: it may contain just functions and/or classes. You create it with the necessary header and source files, but you don't need to include an entry point. After creating or adding the necessary files to the project, you can then compile it. In this case, the compiler would also create an assembly.

When an assembly has an entry point, which is the case for a regular executable, it is called an application.

The Public and Private Objects of an Assembly

One of the most useful aspects of a programming studio such as Microsoft Visual Studio (or Borland Developer Studio) is that it allows you to create objects, such as classes, using a language of your choice, but that other programming environments or languages, can utilize. The programmers of these languages don't need to know the original language. Based on this, you can create a class in C++ and use that class in Visual Basic, when indeed these are two completely different languages. In order to make this happen, as you may suspect, there are rules you must follow.

If you create a class in an assembly, by default, that class class can be used only by the other files or objects of the same project. Such a class is referred to as private. If you want, in your project, you can create a class that can be used in another project, whether the project is in C++, in C#, or in Visual Basic. Such a class must be made public. The ability to be public or private for a class is controlled using the public and private keywords.

As seen so far, you can create a class as follows:

class house
{
};

As mentioned already, such a class is considered private. If you want this class to be accessible outside of its assembly, you must make it public. To do this, precede its struct or class keyword with the public keyword. Here is an example:

public class house
{
};

If you don't specify public, the class is considered private. Based on this, the notation:

class house
{
};

is the same as:

private class house
{
};

Practical LearningPractical Learning: Controlling the Assembly Access of a Class

  1. To make the CProperty class public, change the Property.h file as follows:
     
    public class CProperty
    {
    	int ShowWelcome();
    };
  2. Save the file

Creating a Class

Microsoft Visual Studio provides a more flexible technique of creating a header file combined with a source file. This allows you to get both files with one click. Both files are created from a class. To create the class, on the main menu, you can click Project -> Add Class... In the Templates list of the Add Class dialog box, click C++ Class and click Add. Enter a name if the Class Name text box. By a tradition dating from the previous versions of Microsoft Visual C++, you should start the name of the class with C. If you start the name of the class with C, a header file and a source file without the C would be suggested, which you should accept, and click Finish. To keep this Visual C++ tradition, we will start most names of our classes with C.

Practical LearningPractical Learning: Creating a Class

  1. On the main menu, click Project -> Add Class...
  2. In the left frame, expand the Visual C++ node and click C++
  3. To create a class, in the Templates list, click C++ Class
     
  4. Click Add
  5. In the Class Name box, type CRealEstateAgent
  6. Click Finish.
    Once again, don't mind the skeleton code that was written. The purpose of this exercise was to show you how to create a class
 

Previous Copyright © 2006-2007 FunctionX, Inc. Next