Microsoft Visual C++.Net [1 ed.] 0619016574, 9780619016579

Master the skills to create applications in the new, dynamic Visual C++.NET development platform! Rather than using pre-

123 107 3MB

English Pages [58]

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Microsoft Visual C++.Net [1 ed.]
 0619016574, 9780619016579

Table of contents :
Chapter 1: Introduction to Programming and Visual C++
Computer Programming and Programming Languages
Logic and Debugging
Creating a New Project in Visual C++
The Visual Studio Ide
Managing the Solution
Visual C++ Help
Chapter Summary
Review Questions
Programming Exercises

Citation preview

Licensed to: iChapters User

A4535_FM

2/9/09

Licensed to: iChapters User

9:06 AM

Page ii

Microsoft® Visual C++®.NET Don Gosselin Senior Vice President, Publisher: Kristen Duerr Managing Editor: Jennifer Locke Senior Product Manager: Margarita Leonard Development Editor: Marilyn Freedman Production Editor: Melissa Panagos Marketing Manager: Angie Laughlin Associate Product Manager: Janet Aras Editorial Assistant: Christy Urban Text Designer: GEX Publishing Services

© 2002 Course Technology, Cengage Learning ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher. For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 For permission to use material from this text or product, submit all requests online at www.cengage.com/permissions Further permissions questions can be emailed to [email protected]

Cover Designer: Abby Scholz Manufacturing Manager: Denise Sandler

ISBN-13: 978-0-619-01657-9 ISBN-10:0-619-01657-4 Course Technology 20 Channel Center Street Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. To learn more about Course Technology, visit www.cengage.com/coursetechnology To learn more about Cengage Learning, visit www.cengage.com. Purchase any of our products at your local college store or at our preferred online store www.ichapters.com

Printed in the United States of America 4 5 6 7 8 9 11 10 09

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 1

Licensed to: iChapters User

CHAPTER

1

INTRODUCTION TO PROGRAMMING AND VISUAL C++ In this chapter you will learn about: ♦ ♦ ♦ ♦ ♦ ♦ ♦

Computer programming and programming languages C/C++ programming Logic and debugging Creating a new project in Visual C++ The Visual Studio IDE Managing the solution Visual C++ Help

There is nothing more difficult to take in hand, more perilous to conduct or more uncertain in its success than to take the lead in the introduction of a new order of things. Niccolo Machiavelli The Prince 1532

COMPUTER PROGRAMMING

AND

PROGRAMMING LANGUAGES

Creating instructions that control the operation of a computer or computer hardware is called computer programming. The instructions themselves are called computer programs, applications, software, or simply programs.The pieces of information that a program needs are called data. Think, for a moment, about an automobile. The automobile is useless without someone to drive it. If you compare an automobile to a computer, you can see that the computer is useless without a program that drives its hardware.

1 Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 2

Licensed to: iChapters User

2

Chapter 1

Introduction to Programming and Visual C++

When you start a program and provide it with the data it needs to function, you are running, or executing, the program. To develop the comparison to an automobile a little further, you can think of the driver as the program that operates the automobile. The gas and oil that the automobile needs to operate are its data. Figure 1-1 illustrates the concept of how hardware, software, and data all work together to execute an automobile program. Gasoline (data)

Driver (computer program)

Automobile (computer hardware)

Figure 1-1

Motor Oil (data)

Executing an automobile program

The instructions used to create computer programs are called programming languages. To understand Visual C++ programming and how it relates to the C/C++ programming languages, it is helpful to know a little background about computer programming and how current programming languages evolved.

Machine and Assembly Languages The electronic circuitry of a computer is controlled by two simple electronic signals, or switches—an on switch and an off switch. A 1 represents the on switch, and a 0 (zero) represents the off switch. Telling a computer what to do involves writing programs that set these switches to on or off. Machine language is the lowest level of computer languages, and programs written in machine language consist entirely of 1s and 0s that control the computer’s on and off switches. For example, a program written in machine language may contain lines similar to the following (note that the following code is not an actual program; it just serves to give an idea of how difficult it can be to program with machine language):

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 3

Licensed to: iChapters User

Computer Programming and Programming Languages

0 1 0 1 0 1 0

0 0 0 0 0 0 0

1 0 1 0 1 0 1

0 1 1 0 0 1 1

