![]() |
Primary Details of Program Structure |
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 . . .
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.
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:
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 + 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 |
|
|
||