Free Essay

C++Language Compasion

In:

Submitted By dzz18
Words 1609
Pages 7
++Starting Out with Programming Logic and Design, 3rd Edition
By Tony Gaddis

C++ Language Companion or

Copyright © 2013 Pearson Education, Inc.

Table of Contents
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Introduction 3 Introduction to Computers and Programming 4 Input, Processing, and Output 9 Functions 19 Decision Structures and Boolean Logic 27 Repetition Structures 41 Value-Returning Functions 49 Input Validation 59 Arrays 61 Sorting and Searching Arrays 72 Files 77 Menu-Driven Programs 86 Text Processing 89 Recursion 95 Object-Oriented Programming 97

Page 2

Introduction
Welcome to the C++ Language Companion for Starting Out with Programming Logic and Design, 3rd Edition, by Tony Gaddis. You can use this guide as a reference for the C++ Programming Language as you work through the textbook. Each chapter in this guide corresponds to the same numbered chapter in the textbook. As you work through a chapter in the textbook, you can refer to the corresponding chapter in this guide to see how the chapter's topics are implemented in the C++ programming language. In this book you will also find C++ versions of many of the pseudocode programs that are presented in the textbook. Note: This booklet does not have a chapter corresponding to Chapter 15 of your textbook because C++ does not provide a GUI programming library.

Page 3

Chapter 1
This chapter accompanies Chapter 1 of Starting Out with Programming Logic and Design, 3rd Edition

Introduction to Computers and Programming
A Brief History of C++
The C++ programming language was based on the C programming language. C was created in 1972 by Dennis Ritchie at Bell Laboratories for writing system software. System software controls the operation of a computer. For example, an operating system like Windows, Linux, or Mac OS is system software. Because system software must be efficient and fast, the C programming language was designed as a high performance language. The C++ language was created by Bjarne Stroustrup at Bell Laboratories in the early 1980s, as an extension of the C language. C++ retains the speed and efficiency of C, and adds numerous modern features that make it a good choice for developing large applications. Today, many commercial software applications are written in C++.

The Core Language and Libraries
The C++ language consists of two parts: The core language and the standard library. The core language is the set of key words shown in Table 1-1. Each of the key words in the table has a specific meaning and cannot be used for any other purpose. These key words allow a program to perform essential operations, but they do not perform input, output, or other complex procedures. For example, there are no key words in the core language for displaying output on the screen or reading input from the keyboard. To perform these types of operations you use the standard library. The standard library is a collection of prewritten code for performing common operations that are beyond those performed by the core language. In addition to input and output, the standard library provides code for performing complex mathematical operations, writing data to files, and other useful tasks.

Writing and Compiling a C++ Program
When a C++ program is written, it must be typed into the computer and saved to a file. A text editor, which is similar to a word processing program, is used for this task. The C++ programming statements written by the programmer are called source code, and the file they are saved in is called a source file. After the programmer saves the source code to a file, he or she runs the C++ compiler. A compiler is a program that translates source code into an executable form. During the translation process, the compiler uncovers any syntax errors that may be in the program. Syntax errors are mistakes that the programmer has made that violate the rules of the C++ programming language. These errors must be corrected before the compiler can successfully translate the source code. If the program is free of syntax errors, the compiler stores the translated machine language instructions, which are called object code, in an object file.

Page 4

Although an object file contains machine language instructions, it is not a complete program because it does not contain any code that the program needs from the standard library. Another program, known as the linker, combines the object file with the necessary library routines. Once the linker has finished with this step, an executable file is created. The executable file contains machine language instructions, or executable code, and is ready to run on the computer. Figure 1-1 illustrates the process of compiling and linking a C++ program. Table 1-1: Key words in the C++ language asm catch continue dynamic_cast false if namespace public signed switch try unsigned wchar_t auto char default else float inline new register sizeof template typedef using while break class delete enum for int operator reinterpret_cast static this typeid virtual bool const do explicit friend long private return static_cast throw typename void case const_cast double extern goto mutable protected short struct true union volatile

Page 5

Figure 1-1 Compiling and Linking a C++ Program

C++ Source Code File

Compiler

Object File

Linker

Executable File

Standard Library