1 1 0 0 1 1 0

0 0 0 1 1 1 1

1 0 1 1 0 1 0

0 1 1 1 1 0 0

3

1 1 0 1 0 0 1

1

Writing a program in machine language is very difficult because you must understand how the exact placement and combination of 1s and 0s will affect your program. Assembly languages provide an easier (although still challenging) method of controlling a computer’s on and off switches. Assembly languages perform the same tasks as machine languages, but use simplified names of instructions instead of 1s and 0s.To get an idea of how difficult it can be to program with assembly languages, examine the following assembly code, which only performs some simple numeric calculations. Main proc ƒƒƒmov ax, dseg Aƒƒinteger ? Bƒƒinteger ? Cƒƒinteger ? cseg segment para public ‘code’ assume cs:cseg, ds:dseg Main proc ƒƒƒmov ax, dseg ƒƒƒmov ds, ax ƒƒƒmov es, ax ƒƒƒmov A, 3 ƒƒƒmov B, -2 ƒƒƒmov C, 254 ƒƒƒmov ax, A ƒƒƒadd ax, B ƒƒƒmov C, ax

Machine languages and assembly languages are known as low-level languages because they are the programming languages that are closest to a computer’s hardware. Each type of central processing unit (CPU) contains its own internal machine language and assembly language. To write programs in machine language, a programmer must know the specific machine language and assembly language for the type of CPU on which a program will run. Because each CPU’s machine language and assembly language is unique, it is difficult to translate low-level languages from one CPU to another.

High-Level Programming Languages Because of the difficulty of working with low-level languages, high-level, or symbolic, languages were developed to make it easier to write computer programs. High-level programming languages create computer programs using instructions that are much

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 4

Licensed to: iChapters User

4

Chapter 1

Introduction to Programming and Visual C++

easier to understand than machine or assembly language code because you use words that more clearly describe the task being performed. Examples of high-level languages include C++, BASIC, and COBOL. To understand the difference between low-level languages and high-level languages, consider the following assembly language code that adds two numbers, and then assigns the result to a variable named C: mov mov add mov

A, B, A, C,

3 2 B A

Although easier than machine language, the above assembly language code is still difficult to understand. In comparison, the same task is accomplished in a high-level language using a much simpler statement. For example, the following C++ statement performs the same addition task and assigns the result to a variable named C: int C = 3 + 2;.The syntax in C++ is much easier to understand than the syntax in assembly language. Another advantage to high-level programming languages is that they are not CPU-specific, as are machine and assembly languages.This means that a program you write in a high-level programming language will run on many different types of CPUs, regardless of their machine languages or assembly languages. However, in order to run, programs written in high-level languages must first be translated into a low-level language using a program called a compiler. A compiler translates programming code into a low-level format.You need to compile a program only once when you are through writing it or after editing an existing program.When you execute the program, you actually execute the compiled, low-level format of the program. However, each time you make any changes to an existing program, you must recompile it before the new version of the program will execute.

Procedural Programming One of the most common forms of programming in high-level languages is called procedural programming. In procedural programming, computer instructions are often grouped into logical units called procedures. In C++ programming, procedures are referred to as functions. Each line in a procedural program that performs an individual task is called a statement. For example, a checkbook program may contain a series of statements grouped as a function named balanceCheckbook() that balances a checkbook. Another function named sumDeposits() may be used to calculate the total of all deposits made during a single period. A single procedural program may contain hundreds of variables and thousands of statements and functions. When a procedure or function is referred to in the book, its name is usually followed by two parentheses, as in balanceCheckbook(). Tip

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 5

Licensed to: iChapters User

Computer Programming and Programming Languages

5

Procedures are also called routines or subroutines.

1

Note

