Home

Microsoft Foundation Classes Library

 

Fundamentals

 

Introduction to C

C is a computer language that can be used to create applications that control the computer the computer and allow a person with interact wit the machine. To enhance it, another language, named C++, was developed from it, adding the concept of object-oriented programming.

To make it possible to write programs that run on Microsoft Windows operating systems, Microsoft developed a library named Win32. This library is mainly a series objects and their behaviors that define how to create programs that allow a person to interact with a personal computer (PC).

 

The MFC 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.

Microsoft Visual C++

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.

A Compiler

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.

The Microsoft CL Compiler

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.

C++ Fundamentals

 

Introduction

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.

C++ is such a huge language that part of its foundation is provided to you so that you can write your programs by adding to it. A program is made of various objects that you use to build your programs. Some of these objects have already been created and are supplied to you. Although you will not see the whole functionality of these objects, you should be aware of which ones exist and how to use them. These objects are provided in files called Header Files or libraries. By default, the libraries used in Microsoft Visual C++ 2005 are located in the C:\Program Files\Microsoft Visual Studio 8\VC folder. Those used for C++ are installed in the C:\Program Files\Microsoft Visual Studio 8\VC\include. Although you are allowed to open and view these files, make sure you don't touch anything; even if you see a comma that does not make sense, do not correct it.

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"

Creating a Console Application

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:

  • You can click Win32 Console Application, give it a name in the Name text box and possibly click OK. You can then click Finish. In this case, the studio would create a few files for you and write default code in them
  • You can click Win32 Console Application, give it a name in the Name text box and possibly click OK. You can then click Next. In the second page, click the Empty Project check box and click Finish. To create a source file, on the main menu, you can click Project -> Add New Item..., select C++ File, give it a name, and click OK. You would then have to write the necessary code

C++ Instructions

Computer programming is the art of telling the computer what to do, when to do it, and how to do it. The programmer accomplishes this by giving instructions to the computer. An example of an instruction is, "Get a number from the user", or “Display a picture on the screen". The computer carries a lot of such assignments. In C++, an assignment is called a function; it is a task you ask the computer to perform to successfully render an intended result. Like any of the objects you will be using in your programs, a function has a name. The name of a function is a letter or a combination of letters and digits. To differentiate the name of a function from other objects, the name of a function is followed by parentheses.

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.

Introduction to C++ Statements

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;

Start Without Debugging

We mentioned earlier that, after creating the instructions in a text file, you could submit them to the compiler. This is also referred to as compiling or executing the project. To do this simply, on the main menu of Microsoft Visual C++, you can click Debug -> Start Without Debugging. The shortcut is Ctrl + F5. If this is the first time you perform this action or the file had been modified since the last time you did this, you would be asked to save it, which you should do.

When executing the program, it there is no error, it would display its result. If there is at least one error, you would be prompted to correct it.

After viewing the result of a program, to close the DOS window, you can press Enter, and return to MSVC.

Code Comments

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 

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.

Escape Sequences

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:

Escape Meaning
\a Produces a bell sound
\b Backspace
\f Formfeed
\n Newline – Linefeed
\r Carriage return
\t Horizontal Tab
\V Vertical Tab
\\ Backslash
\' Single Quote or Apostrophe
\" Double-Quote
\? Question Mark
 

Introduction to C++ Files

 

Source Files

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 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.

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.

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.

Applications

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.

Introduction to Namespaces

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. Therefore, whenever you need to use a library that is part of the std namespace, instead of typing a library with its file extension, as in iostream.h, you can just type the name of the library as in iostream. Then, on the second line, type using namespace std;. As an example, instead of typing

#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;
}

C Routines

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.

Primary Details of Program Structure

 

Preprocessors: #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 . . .

Preprocessors: #pragma

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