Home

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

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++ 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 CLR Console Application, give it a name in the Name text box and possibly click OK. In this case, the studio would create a source file for you and write default code in it
  • You can click CLR Empty Project, give it a name and click OK. In this case, you must create a source file. To do this, 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
 

Practical LearningPractical Learning: Introducing Console Applications

  1. To start a new program, launch Microsoft Visual C++ 2005
  2. On the main menu, click File -> New -> Project...
  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Console Application
  4. In the Name box, replace the string with RealEstate1
     
    New Project
  5. Click OK
    Don't be concerned with the skeleton code that was written. You will understand all those lines in the next few lessons.
  6. To execute the application, on the main menu, click Debug -> Start Without Debugging
  7. Click Yes

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

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 Classes

A class is a technique of creating one or a group of objects to be considered as an entity. To create a class, you must type class or struct, followed by a name, followed by an opening curly bracket "{", a closing curly bracket "}", and a semi-colon. Here is an example:

struct house { };

One of the types of things you can include in a class is a function. To do this, you write the function exactly as we saw earlier. Here is an example:

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 this ebook, 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;
}

Practical LearningPractical Learning: Introducing Classes

  1. To create a class, change the file as follows:
     
    // RealEstate1.cpp : main project file.
    
    #include "stdafx.h"
    
    using namespace System;
    
    class CProperty
    {
    	int ShowWelcome()
    	{
    		return 0;
    	}
    };
    
    int main(array<System::String ^> ^args)
    {
        Console::WriteLine(L"Hello World");
        return 0;
    }
  2. Save the file
 

 

 

Introduction to C++ Files

 

Source Files

C++ is one of those languages that can use different files for a program. 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.

 

Practical LearningPractical Learning: Creating a Source File

  1. To create a source file, on the main menu, click Project -> Add New Item...
  2. In the Templates list, click Source File (.cpp)
  3. In the New box, type Property
     
  4. 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.

Because C# and Visual Basic don't use header files in the strict sense, some programmers don't encourage you to create them. In this ebook, we will adhere to C++ techniques. Therefore, we will create header files whenever we "feel like it".

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.

Practical LearningPractical Learning: Creating a Header File

  1. To create a header file, on the main menu, click Project -> Add New Item...
  2. In the Templates list, click Header File (.h)
  3. In the New box, type Property
     
  4. Click Add
  5. In the empty file, type the following:
     
    class CProperty
    {
    	int ShowWelcome();
    };
  6. Click the Property.cpp tab and type the following in its file
     
    #include "stdafx.h"
    #include "Property.h"
    
    int CProperty::ShowWelcome()
    {
    	return 0;
    }
  7. Click the RealEstate1.cpp tab and change it to have the following contents:
     
    // RealEstate1.cpp : main project file.
    
    #include "stdafx.h"
    #include "Property.h"
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
        Console::WriteLine(L"Hello World");
        return 0;
    }
  8. To save all files, on the main menu, click File -> Save All.
    Except when creating the files, everything we wrote was for experimental purposes. Don't be concerned with any section you may not understand. We will come back to everything as it becomes necessary

Assemblies

After creating the header and source files, when you execute the project, the compiler will find and "synchronize" them to create an executable. The result that the compiler produces by unifying these and other necessary files of a project is called an assembly. If you are creating projects that you intend to use solely in C++, you usually don't care about this and it is done transparently.

Applications

We mentioned that the most fundamental function used in C++ was main(). The functionality of a program starts with main() and ends in main(). For this reason, main() 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. In this case, the compiler would also create an assembly.

When an assembly has an entry point, which is the case for a regular executable, it is called an application.

The Public and Private Objects of an Assembly

One of the most useful aspects of a programming studio such as Microsoft Visual Studio (or Borland Developer Studio) is that it allows you to create objects, such as classes, using a language of your choice, but that other programming environments or languages, can utilize. The programmers of these languages don't need to know the original language. Based on this, you can create a class in C++ and use that class in Visual Basic, when indeed these are two completely different languages. In order to make this happen, as you may suspect, there are rules you must follow.

If you create a class in an assembly, by default, that class class can be used only by the other files or objects of the same project. Such a class is referred to as private. If you want, in your project, you can create a class that can be used in another project, whether the project is in C++, in C#, or in Visual Basic. Such a class must be made public. The ability to be public or private for a class is controlled using the public and private keywords.

As seen so far, you can create a class as follows:

class house
{
};

As mentioned already, such a class is considered private. If you want this class to be accessible outside of its assembly, you must make it public. To do this, precede its struct or class keyword with the public keyword. Here is an example:

public class house
{
};

If you don't specify public, the class is considered private. Based on this, the notation:

class house
{
};

is the same as:

private class house
{
};

Practical LearningPractical Learning: Controlling the Assembly Access of a Class

  1. To make the CProperty class public, change the Property.h file as follows:
     
    public class CProperty
    {
    	int ShowWelcome();
    };
  2. Save the file

Creating a Class

