Home

Primary Details of Program Structure

 

Preprocessors: #include

Include

The preprocessor is used to give a special instruction to the compiler. It is typically placed at the beginning of a line and it is followed by a word that would perform an action. The formula to use the preprocessor is:

#action What-To-Do

There are various actions you could ask the compiler to perform.

The #include is used to include an external file in the current file. The file to include is called a header file, a library, or another kind of file you want the compiler to consider before going any further. For example, to include one of the header files that shipped with Bcb, such as calendar.h, you can write

#include <calendar.h>

To include one of your header files when you will have created one, you can write

#include “Circle.h”
 

Preprocessors: #define

The #define, called a directive, is used to direct the compiler to create or perform a (small) action. This action is called a macro. For example, you can ask the compiler to use "Rio de Janeiro" whenever it sees RDJ. To do that you can write

#define RDJ "Rio de Janeiro"

If you use the word RDJ in your program, the compiler would replace it with the defined name. You can also use the #define directive to create words that would be replaced with numeric values. Here is an example:

#include <iostream>

using namespace std;
using namespace System;

#define RDJ "Rio de Janeiro"
#define Print(Sentence) Console::WriteLine(Sentence)

int main()
{   
	cout << "City: " << RDJ << "\n";
	Print("Welcome to the Wonderful World of C++/CLI.");

	return 0;
}

This would produce:

City: Rio de Janeiro
Welcome to the Wonderful World of C++/CLI.
Press any key to continue . . .

Pragma Directives

 

Introduction

A compiler of one particular programming environment has characteristics that distinguish it from other compilers. To customize its behavior, some instructions can tell it how to behavior and how to treat some code sections when it encounters them. One of the instructions used by a programming to direct a compiler is done using the #pragma preprocessor. It is used to give specific instructions to the compiler based on the particular compiler that is being used to create the program. This means the #pragma directive completely depends on the compiler you are using.

Open Once

When you create a file for your project as we saw in the previous lesson, when the compiler is building the project, you can instruct it to open a certain file only once, during the lifetime of the program. This has the role of reducing the overhead of over and over checking again if the file has already been opened. To give this instruction, you should start the file with

#pragma once

Under this line, you can then include the rest of the lines of code for the file.

 

Code Editor Region Delimiter

Microsoft Visual Studio provides a particularly wonderful editor that provides a good level of control over the code you write. The characteristics include color-coded words, intuitive indentation, delimitation of sections of code, etc. Consider the following contents of the Code Editor based on what we have reviewed so far:

The Code Editor

Notice that there are - buttons on the left side of some lines of code. These allow you to collapse a section of code if you think you don't need to see it. To do this, you can click the - button. If you click that - button, it changes into a + button. Here is an example:

The Code Colors

The + button allows you to expand a hidden code section. This behavior of creating + and - buttons is part of the Code Editor of Microsoft Visual Studio (yes, many other programming environments use that behavior also). To create these sections, the Code Editor follows some rules. For example, it looks for the start and end of such items as directives, namespaces, classes, functions, etc.

Besides, or instead of, the sections of code created by the Code Editor, if you want, you want, you can create your own sections. To do this, start the section with

#pragma region Whatever

and end it with

#pragma endregion Whatever

When and where you start, the #pragma region expression is required. On the right side of this expression, you can type anything you want on the line. To end the section, type #pragma endregion, followed by anything you want. Consider the following example:

#pragma once

struct CHouse
{
	int protect()
	{
		return 0;
	}
};

#pragma region These are classes used for Student Registration
struct CCar
{
	int drive()
	{
		return 0;
	}
};

class CStudent
{
	int walk()
	{
		return 0;
	}
};
#pragma endregion We can just stop it here

int main()
{
	return 0;
}

You don't have to type anything on the right side of #pragma endregion. After creating the region, the Code Editor would display a - button to the left side of #pragma region with a line from there to the left of #pragma endregion:

This then allows you to expand and collapse that section at will:

We mentioned that you didn't have to type anything on the right side of #pragma endregion and you could leave it empty. In our example, notice that there is a rectangle with gray lines around the string that follows #pragma region. This rectangle doesn't cover the string that follows #pragma endregion. This means that if you don't type anything on the right side of #pragma endregion, that section on the right side the #pragma region line would not show.

 

Previous Copyright © 2006 FunctionX, Inc. Home