introduction to .net and visual c++ - deitel & associates · chapter 1 introduction to .net and...

24
1 Introduction to .NET and Visual C++ ® .NET Objectives To learn the history of the Internet and the World Wide Web. To become familiar with the World Wide Web Consortium (W3C). To learn what the Extensible Markup Language (XML) is and why it is an important technology. To understand the impact of object technology on software development. To understand the Microsoft ® .NET initiative. To introduce Managed Extensions for C++. To preview the remaining chapters of the book. Things are always at their best in their beginning. Blaise Pascal High thoughts must have high language. Aristophanes Our life is frittered away by detail…Simplify, simplify. Henry David Thoreau Before beginning, plan carefully…. Marcus Tullius Cicero Look with favor upon a bold beginning. Virgil I think I’m beginning to learn something about it. Auguste Renoir

Upload: dangliem

Post on 06-May-2018

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

1Introduction to .NET and

Visual C++® .NET

Objectives• To learn the history of the Internet and the World

Wide Web.• To become familiar with the World Wide Web

Consortium (W3C).• To learn what the Extensible Markup Language

(XML) is and why it is an important technology.• To understand the impact of object technology on

software development.• To understand the Microsoft® .NET initiative.• To introduce Managed Extensions for C++.• To preview the remaining chapters of the book.Things are always at their best in their beginning.Blaise Pascal

High thoughts must have high language.Aristophanes

Our life is frittered away by detail…Simplify, simplify.Henry David Thoreau

Before beginning, plan carefully….Marcus Tullius Cicero

Look with favor upon a bold beginning.Virgil

I think I’m beginning to learn something about it.Auguste Renoir

vcppfep_01.fm Page 1 Tuesday, December 3, 2002 2:44 PM

Page 2: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

2 Introduction to .NET and Visual C++® .NET Chapter 1

1.1 IntroductionWelcome to Visual C++ .NET! We have worked hard to provide programmers with the most accurate and complete information regarding Visual C++ .NET and the .NET plat-form. We hope that this book will provide an informative, entertaining and challenging learning experience for you. In this chapter, we present the history of the Internet and World Wide Web and introduce Microsoft’s .NET initiative. The chapter concludes by touring the remainder of the book.

1.2 History of the Internet and World Wide WebIn the late 1960s, at a conference at the University of Illinois Urbana-Champaign, ARPA—the Advanced Research Projects Agency of the Department of Defense—rolled out the blueprints for networking the main computer systems of approximately a dozen ARPA-funded universities and research institutions. The computers were to be connected with communications lines operating at a then-stunning 56 Kbps (1 Kbps is equal to 1,024 bits per second), at a time when most people (of the few who had access to networking technol-ogies) were connecting over telephone lines to computers at a rate of 110 bits per second. Researchers at Harvard talked about communicating with the Univac 1108 “supercomput-er,” which was located across the country at the University of Utah, to handle calculations related to their computer graphics research. Many other intriguing possibilities were dis-cussed. Academic research was about to take a giant leap forward. Shortly after this con-ference, ARPA proceeded to implement what quickly became called the ARPAnet, the grandparent of today’s Internet.

Things worked out differently from the original plan. Although the ARPAnet did enable researchers to network their computers, its chief benefit proved to be its capability for quick and easy communication via what came to be known as electronic mail (e-mail). This is true even on today’s Internet, with e-mail, instant messaging and file transfer facil-itating communications among hundreds of millions of people worldwide.

Outline

1.1 Introduction1.2 History of the Internet and World Wide Web1.3 World Wide Web Consortium (W3C)1.4 Extensible Markup Language (XML)1.5 Key Software Trend: Object Technology1.6 Introduction to Microsoft .NET1.7 Visual C++ .NET1.8 .NET Framework and the Common Language Runtime1.9 Tour of the Book1.10 Summary1.11 Internet and World Wide Web Resources

vcppfep_01.fm Page 2 Tuesday, December 3, 2002 2:44 PM

Page 3: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 3

The network was designed to operate without centralized control. This meant that if a portion of the network should fail, the remaining working portions would still be able to route data packets from senders to receivers over alternative paths.

The protocol (i.e., set of rules) for communicating over the ARPAnet became known as the Transmission Control Protocol (TCP). TCP ensured that messages were routed prop-erly from sender to receiver and that those messages arrived intact.

In parallel with the early evolution of the Internet, organizations worldwide were implementing their own networks to facilitate both intra-organization (i.e., within the orga-nization) and inter-organization (i.e., between organizations) communication. A huge variety of networking hardware and software appeared. One challenge was to enable these diverse products to communicate with each other. ARPA accomplished this by developing the Internet Protocol (IP), which created a true “network of networks,” the current archi-tecture of the Internet. The combined set of protocols is now commonly called TCP/IP.

Initially, the use of the Internet was limited to universities and research institutions; later, the military adopted the technology. Eventually, the government decided to allow access to the Internet for commercial purposes. When this decision was made, there was resentment among the research and military communities—it was felt that response times would become poor as “the Net” became saturated with so many users.

In fact, the opposite has occurred. Businesses rapidly realized that, by making effective use of the Internet, they could refine their operations and offer new and better services to their clients. Companies started spending vast amounts of money to develop and enhance their Internet presence. This generated fierce competition among communications carriers and hardware and software suppliers to meet the increased infrastructure demand. The result is that bandwidth (i.e., the information-carrying capacity of communications lines) on the Internet has increased tremendously, while hardware costs have plummeted. The Internet has played a significant role in the economic growth that many industrialized nations experienced over the last decade.

The World Wide Web allows computer users to locate and view multimedia-based doc-uments (i.e., documents with text, graphics, animations, audios or videos) on almost any subject. Even though the Internet was developed more than three decades ago, the introduc-tion of the World Wide Web (WWW) was a relatively recent event. In 1989, Tim Berners-Lee of CERN (the European Organization for Nuclear Research) began to develop a tech-nology for sharing information via hyperlinked text documents. Basing the new language on the well-established Standard Generalized Markup Language (SGML)—a standard for business data interchange—Berners-Lee called his invention the HyperText Markup Lan-guage (HTML). He also wrote communication protocols to form the backbone of his new hypertext information system, which he referred to as the World Wide Web.

Surely, historians will list the Internet and the World Wide Web among the most important and profound creations of humankind. In the past, most computer applications ran on “stand-alone” computers (computers that were not connected to one another). Today’s applications can be written to communicate among the world’s hundreds of mil-lions of computers. The Internet and World Wide Web merge computing and communica-tions technologies, expediting and simplifying our work. They make information instantly and conveniently accessible to large numbers of people. They enable individuals and small businesses to achieve worldwide exposure. They are changing the way we do business and conduct our personal lives.

vcppfep_01.fm Page 3 Tuesday, December 3, 2002 2:44 PM

Page 4: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

4 Introduction to .NET and Visual C++® .NET Chapter 1

1.3 World Wide Web Consortium (W3C)In October 1994, Tim Berners-Lee founded an organization, called the World Wide Web Consortium (W3C), that is devoted to developing nonproprietary, interoperable technolo-gies for the World Wide Web. One of the W3C’s primary goals is to make the Web univer-sally accessible—regardless of its users’ disabilities, languages or cultures.

The W3C is also a standardization organization and is composed of three hosts—the Massachusetts Institute of Technology (MIT), France’s INRIA (Institut National de Recherche en Informatique et Automatique) and Keio University of Japan—and over 400 members, including Deitel & Associates, Inc. Members provide the primary financing for the W3C and help set the strategic direction of the Consortium. To learn more about the W3C, visit www.w3.org.

Web technologies standardized by the W3C are called Recommendations. Current W3C Recommendations include Extensible HyperText Markup Language (XHTML™) for marking up content for the Web (discussed in Section 1.4), Cascading Style Sheets (CSS™)for describing how content is formatted and the Extensible Markup Language (XML) for cre-ating markup languages. Recommendations are not actual software products, but documents that specify the role, syntax and rules of a technology. Before becoming a W3C Recommen-dation, a document passes through three major phases: Working Draft, which, as its name implies, specifies an evolving draft; Candidate Recommendation, a stable version of the document that industry can begin to implement; and Proposed Recommendation, a Candi-date Recommendation that is considered mature (i.e., has been implemented and tested over a period of time) and is ready to be considered for W3C Recommendation status. For detailed information about the W3C Recommendation track, see “6.2 The W3C Recommen-dation track” at

