Introduction to C++/CLI
Introduction to C++/CLI
Fundamentals
Introduction to C
In the world of computer programming, C has always been considered a powerful language. It was developed by Kernighan and Ritchie and became widely accepted. This was amazing because C was never an easy language. After a while, with the high demands of computer programs, C lacked some features of a cute language. Nevertheless, because of its strengths and its ability to communicate with low-level languages such as Assembly, the C language was highly used to develop widely used operating systems, including Microsoft Windows, Unix and Linux.
Win32: A Library
To make it possible for people to write programmers that run on Microsoft Windows operating systems, Microsoft developed a library named Win32. This library is mainly a series of objects and their behaviors that define how to create programs that allow a person to interact with a personal computer (PC).
Introduction to C++
C++ is a computer language that was developed by Bjarne Stroustrup, from AT & T. Instead of completely reinventing the wheel, he based it on the C language. He added various features such object-oriented programming (classes) and inheritance, just to name a few. Based on the solidity of C++, the language laid a great foundation that many other programming languages imitated.
From its original implementation, C++ lacked a few features that some other languages had. For example, C++ didn’t have a string data type. It had some limits on file streaming. It also had little or no support for web programming, native database development, XML, etc.
Still, because C++ featured many strong qualities, various new languages were based on it, including Java, JavaScript, and lately C#. As you would guess, these new languages completed what C++ lacked. Alternative languages were also developed to perform tasks that were difficult in C++. These languages included Perl, Microsoft Visual Basic, PHP, VBScript, etc.
C++ and many other languages have always benefited from additional libraries. These were used to provide support for areas that probably were not dealt with when the language first came up. In fact, these libraries are what usually makes a language appealing. Some of the libraries that assist C++ are STL and boost. Still, with the high demands of modern assignments, and considering that C++ has not been updated for a while, apparently some of its shortcomings have not evaded.
The Microsoft Foundation Classes Library
As the Microsoft Windows 3.X and then 5.5 operating systems were 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.
Remember that 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. Because Win32 was written in C, it also carried C’s limitations, such as a rough or little support for file streaming, web programming, XML, little support for object serialization, etc. The MFC library was a great addition and overcame many C’s limitations.
Microsoft Visual C++ in the Mix
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.
Microsoft Visual Basic
To use the Win32 library, a programmer had to know either C or C++. To use the MFC, you had to know C++ and have the patience of using it with its pointers and memory management. To alleviate these and other types of challenges to create or develop graphical windows, Microsoft created Microsoft Visual Basic as a relatively easy-to-use language. This programming environment appeared to be friendlier than Visual C++. While Microsoft Visual C++ perfectly “understands” Win32, one of the shortcomings of Visual Basic is that it cannot directly interpret Win32 code. To use Win32, you had to “import” or “load” the necessary function from Win32 into Microsoft Visual Basic.
The Microsoft .NET Framework
Introduction
Some of the computer languages used today have been around for a while and might not have been updated, considering that even 10 years is a long time in the computer world. To still make it possible for programmers to create up-to-date programs, Microsoft was developing its own libraries and programming concepts.
COM to Help
If you create two programs with each in a different language, they cannot communicate. For example, an object created in one language cannot be used in the other language. In the beginning, this was not a big issue but as programs were developed by the thousands and hundreds of thousands, it was not unusual for a company to want to use different languages to create commercial programs. For example, suppose you have a company called Corel, you may want to publish a software product named CorelDraw and another product named WordPerfect. At a later time, you may even acquire another product named Paint Shop Pro, from another company (named Jasc). As you use different programmers with different backgrounds, you may end up with programmers of different languages.
If you want the interfaces of your products to look the same, for example, you may want the menus and toolbars of these products to look the same, instead of asking each department to develop an object that looks the same, you can ask one department A to create the object, such as a toolbar, using the language of its choice, then make that object available to another department B so they can integrate that object in their product.
The problem that may arise is that, in order to use the object developed by department A using one language, the programmers of department B may have to learn the language that those A programmers were using. This can cost time and money to the company.
To make it possible to create an object in one language or programming environment to be used in another language or even programming environment, Microsoft developed Microsoft Component Object Model, also called Microsoft COM, or simply COM. COM itself is three technologies in one family: ActiveX controls, COM+, and DCOM.
The objects used in Microsoft Windows are available through libraries called DLLs and a DLL is primarily a computer file. As these files were developed by the thousands, it was not unusual to have library conflicts from different programs installed in the same computer. The problem with this issue was nicknamed DLL Hell.
Dot NET
Although COM was meant to solve many problems, it brought its own set of issues. For example, COM was neither easy to learn nor easy to implement. Another problem with COM was that is was developed for Microsoft Windows to solve the operating system's issues of the company.
With the fast spread, good availability, and high usefulness of the Internet, Microsoft re-examined the limitations that programming languages like C/C++, Microsoft Visual Studio <= 6 on one end, and the new developments such as web development and XML on the other end. The conclusion was to create a new library, almost from scratch, but by enhancing existing languages. At the same time, Microsoft became more open to the outside world. For example, Microsoft created a new language named C# and submitted its standards for public approval so the language could be used outside of Microsoft Windows.
C++ and the Common Language Infrastructure
Introduction
When the C++ language was created, many of today’s computer and objects (components) requirements were not available and there probably was no need to consider them. As new issues came up, additional libraries were developed to complement the language. Examples of these libraries are the Standard Template Library (STL) and boost. Still either those libraries have their own limitations or they can only follow the original trend set in C++ as a language. Even if these libraries solved many problems, they are not known for their ability to collaborate with other languages.
To address the limits of C++, to allow it to interact with other languages, and to modernize it with new software requirements, Microsoft created a new C++ flavor and named it C++/CLI. To make it possible for this language to be used by other companies and on other operating systems, Microsoft submitted its draft to international organizations for public standardization.
A Compiler |
C++/CLI 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++/CLI language in Microsoft Windows, the company developed Microsoft Visual C++ 2008, which has the compiler “embedded” in it.
The Microsoft CL Compiler |
CL, the compiler used in Microsoft Visual C++ 2008 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, in C++, and in C++/CLI. Yes, these are three different languages. In fact, you can include C, C++, and C++/CLI code in the same file and it would compile fine.
C++/CLI Fundamentals |
Introduction |
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++ 2008.
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.
Fundamentals of C++ Code |
C++/CLI is based on the C++ language but with various improvements for building programs. C++/CLI 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++ 2008 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:
Practical Learning: Introducing Console Applications |
Opening a Project
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.
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() {} 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. 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 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; Start Without Debugging
|