One of the most important aspects of procedural programming is that it allows you to temporarily store pieces of data in computer memory locations called variables.The information contained in a specific variable often changes. For example, you may have a program that creates a variable containing the current time. Each time the program runs, the time is different, so the value varies.The value of a variable often changes during the course of program execution. For example, a payroll program might assign employee names to a variable named employeeName. The memory location referenced by the variable employeeName might contain different values (a different value for every employee of the company) at different times. Another form of data that you can store in computer memory locations is a constant. A constant contains information that does not change during the course of program execution.You can think of a constant as a variable with a constant value. A common example of a constant is the value of pi (π), which represents the ratio of the circumference of a circle to its diameter.The value of pi never changes from the constant value of 3.141592. The statements within a procedural program usually execute in a linear fashion, one right after the other. Figure 1-2 displays a simple procedural program written in BASIC that calculates and prints the average of the numbers 1, 2, and 3.The first two statements in the program create variables named SUM and COUNT. During the course of program execution, the numbers 1, 2, and 3 are added to the SUM variable.The COUNT variable maintains a record of how many numbers are assigned to the SUM variable. Finally, the average is calculated using the statement SUM / COUNT. The last statement, which begins with PRINT, prints the result of the program to the screen. LET SUM = 0 LET COUNT = 0 LET SUM = SUM + 1 LET COUNT = COUNT + 1 LET SUM = SUM + 2 LET COUNT = COUNT + 1 LET SUM = SUM + 3 LET COUNT = COUNT + 1 LET AVERAGE = SUM / COUNT PRINT “The average is “; AVERAGE

Figure 1-2

A procedural program written in BASIC

Object-Oriented Programming Procedural-based programs are self-contained; most code, such as variables, statements, and functions, exists within the program itself. For example, you may have written a small business program that calculates accounts receivable and accounts payable. To add

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 6

Licensed to: iChapters User

6

Chapter 1

Introduction to Programming and Visual C++

to the program a new function that calculates the interest on a loan, you must include all the required code within the accounting program, using variables, statements, and functions. If you want to use the interest calculation code in another program, you must copy all of its statements into the new program or recreate it from scratch. Object-oriented programming takes a different approach. Object-oriented programming (OOP) refers to the creation of reusable software objects that can be easily incorporated into another program. Reusable software objects are often referred to as components. For example, you could refer to all of the interest calculation code as a single object—which you could then use over and over again just by using the object name. Popular object-oriented programming languages include C++, Java,Visual Basic, and Turbo Pascal. In object-oriented programming, an object is programming code and data that can be treated as an individual unit or component. Data refers to information contained within variables, constants, or other types of storage structures. The functions associated with an object are referred to as methods.Variables that are associated with an object are referred to as properties or attributes. Objects can range from simple controls such as a button, to entire programs such as a database application. Object-oriented programming allows programmers to use programming objects that they have written themselves or that have been written by others. One of the most powerful features of object-oriented programming is that it allows programmers to use objects in their programs that may have been created in an entirely different programming language. For example, if you are creating an accounting program in Turbo Pascal, you can use an object named Payroll that was created in C++. The Payroll object may contain one method that calculates the amount of federal and state tax to deduct, another function that calculates the FICA amount to deduct, and so on. Properties of the Payroll object may include an employee’s number of tax withholding allowances, federal and state tax percentages, and the cost of insurance premiums. You do not need to know how the Payroll object was created in C++, nor do you need to re-create it in Turbo Pascal.You only need to know how to access the methods and properties of the Payroll object from the Turbo Pascal program. The object-oriented Accounting program is illustrated in Figure 1-3. In the figure, the Accounting program is composed of three separate objects, or components: an Accounts Receivable object, the Payroll object, and an Accounts Payable object. The important thing to understand is that you do not need to rewrite the Payroll, Accounts Payable, and Accounts Receivable objects for the Accounting program; the Accounting program only needs to call their methods and provide the correct data to their properties.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 7

Licensed to: iChapters User

Computer Programming and Programming Languages

7

Accounting Program

1

Accounts Receivable object

Accounts Payble object

Payroll object

Figure 1-3

Note

Accounting receivable program The diagram in Figure 1-3, along with other diagrams in this book, is created in Unified Modeling Language, or UML, which is a symbolic language for visually designing and documenting software systems. Each of the symbols in Figure 1-3 is a UML representation of a component.

Objects are encapsulated, which means that all code and required data are contained within the object itself. Encapsulation is also referred to as a black box, because of the invisibility of the code inside an encapsulated object. When an object is well written, you cannot see “inside” it—all internal workings are hidden. The code (methods and statements) and data (variables and constants) contained in an encapsulated object are accessed through an interface. An interface represents elements required for a source program to communicate with an object. For example, interface elements required to access a Payroll object might be a method named calcNetPay(), which calculates an employee’s net pay, and properties containing the employee’s name and pay rate. You can compare a programming object and its interface to a hand-held calculator.The calculator represents an object, and you represent a program that wants to use the object. You establish an interface with the calculator object by entering numbers (the data required by the object) and then pressing calculation keys (which represent the methods of the object.) You do not need to know, nor can you see, the inner workings of the calculator object. As a programmer, you are concerned only with what the methods and properties are and what results to expect the calculator object to return. Figure 1-4 illustrates the idea of the calculator interface.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 8