Microsoft Visual Studio provides a more flexible technique of creating a header file combined with a source file. This allows you to get both files with one click. Both files are created from a class. To create the class, on the main menu, you can click Project -> Add Class... In the Templates list of the Add Class dialog box, click C++ Class and click Add. Enter a name if the Class Name text box. By a tradition dating from the previous versions of Microsoft Visual C++, you should start the name of the class with C. If you start the name of the class with C, a header file and a source file without the C would be suggested, which you should accept, and click Finish. To keep this Visual C++ tradition, we will start most names of our classes with C.

Practical LearningPractical Learning: Creating a Class

  1. On the main menu, click Project -> Add Class...
  2. In the left frame, expand the Visual C++ node and click C++
  3. To create a class, in the Templates list, click C++ Class
     
  4. Click Add
  5. In the Class Name box, type CRealEstateAgent
  6. Click Finish.
    Once again, don't mind the skeleton code that was written. The purpose of this exercise was to show you how to create a class

The Code Editor

 

Description

Code of an application is written in plain English and readable to human eyes. For a C++ application, you can use any text editor to write your code but one of Visual Studio's main strengths is the code editor that it has always brought. It is very intuitive.

The Code Editor is a window specially designed for code writing.

Author Note Although all languages of the Visual Studio .NET programming environment share the Code Editor, once you have started a type of application, the Code Editor is adapted to the language you are using. Its parser (a program used internally to analyze your code) behaves according to the language you are using. The features and behaviors of the Code Editor are also different, depending on the language.

To display the code editor, if you created an empty project, you can click Project on the main menu and select one of the options that would lead either to a blank text file or a file that contains primary code.

The Code Editor of Visual C++ is divided in 4 sections:

The Code Editor
 

The Tabs Bar

The top section of the Code Editor displays tabs of property pages. Each tab represents a file. To add a new file to the project, on the main menu, you can click

  • File -> Add New Item...
  • Project -> Add New Item...

Once in the Add New Item dialog box, in the Templates section, click the type of file you want to create, type a name in the Name text box, and press Enter. After the file has been created, it is represented by a tab in the top section of the Code Editor. In the same way, you can add as many files as you judge them necessary. Because there can be different types of files, each has an extension that allows you to know the type of file the tab is holding.

To access one of theses files, you can click its corresponding tab. By default, the tabs display in the order their files were created or added to the project, from left to right. If you don't like that arrangement, click and drag its tab either left or right beyond the next tab.

The Scopes Combo Box

The top-left section of the Code Editor displays a combo box. If you position the mouse on it, a tool tip that starts with Scopes in would appear. The Scope combo box holds a list of the types as classes and structures that are used in the current project. You can display the list if you click the arrow of the combo box. Each item of the Scope combo box displays the name of its type associated with its parent as implemented in the code. 

The Functions Combo Box

The top-right section of the Code Editor displays a combo box named Members. If you position the mouse on it, a tool tip that starts with Functions would appear. The Functions combo box holds a list of the members of a class. The content of the Functions combo box depends on the item that is currently selected in the Scopes combo box. This means that, before accessing the members of a particular class, you must first select that class in the Scopes combo box. Then, when you click the arrow of the Functions combo box, the members of only that class display.

If you select an item from the Functions combo box, the Code Editor jumps to that member in the header file and positions the cursor to the left of the member.

Code Colors

Code is written in a wide area with a white background. This is the area you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text:

The colors used are highly customizable. To change the colors, on the main menu, you can click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one:

In both cases, the combo boxes display a fixed list of colors. If you want more colors, you can click a Custom button to display the Color dialog box that allows you to "create" a color.

The Solution Explorer

 

Introduction

The Solution Explorer is a window that displays the file names and other items used in your project. The items of this window display in a tree. To expand a node, you can click its + button. To collapse it, click its - button. To explore an item, you can double-click it. The result depends on the item you double-clicked.

 

Using the Solution Explorer

The Solution Explorer can be used to create and add a new class, a new resource, or a reference to the current project. It can also be used to add a another project to the current project. To perform any of these operations, you can right-click a folder node such as the name of the project, the Source Files, the Header Files, or the Resource Files nodes, position the mouse on Add and select the desired operation:

Remember that you can also perform any of these operations from the Project category of the main menu.

Besides adding new items to the project, you can also use the Solution Explorer to build the project or change its properties. If you add one or more other project(s) to the current one, one of the project must be set as the default. That project would be the first to come up when the user executes the application. By default, the first project created is set as the default. If you have more than one project, to set the default, right-click the name of the desired project in Solution Explorer and click Set As StartUp Project.

The Solution Explorer also you to rename or delete some of the items that belong to your project.

 

The Class View

 

Introduction

The Class View displays the various classes used by your project, including there ancestry. The items of the Class View an organized as a tree list with the name of the project on top.


Class View
 

Using the Class View

The Class View shares some of its functionality with the Solution Explorer. This means that you can use it to build a project, to add new class or a new resource. You can also use it to change the default project if you have more than one.

While the Solution Explorer displays the items that are currently being used by your project, the Class View allows you to explorer the classes used in your applications, including their dependencies. For example, some times you will be using a control of the of the .NET Framework and you may wonder from what class that control is derived. The Class View, rather than the Solution Explorer, can quickly provide this information. To find it out, expand the class by clicking its + button.

 

 
 
   

Home Copyright © 2006-2016, FunctionX, Inc. Next