![]() |
Microsoft Foundation Classes Library |
As the Microsoft Windows 3.X and then 5.5 operating system was becoming popular, many programmers were interested in creating graphical programs. They had to use Win32, which made it possible only to use C to create programs. With this approach, everything was done manually, including the design and code writing, which was an increasing demanding task, wasting a good deal of time. Win32 was written in C and had no native support for C++. Therefore, Microsoft created a library, named Microsoft Foundation Classes Library, and abbreviated MFC. This library was originally an “adaptation” or customization of Win32, adding object-orientation (classes and inheritance) to it.
The MFC is a library and can be used to manually develop programs. To make the use of MFC friendlier, Microsoft developed Microsoft Visual C++. This is a graphical programming environment that allows designing Windows objects and writing code to implement their behavior. As its name indicates, this environment takes C++ as its base language. Fortunately, using MFC, it goes over some of the limitations of C++ and takes advantage of MFC’s classes.
C++ is a computer language that uses specific syntaxes and rules to allow a person, called a user, to give instructions to the machine. To give these instructions, you write a mix of easy to identify and relatively easy to use words in a human language such as English. You create these instructions in a computer file and save them with the .cpp extension. There can also be other files with other types of extensions such as .h. When the instructions are ready, you submit them to a program called a compiler that will translate your instructions from English to a machine language. To make it easy to create programs using the C++ language in Microsoft Windows, the company developed Microsoft Visual C++ 2005, which has the compiler “embedded” in it.
CL, the compiler used in Microsoft Visual C++ 2005 is very powerful and practically unique. It “understands” more languages than any other compiler of the Microsoft Visual Studio 2005 family. You can use CL to compile programs written in C and in C++. Yes, these are three different languages. In fact, you can include C and C++ code in the same file and it would compile fine.
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 an assignment, a function is made of a set of instructions. These instructions are listed in a group of lines referred to as the body of the function. The body of a function starts with an opening curly bracket "{" and ends with a closing curly bracket "}"; everything in between is part of the function. Therefore, to use a function in your program, its syntax is: FunctionName() {}
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. When using Microsoft Visual C++, the CL compiler also recognizes this function as _tmain. The main() or _main() function can take various forms. Here is an example: 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() {}
or as: int _tmain() {}
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() or _tmain(), a program can have other functions.
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 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:
The instructions of a C++ program are written one or different files. 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 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.
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. 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 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.
We mentioned that the most fundamental function used in C++ was main() (or _tmain()). The functionality of a program starts with main() (or _tmain()) and ends in main() (or _tmain()) . For this reason, main() (or _tmain()) 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.
A namespace is a technique of grouping some functions and/or classes to create an entity. This has the main goal of eliminating the probability of having two functions or two classes with the same name. For example, if different people work on the same project, each one can create his or her own classes but put them in only his or her namespace. This way, even if they happen to have a class each with the same name, there is no risk of having a name conflict. One of the ways you can use a namespace is with the using keyword. To do this, before accessing the namespace, type using followed by the name of the namespace. Here is an example: namespace Family
{
}
int main()
{
using namespace Family;
return 0;
}
You can also position the using namespace expression
in the top section of the file or in a header file.
The C++ Standard provides a namespace called std. The
std namespace includes a series of libraries that you will routinely and
regularly use in your programs.
#include <iostream.h> You can type: #include <iostream> using namespace std; You can also type this in a header file. Because this second technique is conform with the C++ Standard, we will use it whenever we need one of its libraries. The std namespace provides the means of displaying a value to the screen. In the std namespace, the class that takes care of this is called cout. To use it, type cout, followed by <<, followed by the value, and end it with a semi-colon. Here is an example: #include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++/CLI Programming";
return 0;
}
The C computer language is the parent of C++. To maintain backward compatibility, the operations performed in C were made available to C++. Based on this, you can transparently include C routines in your C++ program. The most common library of the C language available in C++ is called cstdio. Like C++, the C computer language has its own mechanisms to display a value on the screen. One of the functions used to do this is printf_s. To use it, add an opening and a closing parentheses to it. Inside of the parentheses, enter the value you want to display. Here is an example: #include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++/CLI Programming. ";
printf_s("C++/CLI highlights the best of C++ and significantly modernizes it");
return 0;
}
Of course, there are many other details and aspects concerning these built-in namespaces, libraries, and operations. We will review as many as possible in future lessons.
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”
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 . . .
Every compiler in the industry has characteristics that distinguish it from other compilers. When building or compiling a program, you should be sensitive to the environment the program would be running. The #pragma preprocessor 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. |
|
|
||
| Home | Copyright © 2006-2009 FunctionX, Inc. | Next |
|
|
||