![]() |
Introduction to C++/CLI Code |
Although Microsoft had submitted the standards of C++/CLI for international approval, at the time of this writing, the company was still the only one that had published a compiler for this new language. Even Borland C++ Builder 2006 doesn't support C++/CLI. For this reason, we will study C++/CLI using Microsoft Visual C++ 2005. The most basic program you can create in C++ is called a console application. This type displays its results in a dark box that we will call the DOS window.
A file that you will use as a foundation for your program is called a Header File, this is because, as "head", this file controls some aspects of your program. Therefore, you will place it at the "head" section of your program. When placing a particular file at the head of your program, you are said to "include" it. As headers, these files have an extension of .h. To use such a file, you have to "include" it using the include keyword. Here is an example: include file.h You must include the extension; this allows C++ to know that you are including a header file. As a rule, when including a file, you must precede the line with a # sign. Therefore, our include line would be #include file.h There are usually two kinds of header files you will be using in your programs: those supplied to you and those that you create. You will mostly create your own files in the same folder where you create your program. There are two ways you let C++ know where a header file is located. If the file is located in the C:\Program Files\Microsoft Visual Studio 8\VC\include, which means that it was supplied to you, include it as: #include <file.h> If you created your own file, which means the file is probably located in the same folder you are creating your program, include it as follows: #include "myfile.h"
To create a console application, on the main menu of Microsoft Visual C++, you can click File -> New -> Project… The shortcut is Ctrl + Shift + N. In the Templates list of the New Project dialog box, you have two options:
As opposed to creating a new project, you can open a project that either you or someone else created. To open an existing project, on the main menu, you can click File -> Open -> Project... You can also click File -> Open Solution on the main menu. Alternatively, you can display the Start Page and click Open Project. In all cases, the action would display the Open Project dialog box. This allows you to select a Visual Project and open it.
Actually, a function is a little more than that, but for now, this is enough for us. The most fundamental function in C++ is called main. The main() function can take various forms, the easiest version is as follows: main() {} According to the C++ standard, the main function must return (we will learn what that word means when we study functions) a number. Based on this, it should be written as: int main() {} If it is written like that, then the last line in the body of main must be written as return 0; When we study functions, we will learn what this line means. Instead of writing everything in one line, you should spread your code to various lines to make it easier to read. Now we would have: int main() { return 0; } In future lessons, we will expand our knowledge of functions. For now, note that, besides main(), a program can have other functions. These functions can look like main or be significantly different. The first rule is that, to reduce confusion, no other function can be called main. Here is an example: int other() { return 0; } int main() { return 0; } In the same way, you can have as many functions as you judge necessary in your program. Here is an example: int other() { return 0; } int another() { return 0; } int main() { return 0; } When you have created a function as done above, in order to use it in your program, you must "call" it. From main(), the simplest way to call a function consists of writing its name followed by parentheses and a semi-colon. Here is an example: int other() { return 0; } int main() { other(); return 0; } There are various other aspects you would take into consideration. We will review them when we study functions. For now, the above code would work perfectly.
A C++ file is made of statements. These are sequences of actions you ask C++ to perform while the program is running. You can write such a statement on one line, or you can span more than one line. By default, when you start a new line, the compiler considers that you are starting a new statement. To let the compiler know when a statement ends, you write a semi-colon at the end of the line. For example, to write one statement, you could use: This-Would-Be-A-C++-Statement; To span more than one line, type a semi-colon only when the statement is over. Here is an example: A-Long-Statement-From-C++-That-Spans- More-Than-One-Line;
In computer programming, a comment is text that the compiler will ignore when reading the contents of the source code. For this reason, you can write a comment to your liking. In order for the compiler to skip the section that contains a comment, this section must be delimited appropriately. The C++ language has two types of comment. To write a comment on a line, start the line with //. Everything on the right side of // will not be considered by the compiler. Here are two examples: // Exercise.cpp int main() { // This is the last line of the function return 0; } The // character is used to create a comment on one line. You can use it on different and on as many lines as you want. If you want a comment to cover more than one line, start the section with /* and end it with */. Anything between /* and */ is considered a comment and will not be considered during compilation. Here is an example of such a comment: // Exercise.cpp int main() { /* Anything on this section will not be considered by the compiler. I can write anything I want in it */ return 0; }
Indentation is another feature that makes your program easy to read. Indentation is a technique of grouping lines of code by category. For example, we saw that the body of a function starts with an opening curly bracket “{“ and ends with a closing curly bracket “}”. To delimit the items that are inside of a function, you should indent them by two empty spaces or one tab. By default, Microsoft Visual Studio sets a Tab press to 4 characters. Using indentation, the program could be written: // Exercise.cpp int main() { /* Anything on this section will not be considered by the compiler. I can write anything I want in it */ return 0; } Indentation should be incremental. That is, when a line of code appears to be a child of the previous line, the new line should be indented.
An escape sequence is a character that holds a special meaning to the compiler such as switching to the next line or adding a tab indentation. Although an escape sequence is a character by definition, it is created using the backslash “\” followed by a specific character. For example, the escape sequence \n asks the compiler to add or create a new line, also referred to as Carriage Return or “Carriage Return – Line Feed”. To use an escape sequence by itself, you can include between double quotes: “\n”. Because escape sequences are specially recognized by the compiler, they can be included in a string. Since the compiler will still read a string, if it finds an escape sequence, it acts accordingly. Here is a list of escape sequences and their meanings:
struct house { int protect() { return 0; } }; int main() { return 0; } When a function is created outside of any class, it is called a function. When a function is created inside of a class, it is called a method. For the rest of our lessons, we will use the word "method" to indicate that the function belongs to a class. You can include or create as many methods as you judge necessary in the body of a class. After creating a class, in order to use it, you must first create an "object" from it. This means that you must "declare" the class. To do this, for example in the body of main(), type the name of the class followed by another name that represents the object. Here is an example: struct house { int protect() { return 0; } }; int main() { house home; return 0; } After this declaration, the word home is called an object. After creating the object, such as home above, to access one of its method, type the name of the object, followed by a period, followed by the name of the method, its parentheses, and a semi-colon. Here is an example: struct house { int protect() { return 0; } }; int main() { house home; home.protect(); return 0; } You don't always have to primarily create an object before using a class. To apply this exception, the name of a method that belongs to a class must be preceded with static. Here is an example: struct house { static int protect() { return 0; } }; int main() { return 0; } When a method has been created with the static keyword, it is called a static method. To access it outside of the class, for example to access the above protect from main(), you can type the name of the class, followed by ::, followed by the name of the method, its parentheses, and a semi-colon. Here is an example: struct house { static int protect() { return 0; } }; int main() { house::protect(); return 0; }
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|