Licensed to: iChapters User

8

Chapter 1

Introduction to Programming and Visual C++ Program (You)

Object (Calculator)

Interface Methods and Properties (Calculation and number buttons)

Figure 1-4

Calculator interface

Another example of an object and its interface is a Windows word-processing program. The word-processing program itself is actually a type of object made up of numerous other objects.The program window is called the user interface.The items you see in the word-processing window, such as the menu, toolbars, and other elements, are interface items used for executing methods. For example, an icon that you click to make text bold is an interface element that executes a bold method. The data you provide to the program is the text of your document.You do not need to know how the method works, only what it does.You only need to provide the data (text) and execute the appropriate methods (such as the bold method), when necessary. In object-oriented programming, the code, methods, attributes, and other information that make up an object are contained in a structure known as a class. Programming objects are created from classes. When you use an object in your program, you actually create an instance of the class of the object. An instance is an object that has been created from an existing class. In fact, a class is really just a template, or blueprint, from which you create objects.As an example, let’s return to the Payroll object mentioned earlier.The Payroll object is created from a Payroll class.To use the Payroll class, you create an instance of the class. Particular instances of objects inherit their characteristics from a class. Inheritance refers to the ability of an object to take on the characteristics of the class on which it is based.The Payroll object, for instance, inherits all of the characteristics of the Payroll class.To give another example, when you create a new word-processing document, which is a type of object, it usually inherits the properties of a template on which it is based. The template is a type of class. The document inherits characteristics of the template such as font size, line spacing, and boilerplate text. In the same manner, programs that include instances of objects inherit the object’s functionality. You will learn about object-oriented programming throughout the course of this book. Note

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 9

Licensed to: iChapters User

Computer Programming and Programming Languages

9

Consider the class upon which the Payroll object might be based. As mentioned, the Payroll class (upon which the Payroll object is based) may include a method named calcNetPay().The Payroll class may also include a calcFederalTaxes() method, a calcStateTaxes() method, and a deductIRAContribution() method. Some of the properties of the Payroll class may include federalTaxRate, stateTaxRate, insurancePremium, and iraContribution. Each time you create a new instance of the Payroll object, the object inherits its own copies of these methods and objects. For example, a company that generates its payroll once a month would create 12 separate Payroll objects. Figure 1-5 illustrates how the January and February Payroll objects inherit all of the methods and properties of the Payroll class.

1

Payroll Class insurancePremium federalTaxRate stateTaxRate iraContribution calcNetPay() calcFederalTaxes() calcStateTaxes() deductIRAContribution()

class object instance

class object instance

January Payroll Object

February Payroll Object

insurancePremium federalTaxRate stateTaxRate iraContribution

insurancePremium federalTaxRate stateTaxRate iraContribution

calcNetPay() calcFederalTaxes() calcStateTaxes() deductIRAContribution()

calcNetPay() calcFederalTaxes() calcStateTaxes() deductIRAContribution()

Figure 1-5

Payroll class and objects

Although you will not return to object-oriented programming for several chapters, you need to understand that the classes and objects you can create with C++ are the most important and powerful part of the C++ programming language. In fact, the primary goal of this book is to provide you with a firm foundation in the concepts of classes and object-oriented programming. However, before you can learn about classes and objectoriented programming in detail, you need to understand some of the more basic aspects

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 10

Licensed to: iChapters User

10

Chapter 1

Introduction to Programming and Visual C++

of C++, such as data types, functions, and decision-making statements. These concepts will be examined in the next few chapters