The Parts of a C++ Program
As you work through this book, all of the C++ programs that you will write will contain the following code: #include using namespace std; int main() { return 0; } You can think of this as a skeleton program. As it is, it does absolutely nothing. But you can add additional code to this program to make it perform an operation. Let’s take a closer look at the parts of the skeleton program. Note: At this point, it is not critical that you understand everything about the skeleton program. The description that follows is meant to demystify the code, at least a little. Because you are a beginning programmer, you should expect that some of the following concepts will be unclear. As you dig deeper into the C++ language, you will understand these concepts. So, don't despair! Your journey is just beginning. The first line reads: #include This is called an include directive. It causes the contents of a file named iostream to be included in the program. The iostream file contains prewritten code that allows a C++ program to display output on the screen and read input from the keyboard. The next line reads : using namespace std;

Page 6

A program usually contains several items that have names. C++ uses namespaces to organize the names of program entities. The statement using namespace std; declares that the program will be accessing entities whose names are part of a namespace called std. The reason the program needs access to the std namespace is because every name created by the iostream file is part of that namespace. In order for a program to use the entities in iostream, it must have access to the std namespace. (Notice that the statement ends with a semicolon. More about that in a moment.) The following code appears next: int main() { return 0; } This is called a function. You will learn a great deal about functions later, but for now, you simply need to know that a function is a group of programming statements that collectively has a name. The name of this function is main. Every C++ program must have a function named main, which serves as the program's starting point. Notice that a set of curly braces appears below the line that reads int main(). The purpose of these braces is to enclose the statements that are in the main function. In this particular program, the main function contains only one statement, which is: return 0; This statement returns the number 0 to the operating system when the program finishes executing. When you write your first C++ programs, you will write other statements inside the main function's curly braces, as indicated in Figure 1-2. NOTE: C++ is a case-sensitive language. That means it regards uppercase letters as being entirely different characters than their lowercase counterparts. In C++, the name of the main function must be written in all lowercase letters. C++ doesn’t see Main the same as main, or INT the same as int. This is true for all the C++ key words. Figure 1-2 The skeleton program #include using namespace std; int main() { return 0; }

You will write other C++ statements in this area.

Page 7

Semicolons In C++, a complete programming statement ends with a semicolon. You'll notice that some lines of code in the skeleton program do not end with a semicolon, however. For example, the include directive does not end with a semicolon because, technically speaking, directives are not statements. The main function header does not end with a semicolon because it marks the beginning of a function. Also, the curly braces are not followed by a semicolon because they are not statements because they form a container that holds statements. If this is confusing, don't despair! As you practice writing C++ programs more and more, you will develop an intuitive understanding of the difference between statements and lines of code that are not considered statements.

Page 8

Chapter 2
This chapter accompanies Chapter 2 of Starting Out with Programming Logic and Design, 3rd Edition

Input, Processing, and Output
Displaying Screen Output
To display output on the screen in C++ you write a cout statement (pronounced see out). A cout statement begins with the word cout, followed by the

Similar Documents

Free Essay

1000 Words in English

...a bit a couple a few a little adj, pron a lot (of) (tb lots (of)) a, an art indet a.m. (USA tb A.M.) abrev abandon v abandoned adj ability n able adj about adv, prep un poco un par unos cuantos algo / un poco mucho un/a Ante meridiam abandonar abandonado habilidad poder hacer algo affect v más o menos, hacia, por aquí / affection n prep: sobre algo above prep, adv por encima, más arriba / adv: afford v arriba afraid adj abroad adv en el extranjero after adv, prep, absence n ausencia conj absent adj ausente afternoon n absolute adj absoluto afterwards (USA absolutely adv absolutamente tb afterward) adv absorb v absorber again adv abuse n, v abusar, abuso against prep academic adj académico age n accent n acento aged adj accept v aceptar agency n acceptable adj aceptable agent n access n acceso aggressive adj accident n accidente ago adv accidental adj accidental agree v accidentally adv accidentalmente accommodation alojamiento, espacio, plazas agreement n ahead adv n accompany v acompañar aid n, v according to según algo aim n, v prep account n, v cuenta, relato / considerar air n aircraft n accurate adj preciso airport n accurately adv con precisión alarm n, v accuse v acusar a alguien alarmed adj achieve v lograr alarming adj achievement n logro alcohol n acid n acido alcoholic adj, n acknowledge v reconocer/agradecer/enterarse alive adj all adj, pron, acquire v adquirir adv across adv, a través de / all right adj, prep adv, interj act n, v acto, ley / actuar allied adj...

Words: 14391 - Pages: 58