www.w3.org/Consortium/Process/Process-19991111/ process.html#RecsCR

1.4 Extensible Markup Language (XML)As the popularity of the Web exploded in the 1990s, HTML’s limitations became apparent. Although HTML was created as a common format for the Web, HTML’s lack of extensi-bility (the ability to change or add features) frustrated developers, and the lack of correctly structured documents allowed erroneous HTML to proliferate. Browser vendors attempting to gain market share created platform-specific tags. This forced Web developers to support multiple browsers, which significantly complicated Web development. To address these and other problems, the W3C developed XML.

XML combines the power and extensibility of its parent language, SGML, with sim-plicity. XML is a meta-language—a language used as a basis for other languages—that offers a high level of extensibility. Using XML, the W3C created the Extensible HyperText Markup Language (XHTML), an XML vocabulary (i.e., an XML-based markup language that is developed for a specific industry or purpose) that provides a common, extensible format for the Web. XHTML is expected to replace HTML. The W3C also developed the Extensible Stylesheet Language (XSL), which is composed of several technologies, to manipulate data in XML documents for presentation purposes. XSL provides developers the flexibility to transform data from an XML document into other types of documents—for example, Web pages or reports. In addition to serving as the basis for other markup lan-

vcppfep_01.fm Page 4 Tuesday, December 3, 2002 2:44 PM

Page 5: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 5

guages, developers use XML for data interchange and e-commerce systems. At the time of this writing, there were more than 450 XML standards.

Unlike many technologies, which begin as proprietary solutions and become stan-dards, XML was defined as an open, standard technology. XML’s development has been supervised by the W3C’s XML Working Group, which prepared the XML specification and approved it for publication. In 1998, the XML version 1.0 specification (www.w3.org/TR/REC-xml) was accepted as a W3C Recommendation. This means that the technology is stable for wide deployment in industry.

The W3C continues to oversee the development of XML, as well as Simple Object Access Protocol (SOAP), a technology for the distribution of data (marked up as XML) over the Internet. Developed initially by Microsoft and DevelopMentor, SOAP is a W3C Working Draft that provides a framework for expressing application semantics and encoding and packaging data. Microsoft .NET (discussed in Sections 1.6 and 1.7) uses XML and SOAP to mark up and transfer data over the Internet. XML and SOAP are at the core of .NET—they allow software components to interoperate (i.e., communicate easily with one another). SOAP is supported by many platforms, because of its foundations in XML and HTTP. We discuss XML in Chapter 15, Extensible Markup Language (XML), and SOAP in Chapter 17, Web Services.

1.5 Key Software Trend: Object TechnologyWhat are objects, and why are they special? Object technology is a packaging scheme that facilitates the creation of meaningful software units. These units are large and focused on particular application areas. There are date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects and so on. In fact, almost any noun can be represented as a software object. Objects have properties (i.e., attributes, such as color, size and weight) and perform actions (i.e., behaviors, such as moving, sleep-ing and drawing). Classes represent groups of related objects. For example, all cars belong to the “car” class, even though individual cars vary in make, model, color and options pack-ages. A class specifies the general format of its objects; the properties and actions available to an object depend on its class.

We live in a world of objects. Just look around you—there are cars, planes, people, ani-mals, buildings, traffic lights, elevators and so on. Before object-oriented languages appeared, procedural programming languages (such as Fortran, Pascal, BASIC and C) focused on actions (verbs) rather than things or objects (nouns). We live in a world of objects, but earlier programming languages forced individuals to program primarily with verbs. This paradigm shift made writing programs a bit awkward. However, with the advent of popular object-oriented languages, such as C++, Java™ and C#, programmers can program in an object-oriented manner that reflects the way in which they perceive the world. This process, which seems more natural than procedural programming, has resulted in significant productivity gains.

One of the key problems with procedural programming is that the program units cre-ated do not mirror real-world entities effectively and therefore are difficult to reuse. Pro-grammers often write and rewrite similar software for various projects. This wastes precious time and money as programmers repeatedly “reinvent the wheel.” With object technology, properly designed software entities (called objects) can be reused on future projects. Using libraries of reusable componentry can reduce the amount of effort required

vcppfep_01.fm Page 5 Tuesday, December 3, 2002 2:44 PM

Page 6: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

6 Introduction to .NET and Visual C++® .NET Chapter 1

to implement certain kinds of systems (compared with the effort that would be required to reinvent these capabilities in new projects). Visual C++ .NET programmers use the .NET Framework Class Library (known commonly as the FCL), which is introduced in Section 1.8.

Some organizations report that software reusability is not, in fact, the key benefit of object-oriented programming. Rather, they indicate that object-oriented programming tends to produce software that is more understandable because it is better organized and has fewer maintenance requirements. As much as 80 percent of software costs are not associ-ated with the original efforts to develop the software, but instead are related to the con-tinued evolution and maintenance of that software throughout its lifetime. Object orientation allows programmers to abstract the details of software and focus on the “big pic-ture.” Rather than worrying about minute details, the programmer can focus on the behav-iors and interactions of objects. A roadmap that showed every tree, house and driveway would be difficult, if not impossible, to read. When such details are removed and only the essential information (roads) remains, the map becomes easier to understand. In the same way, a program that is divided into objects is easy to understand, modify and update because it hides much of the detail. It is clear that object-oriented programming will be the key programming methodology for at least the next decade.

Software Engineering Observation 1.1Use a building-block approach to create programs. By using existing pieces in new projects, programmers avoid reinventing the wheel. This is called software reuse, and it is central to object-oriented programming. 1.1

[Note: We will include many of these Software Engineering Observations throughout the book to explain concepts that affect and improve the overall architecture and quality of a software system and, particularly, of large software systems. We also will highlight Good Programming Practices (practices that can help programmers write programs that are clearer, more understandable, more maintainable and easier to test and debug), Common Programming Errors (problems we highlight to ensure that programmers avoid the most common errors), Performance Tips (techniques that will help programmers write programs that run faster and use less memory), Portability Tips (techniques that will help program-mers write programs that can run, with little or no modification, on a variety of computers), Testing and Debugging Tips (techniques that will help programmers remove bugs from their programs and, more importantly, write bug-free programs in the first place) and Look-and-Feel Observations (techniques that will help programmers design the “look and feel” of their graphical user interfaces for appearance and ease of use). Many of these techniques and practices are only guidelines; you will, no doubt, develop your own preferred program-ming style.]

The advantage of writing your own code is that you will know exactly how it works. The code will be yours to examine, modify and improve. The disadvantage is the time and effort that goes into designing, developing and testing new code.

Performance Tip 1.1Reusing proven code components instead of writing your own versions can improve program performance, because these components normally are written to perform efficiently. 1.1

vcppfep_01.fm Page 6 Tuesday, December 3, 2002 2:44 PM

Page 7: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 7

Software Engineering Observation 1.2Extensive class libraries of reusable software components are available over the Internet and the World Wide Web; many are offered free of charge. 1.2

1.6 Introduction to Microsoft .NETIn June 2000, Microsoft announced its .NET (pronounced “dot-net”) initiative. The .NET platform is one that provides significant enhancements to earlier developer platforms. .NET offers a new software-development model that allows applications created in disparate pro-gramming languages to communicate with each other. The platform also allows developers to create Web-based applications that can be distributed to a great variety of devices (even wireless phones) and to desktop computers.