C/C++ PROGRAMMING The term C/C++ refers to two separate, but related, programming languages: C and C++. At Bell Laboratories in the 1970s, Dennis Ritchie and Brian Kernighan designed the procedural C programming language based upon two earlier languages, BCPL and B. In 1985, again at Bell Laboratories, Bjarne Stroustrup created C++ based on the C programming language. C++ is an extension of C that adds object-oriented programming capabilities. You create C and C++ programs in text files using a text-editing tool such as Notepad. The original program structure you enter into a text file is referred to as source code. Once you finish creating your program source code, you use a compiler to translate it into the machine language of the computer on which the program will run. The compiled machine language version of a program is called object code. Once you compile a program into object code, some systems require you to use a program called a linker, which converts the object code into an executable file, typically with an extension of .exe. Figure 1-6 uses a simple program that prints the text Hello World to a console application window to show how source code is transformed into to object code and then into an executable process. You cannot read object code or the code in an .exe file.The only code format in humanreadable form is source code. In Visual C++, you compile and link a program in a single step known as building.You will learn how to build a program later in this chapter. Remember that each computer contains its own internal machine language. The C/C++ compiler you use must be able to translate source code into the machine language of the computer, or platform, on which your program will run. A platform is an operating system and its hardware type. For example, Windows operating systems for PCs, Mac OS 10 operating system for Macintosh, and Solaris for SPARC are different platforms. Numerous vendors market C/C++ compilers for various platforms. Some vendors offer complete development environments containing built-in code editors, compilers, and linkers. Microsoft Visual C++ and Borland C++ Builder are examples of C/C++ professional development environments that contain built-in code editors and compilers, as well as many other development tools.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 11

Licensed to: iChapters User

C/C++ Programming

11

1 Source code

Source code is compiled into object code

Object code

Object code is converted into an executable

Executable file (HelloWorld.exe)

Figure 1-6

Source code to object code to executable process

The C Programming Language One of the most important programs created with the original C language was the UNIX operating system. Because of its involvement in the creation of UNIX and its subsequent use as the main programming language for the UNIX platform, C was originally used almost exclusively on UNIX platforms. During the 1980s, C compilers were written for other platforms, including personal computers.The first C compilers for other platforms were based on the original version of the language developed by Ritchie and Kernighan. By 1985 numerous C compilers had been created by independent software vendors, many of which did not conform to the original version of the language. To provide a level of standardization for the C language, in 1989 the American National Standards Institute (ANSI) created a standardized version of C that is commonly referred to as ANSI C. In 1990, a worldwide standard for the C language named ANSI/ISO C was approved by the International Standards Organization (ISO). One of the great benefits of the C language is that it is much closer to assembly language than other types of high-level programming languages. Being closer to assembly language means that programs written in C often run much faster and more efficiently than programs written in other types of high-level programming languages. Additionally, thanks to

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 12

Licensed to: iChapters User

12

Chapter 1

Introduction to Programming and Visual C++

the ANSI/ISO standard, the same C program can usually run on many different platforms. However, C’s closeness to assembly language can also be a disadvantage. It can make C difficult to use and not ideally suited to certain types of applications, such as graphical applications and object-based programs that you want to use with other programming languages.

The C++ Programming Language Although C programs are not ideally suited to graphical applications, C++ is a different story. C++ is currently the most popular programming language for developing graphical programs that run on platforms such as Macintosh and Windows. Graphical programs refers to programs with visual components such as dialog boxes, menus, toolbars, and so on. C++ has the ability to create graphical programs because of the objectoriented programming capabilities added by Stroustrup when he first developed the language from its C predecessor. Much of this book explores C++’s object-oriented programming capabilities. As with the C language, early versions of C++ suffered from a lack of standardization until the worldwide ANSI/ISO C++ standard was approved in 1997. The standardized version of C++ is commonly referred to as ANSI C++. The ANSI C++ standard ensures that programs written in C++ are compatible with different compilers and can be run on different platforms. The ANSI C and ANSI C++ standards define how C/C++ code can be written. The ANSI standards also define run-time libraries, which contain useful functions, variables, constants, and other programmatic items that you can add to your programs. The ANSI C++ run-time library is also called the Standard Template Library or Standard C++ Library. Although the run-time libraries are not actually part of each language’s structure, they are required for a compiler to conform to the ANSI C/C++ standards.You can be assured that any of the components of the run-time libraries that you use in your programs will be available to all compilers that support the ANSI C/C++ standard. Both the C and C++ programming languages are still in use today. In fact,Visual C++ supports both the C and C++ languages. Most of the features of C are also available in C++, which means you can use C++ to write both procedural and object-oriented programs. For this reason, this book focuses primarily on the C++ language.

