Immediate download Programming with Visual C Concepts and Projects 1st Edition James Allert ebooks 2024
Immediate download Programming with Visual C Concepts and Projects 1st Edition James Allert ebooks 2024
https://ebookultra.com
https://ebookultra.com/download/programming-with-
visual-c-concepts-and-projects-1st-edition-james-
allert/
https://ebookultra.com/download/visual-c-net-programming-1st-edition-
harold-davis/
ebookultra.com
https://ebookultra.com/download/concepts-in-programming-languages-1st-
edition-john-c-mitchell/
ebookultra.com
https://ebookultra.com/download/programming-concepts-in-c-2nd-edition-
robert-burns/
ebookultra.com
https://ebookultra.com/download/windows-game-programming-with-visual-
basic-and-directx-1st-edition-freeze/
ebookultra.com
Programming the Web with Visual Basic NET 1st Edition Lynn
Torkelson
https://ebookultra.com/download/programming-the-web-with-visual-basic-
net-1st-edition-lynn-torkelson/
ebookultra.com
https://ebookultra.com/download/programming-with-microsoft-visual-
basic-2012-6th-edition-diane-zak/
ebookultra.com
https://ebookultra.com/download/programming-with-c-zambak-1st-edition-
osman-ay/
ebookultra.com
https://ebookultra.com/download/an-introduction-to-programming-using-
visual-basic-2008-with-visual-studio-expression-edition-dvd-schneider/
ebookultra.com
JAMES ALLERT
Australia Brazil Japan Korea Mexico Singapore Spain United Kingdom United States
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Programming with Visual C++: Concepts ª2009 Course Technology, a part of Cengage Learning
and Projects
ALL RIGHTS RESERVED. No part of this work covered by the
by James Allert copyright hereon may be reproduced or used in any form or
Senior Product Manager: Alyssa Pratt by any means—graphic, electronic, or mechanical, including
photocopying, recording, taping, Web distribution, or
Acquisitions Editor: Amy Jollymore information storage and retrieval systems—without the written
Development Editor: Ann Shaffer permission of the publisher.
Content Project Manager: Erin Dowler
Editorial Assistant: Patrick Frank For product information and technology assistance, contact us at
Cengage Learning Academic Resource Center, 1-800-423-0563
Marketing Manager: Bryant Chrzan For permission to use material from this text or product, submit
Print Buyer: Julio Esperas all requests online at www.cengage.com/permissions
Cover Designer: Lisa Kuhn, Curio Press, LLC Further permissions questions can be emailed to
permissionrequest@cengage.com
Compositor: Integra
Art Director: Marissa Falco
ª2009 Cengage Learning. All Rights Reserved.
Cover Photo: Jupiterimages Corporation
ISBN-13: 9781423901860
Copyeditor: Mark Goodin
Proofreader: Brandy Lilly ISBN-10: 1-4239-0186-X
Indexer: Michael Brackney Course Technology Cengage Learning
25 Thomson Place
Boston, MA 02210
USA
Or find us on the World Wide Web at: www.course.com.
Cengage Learning products are represented in Canada by
Nelson Education, Ltd.
Disclaimer
Course Technology Cengage Learning reserves the right to
revise this publication and make changes from time to time in
its content without notice.
The programs in this book are for instructional purposes only.
They have been tested with care, but are not guaranteed for
any particular intent beyond educational purposes. The
authors and the publisher do not offer any warranties or
representations, nor do they accept any liabilities with respect
to the programs.
Printed in Canada
1 2 3 4 5 6 7 12 11 10 09 08
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
TO
Deb
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
BRIEF CONTENTS
PREFACE xviii
1. Introduction 1
4. Selection 163
5. Repetition 219
6. Methods 269
7. Arrays 327
9. Sorting 417
INDEX 715
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
TABLE OF CONTENTS
Preface xviii
INTRODUCTION 1
1 Object-Oriented Programming 2
Hardware Fundamentals 6
Software Fundamentals 11
Types of Errors 21
Debugging a Program 50
On Your Own 53
Quick Review 57
Terms to Know 60
Exercises 63
Programming Exercises 69
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
viii | Programming with Visual C++: Concepts and Projects
Data Input 84
The TryParse() Method 85
Arithmetic Operations 86
Operator Precedence in Arithmetic Expressions 88
Arithmetic Operators and Strings 90
Shorthand Assignment 92
The Math Library 93
Data Output 94
The ToString()Method 94
Exercises 111
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Table of Contents | ix
Exercises 156
SELECTION 163
4 Control Structures 164
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
x | Programming with Visual C++: Concepts and Projects
Exercises 211
Projects 214
REPETITION 219
5 User-Controlled Repetition 220
Accumulating a Sum and Counting 225
Instance Variables 226
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Table of Contents | xi
Exercises 263
METHODS 269
6 Methods 270
Exercises 315
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xii | Programming with Visual C++: Concepts and Projects
ARRAYS 327
7 Arrays 328
Exercises 363
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Table of Contents | xiii
Testing 403
Exercises 406
SORTING 417
9 Exchanging Data Values in an Array 418
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xiv | Programming with Visual C++: Concepts and Projects
Exercises 451
RECURSION 459
10 Factorial Numbers 460
Recursion 462
Analysis 496
Exercises 500
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Table of Contents | xv
POINTERS 511
11 Introduction 512
Exercises 565
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xvi | Programming with Visual C++: Concepts and Projects
Exercises 626
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Table of Contents | xvii
Testing 671
Exercises 673
Appendix A 681
Appendix B 693
Appendix C 703
INDEX 715
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
PREFACE TO THE FIRST EDITION
This is the programmer’s friend, the black box console application. As a college student
thirty years ago, I learned how to program black boxes like this, and I have been teaching
students to do the same ever since. At least that was the case until recently, when I began
cutting back.
It is not that black boxes are bad. In fact one could make the point that they are the most
efficient way to learn programming. No fancy graphical user interface (GUI) to confuse
us. No nonstandard terminology and syntax. Just good, old-fashioned (and I do mean
old-fashioned) C++ coding.
Still, the argument that less is more, the minimalist philosophy of computer science
education, doesn’t seem to fit today. Every computer program people use, from the
Internet to the Xbox, is an interactive visual experience with a robust graphics environ-
ment. Compared to them, the black box console application is uninspiring at best. These
programs look nothing like the programs most of us actually use. ‘‘What is the point?
Why are we doing this?’’ I have been asked on more than one occasion. The traditional
answer of course is, ‘‘We are doing this because GUI programming is too difficult or
distracting for beginners. You can learn those skills in future courses.’’
However, the argument about waiting until the second or third year of programming
before learning how to create a user interface is less compelling than it once was. Visual
Basic students for example (usually novice, non-computer science majors) routinely create
elaborate GUI programs in introductory courses and have done so for years, while
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Preface to the First Edition | xix
Computer Science I (CS1) students (computer science majors) make black boxes in Java
or C++.
This book is built around the philosophy that computer programs should be inter-
esting and fun to develop at the same time that they challenge you to learn new
skills. Microsoft Visual C++ 2008 makes the construction of graphical interfaces
quick and easy. It opens up the world of Windows applications to students in a
way that still allows an introductory course to focus on the core conceptual issues.
CS1 can now go GUI.
This book is the outgrowth of several years of experimenting with Visual Studio
and Visual C++ in CS1. I thought it would be tricky integrating a GUI approach
into the course, but it was not a formidable task. No prior programming experience
is assumed. There is some nonstandard Microsoft Visual C++ terminology, but the
trade-off is that it greatly expands the programming possibilities and helps create an
educational experience that can be more enjoyable for the student and the professor.
Concepts
Each chapter starts with a discussion of important concepts and programming commands.
These are the fundamental ideas that allow you to advance your programming knowledge
and are intended to have broad application. The concepts section is not intended to
be an exhaustive reference work, but targets selected concepts that will apply to
the chapter’s main project. Each project incorporates both general computer science
concepts and implementation techniques specific to the Windows platform. The tables in
Appendix A show which concepts are covered in each chapter.
Tutorials
The second half of each chapter centers on a tutorial that demonstrates how to construct a
program that implements the concepts learned earlier. A consistent project development
strategy is used for each project. The tutorials guide you through the process required to
complete a project. Students are encouraged to experiment with their project once it is
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xx | Programming with Visual C++: Concepts and Projects
complete, to teach themselves more by venturing into new territory. An effective way to
advance learning is to be creative and to explore new possibilities using something you
already know.
On Your Own
The On Your Own section is intended to allow you to expand the capabilities of the
tutorial, using knowledge you have gained from the chapter to guide you.
Exercises
At the end of each chapter are a series of short exercises that cover both the conceptual
foundation of that project and its implementation.
Quick Review
This section lists the major concepts covered in the chapter. It is a great tool to use when
studying for an exam or just reviewing your comprehension of what you read.
Terms to Know
The important terms in each chapter appear in boldface in the text. In addition, a
complete list of them is provided at the end of the chapter. An expanded glossary
accompanies the book and is available online at www.course.com.
Programming Exercises
The exercises at the end of the chapters challenge you to build your programming
skills. You are encouraged to create at least one project from the end of each chapter.
These projects encourage you to build on your knowledge to create new applications
without telling you what to do at each step along the way. Creating your own
projects and working along with a tutorial helps solidify both the important ideas and
your knowledge of the specific Visual C++ instructions used to illustrate them. It is
quite satisfying to know that you created a program, from scratch, with minimal
assistance.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Preface to the First Edition | xxi
Chapter 1
Chapter 1 introduces computers and computer programming. Topics include the funda-
mental components of a computer, hardware and software concepts, and the basics of
program compilation. This overview of essential terms is important to understand the
explanations in later chapters. The tutorial in Chapter 1 can be completed quickly and
easily in one 50-minute lab session.
Chapter 2
Chapter 2 presents a simple programming task involving arithmetic expressions. With the
skills acquired from the completion of the tutorial in Chapter 1, students should be able
to work on the tutorial in Chapter 2 on their own.
Chapter 3
Chapter 3 centers on integral data and the arithmetic operators integer division (/) and
mod (%). The tutorial makes use of material related to both the binary number system as
well as integer division (/) and mod (%). The instructor can consider Chapter 3 optional if
he or she prefers to move more rapidly into control structures.
Chapter 4
Chapter 4 covers selection structures in detail. The tutorial implements a vacation
reservation system similar to those used on many travel Web sites.
Chapter 5
Chapter 5 covers repetition structures. The project for this chapter generates random
numbers and processes them in a variety of ways in the context of a manufacturing
program. The more end-of-chapter programming exercises a student does in Chapters 4
and 5, the more comfortable the student will be going on to later chapters.
Chapter 6
Chapter 6 introduces the concept of a method. It also introduces graphical program-
ming using the special drawing tools to create the image of planets revolving around
a sun.
Chapter 7
Chapter 7 introduces the concept of an array and uses the data structure to construct a
color-coded student seating chart that shows the different achievement levels.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xxii | Programming with Visual C++: Concepts and Projects
Chapter 8
Chapter 8 implements a search program that demonstrates two kinds of search techni-
ques, sequential and binary. The search program is designed to locate movie names in a
video store inventory.
Chapter 9
Chapter 9 experiments with various sorting techniques and draws randomly generated
data values, in unsorted and sorted order, as a vertical bar chart.
Chapter 10
Chapter 10 explores the topic of recursion. In this chapter, students create a fractal image
and then use menu controls to change its look.
Chapter 11
Chapter 11 introduces pointers. An array of pointers is used to sort data in another array
without moving data in the second array.
Chapter 12
Chapter 12 draws linked list by requiring the programmer to create the class definition for
a node and then use that definition to create Node objects and then link them.
Chapter 13
Chapter 13 requires the student to create several class definitions and add a variety of
methods to each. In the end, a two-dimensional array is created in which a mouse runs
around looking for a piece of cheese. This project can be expanded to direct the mouse to
the cheese using walls within the maze.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Preface to the First Edition | xxiii
This Figure shows some of the tutorial programs and programming exercises the student
creates in this book.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xxiv | Programming with Visual C++: Concepts and Projects
Chapter 6 introduces instance methods and graphics objects. Both are important in later
chapters. Chapter 6 is important because it emphasizes the development of instance
methods and sets up the use of graphical programming throughout the remainder of
the text. The Chapter 6 tutorial is one of the most interesting in the book and a student
favorite; it assumes familiarity with both the sine and cosine concepts.
Chapter 7 (arrays), Chapter 8 (binary search), and Chapter 9 (sorting) are topics com-
monly addressed by CS1 textbooks. Chapter 10 (recursion), however, is a topic often
reserved for courses beyond CS1. Like Chapter 6, Chapter 10 is a student favorite, but
the topic is challenging. Skipping this chapter will not adversely affect the student’s
success with Chapters 11, 12, or 13.
Chapter 11 (pointers) is a traditional C++ topic. The tutorial is an academic exercise
designed to demonstrate pointer arrays. Chapter 12 is a way of visualizing a linked list. It
is not a fully developed abstract data type (common in CS2 courses). The project is
used as a transition from pointers to the larger, object-oriented concepts presented in
Chapter 13.
Instructors who want to drill down into C++ basics can skip Chapters 3, 10, and even 12,
substituting week-long Programming Exercise projects instead. On the other hand,
instructors who want to experience a broader range of topics and challenges can follow
the book chapter by chapter.
I have found that teaching this material is enjoyable and stimulating. I hope you will too.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
FEATURES OF THE BOOK
Over 425
color
illustrations
and diagrams
explain key
concepts.
Diagrams are
often linked to
code
examples.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Numbered algorithms
and examples are
used to show how
key concepts are
implemented.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Previews of the
interface provide a
guide to the
construction of the
tutorial program
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Screen captures
are used to show
the student exactly
what is required
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
The On Your Own
section at the end of
each tutorial
challenges students
to demonstrate their
understanding of
key concepts
covered in the
tutorial.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Quick Review
sections at the
end of each
chapter
summarize key
points
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Exercises provide
students with short skill
and knowledge tests.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Programming Exercises
provide additional ‘‘on
your own’’ skill building.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
SUPPLEMENTAL RESOURCES
The following supplemental materials are available when this book is used in a classroom
setting. All instructor teaching tools outlined below are provided to the instructor on a single
CD-ROM.
ExamView
This textbook is accompanied by ExamView, a powerful testing software package that allows
instructors to create and administer printed, computer (LAN-based), and Internet exams.
ExamView includes hundreds of questions that correspond to the topics covered in this text,
enabling students to generate detailed study guides that include page references for further
review. These computer-based and Internet testing components allow students to take exams
at their computers, and save the instructor time because each exam is graded automatically.
PowerPoint Presentations
This book comes with Microsoft PowerPoint slides for each chapter. These are included as a
teaching aid for classroom presentations, either to make available to students on the network
for chapter review or to be printed for classroom distribution. Instructors can add their own
slides for additional topics that they introduce to the class.
Distance Learning
Course Technology is proud to offer online content in WebCT and Blackboard to provide
the most complete and dynamic learning experience possible. For more information on how
to bring distance learning to your course, contact your local Cengage Course Technology
sales representative.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
xxxiv | Programming with Visual C++: Concepts and Projects
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
ACKNOWLEDGMENTS
I want to thank many people for their role in the production of this volume. I received great
encouragement from mentors, colleagues, and others at the University of Minnesota,
Duluth—especially Steve Holtz and others in the Department of Computer Science. I would
like to especially thank the Archibald Bush Foundation for its efforts to promote the study of
teaching and learning at the University of Minnesota and to the many faculty members in the
Bush group who provided advice, support, and encouragement.
Many thanks to the following reviewers, who provided thorough, precise, and insightful
suggestions: Kevan Croteau, Francis Marion University; Brian English, Henderson State
University; Catherine Leach, Henderson State University; Heh Miao, Tennessee State Uni-
versity; Katherine Salch, Illinois Central College; Jijun Tang, University of South Carolina;
Vitalie Volosin, Troy University; and Emily Wenk, Penn State York. I would like to extend a
special thanks to Debbie Sorrentino, Niagara County Community College, and Victor
Shtern, Boston University, for their extremely detailed and rigorous critiques of the manu-
scripts. I have benefited immensely from the work of this fine group of reviewers and
endeavored to incorporate much of their wisdom into this book.
I have learned that writing a book is a monumental team effort and would like to thank all of
those at Cengage Course Technology who made this book possible. Thanks especially to Amy
Jollymore (Acquisitions Editor) for initiating the project and overseeing it and to Alyssa Pratt
(Senior Product Manager) for actively coordinating things from the top down. This book would
never have been possible without the tireless work of Ann Shaffer, Development Editor, who
was always supportive and cheerful. A special thanks to you, Ann. I also gratefully acknowledge
the work of Erin Dowler, Content Project Manager, and Tintu Thomas, of Integra, for their
many labors in producing this volume. Thanks, also, to Nicole Ashton and John Bosco, whose
keen eyes for detail never seemed to miss anything as they tested the code for each chapter.
Finally, thanks to Mark Goodin, who served as our able and always helpful copy editor.
This book is dedicated to the people who have helped me the most. First, to my wife Debbie,
who patiently waited for me to get through this and never doubted that I would. Second, to
my parents, Don and Maxine Allert, for their many years of love and caring. Finally, to my
many past and present students, who never stop asking questions and creating interesting new
solutions to my perfectly straightforward homework problems.
Thank you all for your contributions and support.
Jim Allert
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
CHAPTER
1
I NTRODUCTION
IN THIS CHAPTER, YOU WILL:
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
2 | Chapter 1: Introduction
This chapter lays the foundation you need to write your own computer programs. It
addresses basic computer science terminology and concepts, as well as the specifics of
how to create your first program using Microsoft Visual C++ 2008. Although
computer programming may sound like a complicated undertaking, modern tools
make it easy for almost anyone to do it. This chapter will introduce you to some of
these tools. Very soon, you should be able to take what you have learned here and
create fun and useful programs that address your own interests.
Object-Oriented Programming
Visual C++ is a programming language developed by Microsoft for computers running
Windows. It is based on the standard C++ programming language. It also includes a
wide assortment of features that allow you to create visual elements with which users can
interact.
Visual C++ is one of a group of languages classified as object-oriented. Generally
speaking, an object is something with identifiable features that you may be able to
manipulate. In a computer program, an object could be something you see on the
screen, such as a button the user clicks, or it could be something within the program
that you don’t see on the screen, such as a list of student exam scores used for
computing a grade point average. An object-oriented programming language is
one in which objects are created from a specific list of instructions. These objects can
interact with the user or with other objects. Objects usually have a set of components
consisting of attributes and methods. An attribute is a characteristic feature of an
object. Often, attributes are data items, such as the size (width and height) of an object.
A method is an operation the object can perform, such as averaging a group of
numbers or changing its width and height.
All objects are constructed from a specific set of instructions, or design specifications.
Taken as a whole, the design specifications for an object are called its class definition.
Class definitions specify the object’s attributes and methods. The programs you write
in Visual C++ are really just class definitions, from which a rather large object, your
program interface, is created. The program interface is what you see when your
program runs. An interface that contains visual elements for the user to interact
with (such as buttons and menus) is called a graphical user interface (GUI). In
Visual C++, there is a more general term for the GUI object, however; it is simply
called a form.
To get a better handle on the concepts related to object-oriented programming, let’s
consider a more general example. Imagine you want to create a robot. Further,
imagine that, after you create the robot, you want to make it possible for other
people to make an identical one. You would start by devising a plan that included a
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Object-Oriented Programming | 3
rough sketch of the final product. You could also make a list of attributes this robot
should possess and the methods (operations) you would expect it to carry out, as 1
shown in Figure 1-1.
Attributes Methods
Antenna Receive
instructions
FIGURE 1-1 Rough sketch of a robot and list of attributes and methods
You could then use the sketch and the list to build the robot. Building the robot is, of
course, more complicated than what is described here, but let’s assume that you
eventually get it to work. To accomplish the second part of your plan—making it
possible for other people to build an identical robot—you would need to develop a set
of design specifications that list the required parts (for example, a box for the robot’s
body, rubber treads for the feet, etc.). The design specifications would also have to
include illustrations explaining exactly how to put the robot together. Figure 1-2 shows
a crude set of design specifications for a robot.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
4 | Chapter 1: Introduction
Feet
Rubber
treads
Circuit Board
Feet and arms should be wired to circuit board
Attach arms
here
1ft
Aluminum
housing
Cut out
eyes and mouth
Rubber
Feet go claws
here
The goal of your design specifications would be to allow anyone to create a real,
functioning robot object. Assuming people followed your specifications exactly, they
would produce a robot that looks and behaves just like your original.
Creating an object-oriented computer program is similar to designing a robot. First,
you make a rough sketch of what it will look like on the screen—that is, you sketch
the interface. As you design the interface, you must also list the attributes you feel
are important, and the methods (operations) you would like the program to
perform. Figure 1-3 shows a sample sketch as well as a list of attributes and methods
for a program.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Object-Oriented Programming | 5
1
Hello World!
Attributes Methods
green
textBox1 Display text
button1 Change colors to green
red
button2 Change colors to red
blue
FIGURE 1-3 Program interface and list of attributes and methods (operations)
After you create the sketch and the list of attributes like those shown in Figure 1-3, you
need to create the design specifications—or, class definition. A class definition defines the
size, location, and functionality of every control (such as buttons, textboxes or other
visual elements) placed on the form. Among other things, the class definition for a control
specifies what that control should do in response to certain events. For example, it
specifies what a button should do when the user clicks it. The part of the class definition
that tells the control what to do in response to an event is called its event-handler.
In Visual C++, as you create the interface for a program, a good part of your class
definition is automatically written for you behind the scenes by a program known as the
Windows Forms Designer. It automatically generates Visual C++ code to define many
of the properties of your controls. However, you, the programmer, must write the
instructions that go into the event-handlers. You are the one who determines how the
program will respond when the user starts interacting with the controls on the form. You
need to write the Visual C++ instructions that tell each control what to do.
When your program runs, the controls wait for certain events to occur before respond-
ing. An example of a common event is a click on a button. When the user clicks a button,
the program responds by running the Visual C++ commands, called Visual C++ code,
in the button’s click event-handler.
In this chapter’s tutorial, you will create a simple program. First, you will create the
program interface and place commonly used controls (that is, a button and a textbox) on
it. You will then write short Visual C++ instructions for the button so that when it is
clicked your program will respond by displaying a message in the textbox. Additional
tasks provided at the end of the chapter allow you to be more creative by adding
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
6 | Chapter 1: Introduction
instructions to change the color of various aspects of the interface. Before getting to these
exercises, however, we should review some basic concepts that explain what a computer
is and how it works. A firm grounding in these concepts will allow you to more fully
appreciate the programs you create.
Hardware Fundamentals
Most computers have a similar set of components, which are typically grouped into five
essential categories:
• input
• output
• processing
• memory
• storage
Input and output are fundamental operations in every computer. You are already familiar
with numerous input devices, such as a mouse, keyboard, microphone, scanner, and video
camera. Any device with the primary purpose of transmitting data to the internal memory
of a computer is classified as an input device. Any device with the primary purpose of
receiving data from a computer is considered an output device. Common output devices
include monitors and printers.
Processing is carried out by the central processing unit (CPU). Among other things, the
CPU is responsible for logical, numeric, and scheduling tasks. Modern desktop and laptop
computers carry out these tasks using microprocessor chips. A modern CPU has a complex
structure, but it is possible, to identify elements that all CPUs have in common:
• The controller unit is in charge of the transfer of data and instructions to
and from memory. It fetches instructions and decodes them so they can
be executed.
• The arithmetic logic unit (ALU) carries out logical tasks (such as compar-
isons) and the numerical tasks most often associated with computation.
• Other components include registers, which temporarily store data while
processing is taking place, and instruction position indicators, which
keep track of which tasks are up next.
Memory (often called primary storage) is responsible for storing data and instructions
when a computer is running. A computer’s memory utilizes microchips. While these are
not part of the CPU, they work in tandem with it. Most computers use a type of memory
known as random access memory (RAM), which requires the presence of an electric
current to sustain itself. When power is turned off, its contents vanish. For this reason,
RAM is often referred to as being ‘‘volatile.’’ Read-only memory (ROM) is more
expensive, but stores information for long periods of time even when the computer is
turned off.
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Hardware Fundamentals | 7
Storage devices (often called secondary storage) are used to save programs and data
cheaply and efficiently outside of the computer’s main memory. Storage devices have 1
both input and output capabilities, but their primary purpose is to save information. This
includes devices such as magnetic disks (that is, hard and floppy drives), optical disks (such
as CDs and DVDs), and flash memory devices (such as USB drives).
One type of storage device, flash memory, deserves special consideration. Flash memory
is a form of storage that is chip-based, much like RAM, but is not volatile. It stores data
for long periods of time without the constant presence of an electric current. Flash
memory devices have largely replaced magnetic disks as convenient ways of storing and
transporting files. You may find them to be the best method of saving and transporting
your projects; however, you will find that it is faster to develop your projects when they are
stored on the computer’s hard drive.
CPU
Storage controller
(CD/DVD,
Hard drive) Processing
ALU
other
components
Memory
both input
Input Output
and output
devices devices
(external storage,
(keyboard, mouse) (monitor, printer)
communications)
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
8 | Chapter 1: Introduction
Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
Exploring the Variety of Random
Documents with Different Content
obtain from said physician the means wherewith to commit suicide;
for should any poison be furnished or administered by physicians,
under such circumstances, the course of justice would be greatly
obstructed. Should any physician be guilty of this offence, he shall
be liable to punishment for the same.
VIII. Jews shall not Divide their Food into Clean and Unclean,
According to their Custom.
The blessed apostle Paul said, “To the pure all things are pure,”
but nothing is pure to those who are defiled, because they are
unbelievers; and, for this reason, the execrable life of the Jews and
the vileness of their horrible belief, which is more foul than any other
detestable error, must be destroyed and cast out. Therefore, no Jew
shall make a distinction between food which is clean and unclean, as
established by the customs and traditions of his ancient rites. No one
shall perversely refuse to eat food of any kind, whose condition is
proved to be good. No one shall reject one article of food, and
accept another, unless the distinction be such as is considered
salutary and proper by all Christians. Anyone detected in the
violation of this law shall be subjected to the punishment instituted
for the same.