Microsoft’s .NET initiative is a broad new vision for embracing the Internet and the Web in the development, engineering and use of software. One key aspect of the .NET strategy is its independence from a specific language or platform. Rather than requiring programmers to use a single programming language, developers can create a .NET appli-cation by using any combination of .NET-compatible languages (Fig. 1.1). Programmers can contribute to the same software project, writing code in the .NET languages (such as Visual C++ .NET, C#, Visual Basic® .NET and many others) in which they are most pro-ficient.

A key component of the .NET architecture is Web services, which are applications that expose (i.e., make available) functionality to clients via the Internet. Clients and other appli-cations can use these Web services as reusable building blocks. One example of a Web ser-vice is Dollar Rent a Car’s reservation system, known as Quick Keys.1 Dollar wanted to expose the functionality of its mainframe-based system, so that other companies could pro-vide customers with the ability to make rental-car reservations. Dollar could have created

1. Microsoft Corporation, “Dollar Rent A Car Breathes New Life Into Legacy Systems Using .NET Connected Software,” 15 March 2002 <www.microsoft.com/business/casestudies/b2c/dollarrentacar.asp>.

Programming Languages

APL Oberon

C# Oz

COBOL Pascal

Component Pascal Perl

Curriculum Python

Eiffel RPG

Fortran Scheme

Haskell Smalltalk

J# Standard ML

Fig. 1.1 .NET Languages (tabular information from Microsoft Web site, www.microsoft.com.). (Part 1 of 2.)

vcppfep_01.fm Page 7 Tuesday, December 3, 2002 2:44 PM

Page 8: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

8 Introduction to .NET and Visual C++® .NET Chapter 1

individual, proprietary solutions for its business partners. To expose its functionality in a reus-able way, Dollar implemented its solution using Web services. Through the newly created Web service, airlines and hotels can use Dollar’s reservation system to reserve cars for their clients. Dollar’s business partners do not need to use the same platform as Dollar uses, nor do they need to understand how the reservation system is implemented. Reimplementing its application as a Web service has provided Dollar with millions of dollars of additional rev-enue, as well as thousands of new customers.

Web services extend the concept of software reuse by allowing programmers to con-centrate on their specialties without having to implement every component of every application. Instead, companies can buy Web services and devote their time and energy to developing their products. Object-oriented programming has become popular, because it enables programmers to create applications easily, using prepackaged components. Similarly, programmers may create an application using Web services for databases, security, authentication, data storage and language translation without having to know the internal details of those components.

When companies link their products via Web services, a new user experience emerges. For example, a single application could manage bill payments, tax refunds, loans and investments, using Web services from various companies. An online merchant could buy Web services for online credit-card payments, user authentication, network security and inventory databases to create an e-commerce Web site.

The keys to this interaction are XML and SOAP, which enable Web services to com-municate. XML gives meaning to data, and SOAP is the protocol that allows Web services to communicate easily with one another. XML and SOAP act as the “glue” that combines various Web services to form applications.

Universal data access is another essential .NET concept. If two copies of a file exist (e.g., on a personal and a company computer), the oldest version must be updated con-stantly—this is called file synchronization. If the files are different, they are unsynchro-nized, a situation that could lead to errors. With .NET, data can reside in one central location rather than on separate systems. Any Internet-connected device can access the data (under tight control, of course), which would then be formatted appropriately for use or dis-play on the accessing device. Thus, the same document could be seen and edited on a desktop PC, a PDA, a wireless phone or some other device. Users would not need to syn-chronize the information, because it would be fully up-to-date in a central location.

.NET is an immense undertaking. We discuss various aspects of .NET throughout this book. Additional information is available at www.microsoft.com/net.

JScript .NET Visual Basic .NET

Mercury Visual C++ .NET

Programming Languages

Fig. 1.1 .NET Languages (tabular information from Microsoft Web site, www.microsoft.com.). (Part 2 of 2.)

vcppfep_01.fm Page 8 Tuesday, December 3, 2002 2:44 PM

Page 9: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 9

1.7 Visual C++ .NETStandard C++ evolved from C, which was developed from two previous programming lan-guages, BCPL and B. BCPL was created in 1967 by Martin Richards as a language for writ-ing operating-systems software and compilers. Ken Thompson modeled many features in his language B after their counterparts in BCPL and used B to create early versions of the UNIX operating system at Bell Laboratories in 1970 on a DEC PDP-7 computer. Both BCPL and B were “typeless” languages—every data item occupied one “word” in memory and the burden of treating a data item as a whole number or a real number, for example, was the responsibility of the programmer.

The C language was developed by Dennis Ritchie at Bell Laboratories and was origi-nally implemented on a DEC PDP-11 computer in 1972. C uses many important concepts of BCPL and B while adding data typing and other features. By the late 1970s, C had evolved into what now is referred to as “traditional C,” “classic C,” or “Kernighan and Ritchie C.” The publication by Prentice-Hall in 1978 of Kernighan and Ritchie’s book, The C Programming Language, brought attention to the language.

C initially became widely known as the development language of the UNIX operating system. The widespread use of C with various types of computers (sometimes called hard-ware platforms) led to many variations of the language that were similar, but often incom-patible. This was a serious problem for program developers who needed to write portable programs to run on disparate platforms. It became clear that a standard version of C was needed. In 1983, the X3J11 technical committee was created under the American National Standards Committee on Computers and Information Processing (X3) to “provide an unambiguous and machine-independent definition of the language.” In 1989, the standard was approved. ANSI cooperated with the International Organization for Standardization (ISO) to standardize C; the joint standard document was published in 1990 and is referred to as ANSI/ISO 9899: 1990. Copies of this document can be ordered from ANSI. The second edition of Kernighan and Ritchie, published in 1988, reflects this version called ANSI C, a version of the language now used worldwide.

Portability Tip 1.1Because C is a standardized, hardware-independent, widely available language, applica-tions written in C often can be run with little or no modifications on a wide range of different computer systems. 1.1

C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories. C++ provides a number of features that “spruce up” the C language, but more importantly, it provides capabilities for object-oriented programming.

Building software quickly, correctly and economically remains an elusive goal, and this at a time when the demand for new and more powerful software is soaring. Objects are essentially reusable software components that model items in the real world. Software developers are discovering that using a modular, object-oriented design and implementa-tion approach can make software development groups much more productive than is pos-sible with previous popular programming techniques, such as structured programming. Object-oriented programs are easier to understand, correct and modify.

Developed in the early 1990s, Visual C++ is a Microsoft implementation of C++ that includes Microsoft’s proprietary extensions to the language. Over the past decade, Microsoft has released several versions of Visual C++—most recently, Visual C++ .NET.

vcppfep_01.fm Page 9 Tuesday, December 3, 2002 2:44 PM

Page 10: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

10 Introduction to .NET and Visual C++® .NET Chapter 1

Visual C++ .NET is known as a visual programming language—the developer uses graph-ical tools (via Visual Studio .NET) to create applications. We discuss Visual Studio .NET later in this section.

Early graphics and graphical user interface (GUI) programming (i.e., the program-ming of an application’s visual interface) with Visual C++ was implemented using the Microsoft Foundation Classes (MFC). The Microsoft Foundation Classes library is a col-lection of classes that help Visual C++ programmers create powerful Windows-based applications. Now, with the introduction of .NET, Microsoft provides an additional library (.NET’s FCL, discussed in Section 1.8) for implementing GUI, graphics, networking, mul-tithreading and other capabilities. This library is available to .NET-compliant languages such as Visual C++ .NET, Visual Basic .NET and Microsoft’s new language, C#. How-ever, developers still can use MFC—Microsoft has, in fact, upgraded MFC to MFC 7, which includes new classes and documentation that can be accessed using Visual Studio .NET. MFC often is used to develop unmanaged code, or code that does not make use of the .NET Framework.2 We discuss unmanaged code in more detail shortly.

The .NET platform enables Web-based applications to be distributed to a variety of devices, including desktop computers and cell phones. The platform offers a new software-development model that allows applications created in disparate programming languages to communicate with each other. Visual C++ .NET was designed specifically for the .NET platform so Visual C++ programmers could migrate easily to .NET. However, Microsoft also designed Visual C++ .NET to be backward compatible with its previous version, 6.0, and has continued to emphasize compliance with the ANSI/ISO standard for C++.

Visual C++ .NET introduces Managed Extensions for C++ (MC++) that enable a pro-grammer to access the .NET Framework. Programmers can use the .NET Framework to create objects that provide automatic garbage collection, memory management and lan-guage interoperability (with other .NET languages). Such objects are known as managed objects; the code that defines these objects is known as managed code. With managed objects, memory leaks are extremely rare. [Note: The different implementations of C++ and Visual C++ have yielded several industry terms. For the remainder of this book, we use the term “C++” when referring to standard C++. We use the term “Managed Extensions for C++ (MC++)” when referring to features of managed C++ or code that uses these features. Finally, we use the term “Visual C++ .NET” when referring to the Microsoft product/com-piler, which can be used to write both managed and unmanaged code.]

MC++ provides the programmer access to new data types provided by the .NET Framework. These new data types aid in standardization across different platforms and .NET programming languages. In MC++, standard C++ data types are mapped to these new types. [Note: We discuss .NET data types in more detail in Chapter 2, Visual Studio® .NET IDE and Visual C++ .NET Programming.]

The true power of Visual C++ .NET lies in its interoperability between managed and unmanaged code. All legacy Visual C++ code is unmanaged. Rather than forcing devel-opers to discard large amounts of legacy code, Visual C++ .NET enables programmers to mix managed (MC++) and unmanaged (standard C++) code. We discuss mixing managed and unmanaged code in detail in Chapter 24, Managed and Unmanaged Interoperability.

2. Readers can learn more about what is new in MFC 7 at msdn.microsoft.com/library/default.asp?url=/library/en-us/vcedit/html/vcrefwhatsnewlibrariesvisualc70.asp.

vcppfep_01.fm Page 10 Tuesday, December 3, 2002 2:44 PM

Page 11: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 11

In addition to providing interoperability between managed and unmanaged code, Microsoft has upgraded several of the technologies used in unmanaged code. One example of this is the Active Template Library (ATL), a set of template classes that simplify the cre-ation of COM components.3 The newest version of ATL, ATL 7, contains several enhance-ments. An example of one enhancement is the ATL Server, which is a set of classes designed to simplify Web development while providing the ability to call unmanaged C++ from within a Web page. Attributes are a new feature that allow a programmer to inject pre-compiled code into an application. An attribute can be used to modify different portions of an application. We discuss attributed programming in Chapter 22 and ATL Server in Chapter 23.

Although Visual C++ .NET is powerful, it lacks some tools, such as a Windows Form designer, a visual programming tool that simplifies GUI and database programming. With the Windows Form Designer, Visual Studio .NET generates program code from various programmer actions (such as using the mouse for pointing, clicking, dragging and drop-ping). Because Visual C++ .NET does not yet provide the programmer with a Form designer, programmers must manually generate code for their graphical user interfaces.4

In Visual C++ .NET, programs are created using Visual Studio .NET, an Integrated Development Environment (IDE). With the IDE, a programmer can create, run, test and debug programs conveniently, thereby reducing the time it takes to produce a working pro-gram to a fraction of the time it would have taken without using the IDE. The process of rapidly creating an application using an IDE typically is referred to as Rapid Application Development (RAD).

Visual C++ .NET enables a new degree of language interoperability: Software compo-nents from different languages can interact as never before. Developers can package even old software to work with new Visual C++ .NET programs. In addition, Visual C++ .NET applications can interact via the Internet, using industry standards such as the Simple Object Access Protocol (SOAP) and XML, which we discuss in Chapter 15, Extensible Markup Language (XML) and Chapter 17, Web Services. The programming advances embodied in .NET and Visual C++ .NET will lead to a new style of programming, in which applications are created from building blocks available over the Internet.

1.8 .NET Framework and the Common Language RuntimeThe .NET Framework is at the heart of .NET. This framework manages and executes ap-plications, contains a class library (called the Framework Class Library, or FCL), enforces security and provides many other programming capabilities. The details of the .NET Framework are found in the Common Language Specification (CLS), which contains infor-mation about the storage of objects and other information. The CLS has been submitted for standardization to ECMA (the European Computer Manufacturers Association). This al-lows independent software vendors to create the .NET Framework for other platforms. The .NET Framework exists only for the Windows platform, but is being developed for other

3. COM (the Component Object Model) is a specification that provides a platform-, language- and location-independent environment to develop reusable, binary object-oriented software compo-nents.

4. For convenience, we used C# to generate all of the GUI code for this book. We then converted the autogenerated C# code to MC++. Normally, however, programmers should not modify autogener-ated code.

vcppfep_01.fm Page 11 Tuesday, December 3, 2002 2:44 PM

Page 12: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

12 Introduction to .NET and Visual C++® .NET Chapter 1

platforms, as well, such as Microsoft’s Shared Source CLI (Common Language Infrastruc-ture). The Shared Source CLI is an archive of source code that provides a subset of the Microsoft .NET Framework for both Windows XP and the FreeBSD5 operating systems.6

For more information in the Shared Source CLI, visit msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/ mssharsourcecli.asp.

The Common Language Runtime (CLR) is another central part of the .NET Frame-work—it executes managed code. Programs are compiled into machine-specific instruc-tions in two steps. First, the program is compiled into Microsoft Intermediate Language (MSIL), which defines instructions for the CLR. Code converted into MSIL from other lan-guages and sources is woven together by the CLR. Then, another compiler in the CLR com-piles the MSIL into machine code (for a particular platform), creating a single application.

Why bother having the extra step of converting from Visual C++ .NET to MSIL, instead of compiling directly into machine language? The key reasons are portability between operating systems, interoperability between languages and execution-manage-ment features such as memory management and security.

If the .NET Framework exists (and is installed) for a platform, that platform can run any .NET program. The ability of a program to run (without modification) across multiple platforms is known as platform independence. Code written once can be used on another machine without modification, saving both time and money. In addition, software can target a wider audience—previously, companies had to decide whether converting (some-times called porting) their programs to different platforms was worth the cost. With .NET, porting is simplified.

The .NET Framework also provides a high level of language interoperability. Pro-grams written in different languages are all compiled into MSIL—the different parts can be combined to create a single, unified program. MSIL allows the .NET Framework to be lan-guage independent, because MSIL is not tied to a particular programming language. Any language that can be compiled into MSIL is called a .NET-compliant language.

Language interoperability offers many benefits to software companies. Visual C++ .NET, Visual Basic .NET and C# developers, for example, can work side-by-side on the same project without having to learn another programming language—all their code is compiled into MSIL and linked together to form one program. In addition, the .NET Frame-work can package preexisting components (i.e., components created using tools that pre-date .NET) and .NET components to work together. This allows companies to reuse the code that they have spent years developing and integrate it with the .NET code that they write. Integration is crucial, because companies cannot migrate easily to .NET unless they can stay productive, using their existing developers and software.

Another benefit of the .NET Framework is the CLR’s execution-management features. The CLR manages memory, security and other features, relieving the programmer of these responsibilities. With languages like C++, programmers must manage their own memory. This leads to problems if programmers request memory and never release it—programs

5. The FreeBSD project provides a freely available and open-source UNIX-like operating system that is based on UC Berkeley’s Berkeley System Distribution (BSD). For more information on BSD, visit www.freebsd.org.

6. Microsoft Corporation, “The Microsoft Shared Source CLI Implementation,” March 2002, <msdn.microsoft.com/library/en-us/Dndotnet/html/ mssharsourcecli.asp>.

vcppfep_01.fm Page 12 Tuesday, December 3, 2002 2:44 PM

Page 13: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 13

could consume all available memory, which would prevent applications from running. By managing the program’s memory, the .NET Framework allows programmers to concen-trate on program logic.

The .NET Framework also provides programmers with a huge library of reusable classes. This library, called the Framework Class Library (FCL), can be used by any .NET language.

This book explains how to develop .NET software with Visual C++ .NET and the FCL. Steve Ballmer, Microsoft’s CEO, stated in May 2001 that Microsoft was “betting the company” on .NET. Such a dramatic commitment surely indicates a bright future for Visual C++ .NET and its community of developers.

1.9 Tour of the BookIn this section, we tour the chapters and appendices of Visual C++ .NET for Experienced Programmers: A Managed Code Approach. In addition to the topics presented in each chapter, several of the chapters contain an Internet and Web Resources section that lists ad-ditional sources from which readers can enhance their knowledge of Visual C++ program-ming.

Chapter 1—Introduction to .NET and Visual C++® .NETThe first chapter presents the history of the Internet, World Wide Web and various technol-ogies (such as XML and SOAP) that have led to advances in computing. We introduce the Microsoft .NET initiative and Visual C++ .NET, including Web services. We explore the impact of .NET on software development and software reusability. The chapter concludes with a tour of the book.

Chapter 2—Visual Studio® .NET IDE and Visual C++ .NET ProgrammingChapter 2 introduces Visual Studio .NET, an integrated development environment (IDE) that allows programmers to create applications using standard C++ and Managed Exten-sions for C++ (MC++). Visual Studio .NET contains tools for debugging and writing code. The chapter presents features of Visual Studio .NET, including its key windows, and shows how to compile and run programs. The chapter also introduces readers to console-applica-tion programming in MC++. Every concept is presented in the context of a complete work-ing MC++ program and is followed by one or more screen shots showing actual inputs and outputs as the program executes. This is our LIVE-CODE™ approach. We discuss funda-mental tasks, such as how a program inputs data from its users and how to write arithmetic expressions.

Chapter 3—Control StructuresThis chapter introduces the principles of structured programming, a set of techniques that will help the reader develop clear, understandable and maintainable programs. The chapter then introduces the use of control structures that affect the sequence in which statements are executed. Control structures produce programs that are easily understood, debugged and maintained. We discuss the three forms of program control—sequence, selection and repetition—focusing on the if/else, while, for, do/while and switch structures. We explain the break and continue statements and the logical operators. We build on infor-mation presented in the previous chapter to create programs that are interactive (i.e., they change their behavior to suit user-supplied inputs).

vcppfep_01.fm Page 13 Tuesday, December 3, 2002 2:44 PM

Page 14: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

14 Introduction to .NET and Visual C++® .NET Chapter 1

Chapter 4—Functions and ArraysA function allows the programmer to create a block of code that can be called upon from various points in a program. Larger programs can be divided into interacting classes, each consisting of functions—this is sometimes called the “divide and conquer” strategy. Pro-grams are divided into simple components that interact in straightforward ways. We discuss how to create our own functions that can take inputs, perform calculations and return out-puts. Recursive functions (functions that call themselves) and function overloading, which allows multiple functions to have the same name, are introduced. We demonstrate over-loading by creating two square functions that each take an integer (i.e., a whole number) and a floating-point number (i.e., a number with a decimal point), respectively. This chap-ter also introduces arrays, our first data structure. Data structures are crucial to storing, sort-ing, searching and manipulating large amounts of information. Arrays are groups of related data items that allow the programmer to access any element directly. Rather than creating 100 separate variables that are all related in some way, the programmer can create an array of 100 elements and access these elements by their location in the array. We discuss how to declare and allocate managed arrays, and we build on the techniques of the previous chapter by passing arrays to functions. Chapter 3 provides essential background for the dis-cussion of arrays, because repetition structures are used to iterate through elements in the array. The combination of these concepts helps the reader create highly structured and well-organized programs. We discuss multidimensional arrays (both rectangular and jagged), which can be used to store tables of data.

Chapter 5—Object-Based ProgrammingChapter 5 introduces objects and classes. Object technology has led to considerable im-provements in software development, allowing programmers to create reusable software components. Objects allow programs to be organized in natural and intuitive ways. This chapter presents the fundamentals of object-based programming, such as encapsulation, data abstraction and abstract data types (ADTs). These techniques hide the details of com-ponents so that the programmer can concentrate on the “big picture.” We create a Timeclass, which displays the time in standard and universal formats. We show how to create reusable software components with assemblies, namespaces and dynamic-link-library (DLL) files. We create classes and namespaces, and discuss properties and the const key-word. This chapter lays the groundwork for the next two chapters, which introduce object-oriented programming.

Chapter 6—Object-Oriented Programming: InheritanceIn this chapter, we discuss inheritance—a form of software reusability in which classes (called derived classes) are created by absorbing attributes and methods of existing classes (called base classes). The inherited class (i.e., the derived class) can contain additional at-tributes and methods. We show how finding the commonality between classes of objects can reduce the amount of work it takes to build large software systems. A detailed case study demonstrates software reuse and good programming techniques by finding the com-monality among a three-level inheritance hierarchy: The Point, Circle and Cylin-der classes. We discuss the software engineering benefits of object-oriented programming. Crucial object-oriented programming concepts, such as creating and extend-ing classes, are presented in this chapter.

vcppfep_01.fm Page 14 Tuesday, December 3, 2002 2:44 PM

Page 15: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 15

Chapter 7—Object-Oriented Programming: PolymorphismChapter 7 continues our presentation of object-oriented programming. We discuss poly-morphic programming and its advantages. Polymorphism permits classes to be treated in a general manner, allowing the same method call to act differently depending on context (e.g., “move” messages sent to a bird and a fish result in dramatically different types of ac-tion—a bird flies and a fish swims). In addition to treating existing classes in a general man-ner, polymorphism allows new classes to be added to a system easily. We identify situations in which polymorphism is useful. A payroll system case study demonstrates polymorphism—the system determines the wages for each employee differently to suit the type of employee (bosses, paid fixed salaries; hourly workers, paid by the hour; commis-sion workers, who receive a base salary plus commission; and piece workers, who are paid per item produced). These programming techniques and those of the previous chapter allow the programmer to create extensible and reusable software components.

Chapter 8—Exception HandlingException handling is one of the most important topics in Visual C++ .NET from the stand-point of building mission-critical and business-critical applications. Users can enter incor-rect data, data can be corrupted and clients can try to access records that do not exist or are restricted. A simple division-by-zero error may cause a calculator program to crash, but what if such an error occurs in the navigation system of an airplane while it is in flight? In some cases, the results of program failure could be disastrous. Programmers need to know how to recognize the errors (exceptions) that could occur in software components and han-dle those exceptions effectively, allowing programs to deal with problems and continue ex-ecuting instead of “crashing.” Programmers who construct software systems from reusable components built by other programmers must deal with the exceptions that those compo-nents may “throw.” This chapter covers the details of MC++ exception handling, the ter-mination model of exception handling, throwing and catching exceptions and FCL class Exception.

Chapter 9—Graphical User Interface Concepts: Part 1Chapter 9 introduces techniques for designing graphical user interfaces. We discuss how to construct user interfaces with Windows Forms controls such as labels, buttons, text boxes and picture boxes. We also introduce events, which are messages sent by a program to sig-nal to an object or a set of objects that an action has occurred. Events most commonly are used to signal user interactions with GUI controls, but also can signal internal actions in a program. We overview event handling and discuss how to handle events specific to con-trols, the keyboard and the mouse. Tips are included throughout the chapter to help the pro-grammer create visually appealing, well-organized and consistent GUIs.

Chapter 10—Graphical User Interface Concepts: Part 2Chapter 10 introduces more complex GUI components, including menus, link labels, pan-els, list boxes, combo boxes and tab controls. The chapter presents Multiple Document In-terface (MDI) programming, which allows multiple documents (i.e., forms) to be open simultaneously in a single GUI. We introduce visual inheritance, which enables program-mers to combine the GUI concepts presented in this chapter with the object-oriented con-cepts presented in Chapter 6 to create user interfaces that can be used and extended by other programmers. The chapter concludes with a discussion of creating user-defined controls.

vcppfep_01.fm Page 15 Tuesday, December 3, 2002 2:44 PM

Page 16: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

16 Introduction to .NET and Visual C++® .NET Chapter 1

Chapter 11—MultithreadingWe have come to expect much from our applications. We want to download files from the Internet, listen to music, print documents and browse the Web—all at the same time! To do this, we need a technique called multithreading, which allows applications to perform mul-tiple activities concurrently. MC++ gives programmers access to the multithreading classes provided by the FCL, while shielding programmers from complex details. MC++ is better equipped to deal with more sophisticated multimedia, network-based and multiprocessor-based applications than other languages that do not have multithreading features. This chapter overviews the threading classes in the FCL and covers threads, thread life-cycles, time-slicing, scheduling and priorities. We analyze the producer-consumer relationship, thread synchronization and circular buffers. This chapter lays the foundation for creating the impressive multithreaded programs that clients demand.

Chapter 12—Strings, Characters and Regular ExpressionsIn this chapter, we discuss the processing of words, sentences, characters and groups of characters. In MC++, strings (groups of characters) are objects of type String *. This is yet another benefit of MC++’s emphasis on object-oriented programming. Objects of type String * contain methods that can copy, search, extract substrings and concatenate strings with one another. We introduce class StringBuilder, which defines string-like objects that can be modified after initialization. As an interesting example of strings, we create a card shuffling-and-dealing simulation. We discuss regular expressions, a powerful tool for searching and manipulating text.

Chapter 13—Graphics and MultimediaIn this chapter, we discuss GDI+ (an extension of the Graphics Device Interface—GDI), the Windows service that provides the graphical features used by .NET applications. The extensive graphical capabilities of GDI+ can make programs more visual and fun to create and use. We discuss Visual C++ .NET’s treatment of graphics objects and color control. We also discuss how to draw arcs, polygons and other shapes. This chapter also demon-strates how to use various pens and brushes to create color effects and includes an example that demonstrates gradient fills and textures. We also introduce techniques for turning text-only applications into aesthetically pleasing programs that even novice programmers can write with ease. The second half of the chapter focuses on audio, video and speech technol-ogy. We discuss adding sound, video and animated characters to programs (primarily via existing audio and video clips). You will see how easy it is to incorporate multimedia into Visual C++ .NET applications. This chapter introduces a technology called Microsoft Agent for adding interactive animated characters to a program. Each character allows users to interact with the application, using more natural human communication techniques, such as speech. The agent characters respond to mouse and keyboard events, speak and hear (i.e., they support speech synthesis and speech recognition). With these capabilities, your appli-cations can speak to users and actually respond to their voice commands!

Chapter 14—Files and StreamsImagine a program that could not save data to a file. Once the program is closed, all the work performed by the program is lost forever. For this reason, this chapter is one of the most important for programmers who will be developing commercial applications. We in-troduce FCL classes for inputting and outputting data. A detailed example demonstrates

vcppfep_01.fm Page 16 Tuesday, December 3, 2002 2:44 PM

Page 17: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 17

these concepts by allowing users to read and write bank account information to and from files. We introduce the FCL classes and methods that help perform input and output con-veniently—they demonstrate the power of object-oriented programming and reusable classes. We discuss benefits of sequential files, random-access files and buffering. This chapter lays the groundwork for the material presented in Chapter 18, Networking: Streams-Based Sockets and Datagrams.

Chapter 15—Extensible Markup Language (XML)The Extensible Markup Language (XML) derives from SGML (Standard Generalized Markup Language), which became an industry standard in 1986. Although SGML is em-ployed in publishing applications worldwide, it has not been incorporated into the mainstream programming community because of its sheer size and complexity. XML is an effort to make SGML-like technology available to a much broader community. XML, created by the World Wide Web Consortium (W3C), describes data in a portable format. XML differs in concept from markup languages such as HTML, which only describes how information is rendered in a browser. XML is a technology for creating markup languages for virtually any type of in-formation. Document authors use XML to create entirely new markup languages to describe specific types of data, including mathematical formulas, chemical molecular structures, mu-sic, recipes and much more. Markup languages created with XML include XHTML (Exten-sible HyperText Markup Language, for Web content), MathML (for mathematics), VoiceXML™ (for speech), SMIL™ (Synchronized Multimedia Integration Language, for multimedia presentations), CML (Chemical Markup Language, for chemistry) and XBRL (Extensible Business Reporting Language, for financial data exchange). The extensibility of XML has made it one of the most important technologies in industry today and is being inte-grated into almost every field. Companies and individuals constantly are finding new and innovative uses for XML. In this chapter, we present examples that illustrate the basics of marking up data with XML. We demonstrate several XML-derived markup languages, such as XML Schema (for checking an XML document’s grammar), and XSLT (Extensible Stylesheet Language Transformations, for transforming an XML document’s data into an-other text-based format such as XHTML). (For readers who are unfamiliar with XHTML, we provide Appendices E and F, which provide a detailed introduction to XHTML.)

Chapter 16—Database, SQL and ADO .NETData storage and access are integral to creating powerful software applications. This chap-ter discusses .NET support for database manipulation. Today’s most popular database sys-tems are relational databases. In this chapter, we introduce the Structured Query Language(SQL) for performing queries on relational databases. We also introduce ActiveX Data Ob-jects (ADO .NET)—an extension of ADO that enables .NET applications to access and ma-nipulate databases. ADO .NET allows data to be exported as XML, which enables applications that use ADO .NET to communicate with a variety of programs that understand XML. We show the reader how to create database connections and how to use ADO .NET classes to query a database.

Chapter 17—Web ServicesPrevious chapters demonstrated how to create applications that execute locally on the us-er’s computer. In this chapter, we introduce Web services, which are programs that “ex-pose” services (i.e., methods) to clients over the Internet, intranets and extranets. Web

vcppfep_01.fm Page 17 Tuesday, December 3, 2002 2:44 PM

Page 18: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

18 Introduction to .NET and Visual C++® .NET Chapter 1

services offer increased software reusability by allowing services on disparate platforms to interact with each other seamlessly. We discuss .NET Web services basics and related tech-nologies, including Simple Object Access Protocol (SOAP) and Active Server Pages(ASP) .NET. This chapter presents an interesting example of a Web service that manipu-lates huge integers (up to 100 digits). We present a Black Jack application that demon-strates session tracking, a form of personalization that enables the application to “recognize” a user. We conclude with a discussion of Microsoft’s Global XML Web Ser-vices Architecture (GXA), a series of specifications that provide additional capabilities to Web services developers.

Chapter 18—Networking: Streams-Based Sockets and DatagramsChapter 18 introduces the fundamental techniques of streams-based networking. We dem-onstrate how streams-based sockets allow programmers to hide many networking details. With sockets, networking is as simple as if the programmer were reading from and writing to a file. We also introduce datagrams, in which packets of information are sent between programs. Each packet is addressed to its recipient and sent out to the network, which routes the packet to its destination. The examples in this chapter focus on communication between applications. One example demonstrates using streams-based sockets to commu-nicate between two MC++ programs. Another, similar example sends datagrams between applications. We also show how to create a multithreaded-server application that can com-municate with multiple clients in parallel. In this client/server tic-tac-toe game, the server maintains the status of the game, and two clients communicate with the server to play the game.

Chapter 19—Data Structures and CollectionsThis chapter discusses arranging data into aggregations such as linked lists, stacks, queues and trees. Each data structure has properties that are useful in a wide variety of applications, from sorting elements to keeping track of method calls. We discuss how to build each of these data structures. This is also a valuable experience in crafting useful classes. In addi-tion, we cover pre-built collection classes in the FCL. These classes store sets, or collec-tions, of data and provide functionality that allow the developer to sort, insert, delete and retrieve data items. Different collection classes store data in different ways. This chapter focuses on classes Array, ArrayList, Stack and Hashtable, discussing the details of each. When possible, MC++ programmers should use the FCL to find appropriate data structures, rather than implementing these data structures themselves. This chapter rein-forces much of the object technology discussed in Chapters 5–7, including classes, inherit-ance and composition.

Chapter 20—AccessibilityThe World Wide Web presents challenges to individuals with disabilities. Multimedia-rich Web sites are difficult for text readers and other programs to interpret; thus, users with hear-ing and visual impairments may have difficulty browsing such sites. To help rectify this sit-uation, the World Wide Web Consortium (W3C) launched the Web Accessibility Initiative (WAI), which provides guidelines for making Web sites accessible to people with disabili-ties. This chapter provides a description of these guidelines, such as the use of the headers element to make tables more accessible to page readers, the alt attribute of the img element to describe images and the combination of XHTML and Cascading Style

vcppfep_01.fm Page 18 Tuesday, December 3, 2002 2:44 PM

Page 19: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 19

Sheets (CSS) to ensure that a page can be viewed on almost any type of display or reader. We illustrate key accessibility features of Visual Studio .NET, Internet Explorer 6 and Windows XP. We also introduce VoiceXML™ and CallXML™, two technologies for in-creasing the accessibility of Web-based content. VoiceXML helps people with visual im-pairments to access Web content via speech synthesis and speech recognition. CallXML allows users with visual impairments to access Web-based content through a telephone.

Chapter 21—Introduction to Unmanaged Code in Visual C++ .NETThis chapter introduces the unmanaged aspect of Visual C++ .NET. We highlight the dif-ferences between managed and unmanaged code, discuss how unmanaged code fits into Microsoft's .NET strategy and suggest a possible future for unmanaged code. In addition, we discuss the new Unified Event Model, designed to provide a simplified syntax for Vi-sual C++ .NET event handling. The chapter includes a presentation of the changes to un-managed code designed to increase ANSI/ISO compliance and increase native code Wide-Character and 64-bit processing support. The chapter’s examples include a demonstration of the new compiler features designed to provide program run-time checking into the de-bugging mode of native code. This chapter (as well as Chapters 22–25) assumes knowledge of standard C++, including pointers.

Chapter 22—Attributed Programming in ATL/COMAttributed programming is a new feature in Visual C++ .NET designed to simplify com-mon programming tasks, most notably in the area of COM development. In this chapter, we discuss how attributes simplify COM and ATL. This chapter is targeted towards devel-opers with previous COM and ATL knowledge and is designed to demonstrate the power of attributed programming in COM. While attributes simplify COM development, a solid understanding of COM basics is essential to use attributes correctly. The chapter’s exam-ples demonstrate basic COM component creation, the Unified Event Model in COM and include a case study demonstrating an attributed ATL ActiveX control.

Chapter 23—ATL Server Web ApplicationsIn this chapter, we discuss how to create Web-based applications using ATL Server. This technology provides rapid Web-application development coupled with native code perfor-mance. ATL Server is an integral technology for creating dynamic Web content in a native context. Server Response Files (SRFs) provide dynamic content using replacement tags (or stencils) coupled with HTML markup. An ATL Server Web application dynamically re-places SRF stencils with native C++ generated HTML. This chapter presents many inter-esting examples, which include an online guest book application and a multi-tier, database intensive application that allows users to query a database for a list of publications by a spe-cific author.

Chapter 24—Managed and Unmanaged InteroperabilityThis chapter focuses on the differences between managed and unmanaged code, and the technologies used to surmount the differences to allow interoperability between code con-texts. Unlike other .NET languages, Visual C++ .NET allows side-by-side execution of managed and unmanaged code within the same application. This is possible due to the sep-aration of execution into a managed and unmanaged context and managing all communi-cation between the contexts. The interoperability features of Visual C++ .NET may be split into three technologies, the It Just Works (IJW) compiler design goal, the Platform Invoke

vcppfep_01.fm Page 19 Tuesday, December 3, 2002 2:44 PM

Page 20: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

20 Introduction to .NET and Visual C++® .NET Chapter 1

Services and COM Interop Services. This chapter discusses IJW and the Platform Invoke Services, while Chapter 25 discusses the COM interoperability features. This chapter con-tains a set of examples designed to demonstrate common interoperability uses, such as us-ing Win32 API functions and templated classes.

Chapter 25—COM Interoperability ServicesThis chapter overviews the services provided by Visual C++ .NET that allow interopera-bility between the COM and .NET architectures. The COM and .NET component architec-tures are fundamentally different technologies, from data type representation and object construction to the descriptions and layout of their respective component libraries. The COM interoperability services enable the .NET programmer to bypass these difficulties and provide seamless interaction between both component architectures. The chapter dem-onstrates type-library and assembly conversions, discusses the Runtime Callable Wrapper and the COM Callable Wrapper and provides examples that show COM objects in .NET and .NET objects in COM.

Appendix A—Operator Precedence ChartThis appendix lists MC++ operators and their precedence.

Appendix B—Number SystemsThis appendix explains the binary, octal, decimal and hexadecimal number systems. It also reviews the conversion of numbers among these bases and illustrates mathematical opera-tions in each base.

Appendix C—ASCII Character SetThis appendix contains a table of the 128 ASCII (American Standard Code for Information Interchange) alphanumeric symbols and their corresponding integer values.

Appendix D—Unicode®

This appendix introduces the Unicode Standard, an encoding scheme that assigns unique numeric values to the characters of most of the world’s languages. We include a Windows application that uses Unicode encoding to print welcome messages in several languages.

Appendices E and F—Introduction to XHTML: Parts 1 & 2In these appendices, we introduce the Extensible HyperText Markup Language (XHTML), an emerging W3C technology designed to replace HTML as the primary means of describ-ing Web content. As an XML-based language, XHTML is more robust and extensible than HTML. XHTML incorporates most of HTML’s elements and attributes—the focus of these appendices. Appendices E and F are included for our readers who do not know XHTML or who would like a review of XHTML before studying Chapter 15, Extensible Markup Lan-guage (XML), and Chapter 20, Accessibility.

Appendix G—XHTML Special CharactersThis appendix provides many commonly used XHTML special characters, called character entity references.

vcppfep_01.fm Page 20 Tuesday, December 3, 2002 2:44 PM

Page 21: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 21

Appendix H—XHTML ColorsThis appendix lists commonly used XHTML color names and their corresponding hexadec-imal values.

Appendix I—Bit ManipulationThis appendix discusses Visual C++ .NET’s powerful bit-manipulation capabilities. This helps programs process bit strings, set individual bits on or off and store information more compactly. Such capabilities are characteristic of low-level assembly languages and are valued by programmers writing systems software, such as operating system and network-ing software.

1.10 SummaryIn the late 1960s, at a conference at the University of Illinois Urbana-Champaign, ARPA—the Advanced Research Projects Agency of the Department of Defense—rolled out the blueprints for networking the main computer systems of approximately a dozen ARPA-funded universities and research institutions. Shortly after this conference, ARPA proceed-ed to implement the ARPAnet, the grandparent of today’s Internet.

Although the ARPAnet did enable researchers to network their computers, its chief benefit proved to be its capability for quick and easy communication via what came to be known as electronic mail (e-mail). This is true even on today’s Internet, with e-mail, instant messaging and file transfer facilitating communications among hundreds of millions of people worldwide.

The protocol (i.e., set of rules) for communicating over the ARPAnet became known as the Transmission Control Protocol (TCP). TCP ensured that messages were routed prop-erly from sender to receiver and that those messages arrived intact. ARPA developed the Internet Protocol (IP), which created a true “network of networks,” the current architecture of the Internet. The combined set of protocols is now commonly called TCP/IP.

The World Wide Web allows computer users to locate and view multimedia-based documents (i.e., documents with text, graphics, animations, audios or videos) on almost any subject. In 1989, Tim Berners-Lee of CERN (the European Organization for Nuclear Research) began to develop a technology for sharing information via hyperlinked text doc-uments. Berners-Lee called his invention the HyperText Markup Language (HTML). He also wrote communication protocols to form the backbone of his new hypertext informa-tion system, which he referred to as the World Wide Web.

In October 1994, Berners-Lee founded the World Wide Web Consortium (W3C), an organization that is devoted to developing nonproprietary, interoperable technologies for the World Wide Web. One of the W3C’s primary goals is to make the Web universally accessible—regardless of an individual’s disabilities, language or culture.

XML combines the power and extensibility of its parent language, SGML, with sim-plicity. XML is a meta-language that offers a high level of extensibility. Using XML, the W3C created the Extensible HyperText Markup Language (XHTML), an XML vocabulary that provides a common, extensible format for the Web. In addition to serving as the basis for other markup languages, developers use XML for data interchange and e-commerce systems. At the time of this writing, there were more than 450 XML standards.

vcppfep_01.fm Page 21 Tuesday, December 3, 2002 2:44 PM

Page 22: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

22 Introduction to .NET and Visual C++® .NET Chapter 1

Unlike many technologies, which begin as proprietary solutions and become stan-dards, XML was defined as an open, standard technology. XML’s development has been supervised by the W3C’s XML Working Group, which prepared the XML specification and approved it for publication. In 1998, the XML version 1.0 specification was accepted as a W3C Recommendation.

Object technology is a packaging scheme that facilitates the creation of meaningful software units. Objects have properties (i.e., attributes, such as color, size and weight) and perform actions (i.e., behaviors, such as moving, sleeping or drawing). Classes represent groups of related objects.

With the advent of popular object-oriented languages, such as C++, Java and C#, pro-grammers can program in an object-oriented manner that reflects the way in which they perceive the world. This process, which seems more natural than procedural programming, has resulted in significant productivity gains.

With object technology, properly designed software entities (called objects) can be reused on future projects. Using libraries of reusable componentry can reduce the amount of effort required to implement certain kinds of systems (as compared to the effort that would be required to reinvent these capabilities in new projects). Visual C++ .NET pro-grammers use the .NET Framework Class Library (FCL).

In June 2000, Microsoft announced its .NET initiative. The .NET platform is one that provides significant enhancements to earlier developer platforms. .NET offers a new soft-ware-development model that allows applications created in disparate programming lan-guages to communicate with each other. The platform also allows developers to create Web-based applications that can be distributed to a great variety of devices (even wireless phones) and to desktop computers.

One key aspect of the .NET strategy is its independence from a specific language or platform. Rather than requiring programmers to use a single programming language, devel-opers can create a .NET application by using any combination of .NET-compatible lan-guages. Programmers can contribute to the same software project, writing code in the .NET languages in which they are most proficient.

A key component of the .NET architecture is Web services, which are applications that expose functionality to clients via the Internet. Clients and other applications can use these Web services as reusable building blocks.

Universal data access is another essential .NET concept. With .NET, data can reside in one central location rather than on separate systems. Any Internet-connected device can access the data (under tight control, of course), which would then be formatted appropri-ately for use or display on the accessing device.

Developed in the early 1990s, Visual C++ is a Microsoft implementation of C++ that includes Microsoft’s proprietary extensions to the language. Over the past decade, Microsoft has released several versions of Visual C++—most recently, Visual C++ .NET. Visual C++ .NET is known as a visual programming language—the developer uses graph-ical tools (via Visual Studio .NET) to create applications.

Early graphics and graphical user interface (GUI) programming (i.e., the programming of an application’s visual interface) with Visual C++ was implemented using the Microsoft Foundation Classes (MFC). The Microsoft Foundation Classes library is a collection of classes that help Visual C++ programmers create powerful Windows-based applications. Now, with the introduction of .NET, Microsoft provides an additional library (.NET’s FCL) for implementing GUI, graphics, networking, multithreading and other capabilities.

vcppfep_01.fm Page 22 Tuesday, December 3, 2002 2:44 PM

Page 23: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

Chapter 1 Introduction to .NET and Visual C++® .NET 23

This library is available to .NET-compliant languages such as Visual C++ .NET, Visual Basic .NET and Microsoft’s new language, C#. However, developers still can use MFC—Microsoft has, in fact, upgraded MFC to MFC 7, which includes new classes and documen-tation that can be accessed using Visual Studio .NET. MFC often is used to develop unman-aged code, or code that does not make use of the .NET Framework.

Visual C++ .NET introduces Managed Extensions for C++ (MC++) that enable a pro-grammer to access the .NET Framework. Programmers can use the .NET Framework to create objects that provide automatic garbage collection, memory management and lan-guage interoperability (with other .NET languages). Such objects are known as managed objects; the code that defines these objects is known as managed code. With managed objects, memory leaks are extremely rare.

The true power of Visual C++ .NET lies in its interoperability between managed and unmanaged code. All legacy Visual C++ code is unmanaged. Rather than forcing devel-opers to discard large amounts of legacy code, Visual C++ .NET enables programmers to mix managed (MC++) and unmanaged (standard C++) code.

Microsoft has upgraded several of the technologies used in unmanaged code. One example of this is the Active Template Library (ATL), a set of template classes that sim-plify the creation of COM components. The newest version of ATL, ATL 7, contains sev-eral enhancements. An example of one enhancement is the ATL Server, which is a set of classes designed to simplify Web development while providing the ability to call unman-aged C++ from within a Web page.

In Visual C++ .NET, programs are created using Visual Studio .NET, an Integrated Development Environment (IDE). With the IDE, a programmer can create, run, test and debug programs conveniently, thereby reducing the time it takes to produce a working pro-gram to a fraction of the time it would have taken without using the IDE. The process of rapidly creating an application using an IDE typically is referred to as Rapid Application Development (RAD).

The .NET Framework is at the heart of .NET. This framework manages and executes applications, contains the FCL, enforces security and provides many other programming capabilities. The details of the .NET Framework are found in the Common Language Specification (CLS), which contains information about the storage of data types, objects and so on.

The Common Language Runtime (CLR) is another central part of the .NET Frame-work—it executes Visual C++ .NET programs. Programs are compiled into machine-spe-cific instructions in two steps. First, the program is compiled into Microsoft Intermediate Language (MSIL), which defines instructions for the CLR. Code converted into MSIL from other languages and sources is woven together by the CLR. Then, another compiler in the CLR compiles the MSIL into machine code (for a particular platform), creating a single application.

1.11 Internet and World Wide Web Resourceswww.deitel.comThis is the official Deitel & Associates, Inc. Web site. Here you will find updates, corrections, down-loads and additional resources for all Deitel publications. In addition, this site provides information about Deitel & Associates, Inc., information on international translations and much more.

vcppfep_01.fm Page 23 Tuesday, December 3, 2002 2:44 PM

Page 24: Introduction to .NET and Visual C++ - Deitel & Associates · Chapter 1 Introduction to .NET and Visual C++®.NET 3 The network was designed to operate without centralized control

24 Introduction to .NET and Visual C++® .NET Chapter 1

www.deitel.com/newsletter/subscribe.htmlYou can register here to receive the DEITEL™ BUZZ ONLINE e-mail newsletter. This free monthly newsletter updates readers on our publishing program, instructor-led corporate training courses, hot-test industry trends and topics and much more. The newsletter is available in full-color HTML and plain-text formats.

www.prenhall.com/deitelThis is Prentice Hall’s Web site for Deitel publications, which contains information about our prod-ucts and publications, downloads, Deitel curriculum and author information.

www.InformIT.com/deitelThis is the Deitel & Associates, Inc. page on Pearson’s InformIT Web site. (Pearson owns our pub-lisher Prentice Hall.) InformIT is a comprehensive resource for IT professionals providing articles, electronic publications and other resources for today’s hottest information technologies. The Deitel kiosk at InformIT.com provides two or three free articles per week and for-purchase electronic publications. All Deitel publications can be purchased at this site.

www.w3.orgThe World Wide Web Consortium (W3C) is an organization that develops and recommends technol-ogies for the Internet and World Wide Web. This site includes links to W3C technologies, news, mis-sion statements and frequently asked questions (FAQs). Deitel and Associates, Inc. is a member of the W3C.

www.microsoft.comThe Microsoft Corporation Web site provides information and technical resources for all Microsoft products, including .NET, enterprise software and the Windows operating system.

www.microsoft.com/netThe .NET home page provides downloads, news and events, certification information and subscrip-tion information.

vcppfep_01.fm Page 24 Tuesday, December 3, 2002 2:44 PM