Visual C++ .NET Microsoft Visual C++ .NET, or Visual C++ for short, is a Windows-based, visual development environment for creating C and C++ applications. It is part of the Microsoft Visual Studio line of products.Visual C++ contains a built-in code editor, compiler, and other tools for creating programs.You can create both C and C++ applications in Visual C++. The C and C++ language syntax used in Visual C++ conforms to ANSI C/C++ specifications. Actually, Visual C++ itself is not a programming language. It is a development environment used for creating programs with the C/C++ languages.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 13

Licensed to: iChapters User

C/C++ Programming

13

Visual C++ supports a number of extensions to the ANSI C/C++ run-time libraries. Extensions are new or additional features that have been added to the original run-time libraries.Visual C++ extensions may or may not be supported by other C/C++ compilers, so you cannot be absolutely certain that any programs you write that use the extensions will be able to run on other platforms. For example, Visual C++ extends the C++ language by allowing you to include Microsoft Foundation Classes in your programs. Microsoft Foundation Classes (MFCs), are libraries of classes that can be used as the building blocks for creating Windows applications with Visual C++. It is very important to understand that any Visual C++ programs you create that utilize MFCs will not conform to the ANSI C/C++ standards, and therefore will not be able to run with other vendor’s C/C++ compilers. If you need your C/C++ program to be portable to other platforms, you must use only the standard ANSI C/C++ run-time libraries. Nevertheless, MFCs are an extremely powerful feature of Visual C++ because they allow you to create true Windows applications. One of the primary uses of Visual C++ is in the creation of Windows applications, and many of the projects you create in this book will include MFCs and therefore will not conform to ANSI C/C++.

1

You will learn how to use the various Visual C++ libraries throughout this book. Note

The visual aspect of Visual C++ is used for designing the user interface of certain types of programs, such as an MFC program.You can also use your mouse to draw some user interface elements of your program, such as the controls in a dialog box, using various Visual C++ tools. Figure 1-7 shows an example of the visual portion of a calculator program that you will work on in later chapters.You will draw the user interface elements shown in the figure using the controls displayed in the Toolbox window.

Figure 1-7

Visual portion of a calculator program that you will work on in later chapters

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

1 Chapter A4535 25000 4/22/02 2:42 PM Page 14

Licensed to: iChapters User

14

Chapter 1

Introduction to Programming and Visual C++

The Microsoft Visual Studio line of development tools includes a platform called the .NET Framework that is designed for developing and running Internet applications, primarily Web-based applications and services. As part of the .NET Framework, Microsoft has introduced a new programming language, C# (pronounced C sharp). C# is an object-oriented programming language based on C/C++ that creates Web-based programs designed to run on the .NET Framework. This text does not discuss C#, but focuses on traditional C++ programming. Traditional C++ programs, including Visual C++ programs, are not designed to run on the .NET Framework. Rather, they are designed to run on standard platforms such as Windows and UNIX operating systems. You can find a supplemental chapter online at www.course.com that discusses how to use Managed Extensions to write C++ programs that operate on the .NET Framework. Managed Extensions are special sets of code that allow traditional C++ programs to function on the .NET Framework. To locate supplemental chapters and other support material for this book, search on 0-619-01657-4 at www.course.com. Tip

If you would like to study the C# language, refer to Microsoft C# by Joyce Farrell, published by Course Technology/Thomson Learning. Note

LOGIC

AND

DEBUGGING

Each high-level programming language, including Visual C++, has its own syntax, or rules of the language. All languages have a specific, limited vocabulary and a specific set of rules for using that vocabulary. For example, you might use the commands print or write to produce output to the screen.To create a program, you must understand a given programming language’s syntax. To write a program, you must also understand computer-programming logic. The logic underlying any program involves executing the various parts of the program in the correct order to produce the desired results. For example, although you know how to drive a car well, you may not reach your destination if you do not follow the correct route. Similarly, you might be able to use a programming language’s syntax correctly, but be unable to execute a logically constructed, workable program. Examples of logical errors include multiplying two values when you meant to divide them, or producing output prior to obtaining the appropriate input.The following C++ code contains another example of a logic error: int count = 0; while (count