Chapter 1 of 'C++ How to Program, 10/e' introduces C++ as a powerful programming language suitable for both beginners and experienced programmers. It covers the evolution of programming languages, the phases of C++ program development, and the use of integrated development environments (IDEs) such as Visual Studio. The chapter concludes with a practical example of creating and running a simple C++ application, the 'Guess-the-number' game.
Chapter 1 of 'C++ How to Program, 10/e' introduces C++ as a powerful programming language suitable for both beginners and experienced programmers. It covers the evolution of programming languages, the phases of C++ program development, and the use of integrated development environments (IDEs) such as Visual Studio. The chapter concludes with a practical example of creating and running a simple C++ application, the 'Guess-the-number' game.
C++—a powerful computer programming language that’s appropriate for technically oriented people with little or no programming experience, and for experienced programmers to use in building substantial information systems. You’ll write instructions commanding computers to perform those kinds of tasks. Software (i.e., the instructions you write) controls hardware (i.e., computers). You’ll learn object-oriented programming—today’s key programming methodology. You’ll create many software objects in the real world.
C++ is one of today’s most popular software development languages. C++ is one of today’s most popular software development languages. C++11 and C++14 are the latest versions standardized through the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC).
Programmers write instructions in various programming languages, some directly understandable by computers and others requiring intermediate translation steps. These may be divided into three general types: ◦ Machine languages ◦ Assembly languages ◦ High-level languages
Machine Languages Any computer can directly understand only its own machine language (also called machine code), defined by its hardware architecture. Machine languages generally consist of numbers (ultimately reduced to 1s and 0s). Such languages are cumbersome for humans.
Assembly Languages English-like abbreviations to represent elementary operations. These abbreviations formed the basis of assembly languages. Translator programs called assemblers were developed to convert early assembly-language programs to machine language.
High-Level Languages To speed up the programming process further, high-level languages were developed in which single statements could be written to accomplish substantial tasks. Translator programs called compilers convert high-level language programs into machine language. Allow you to write instructions that look more like everyday English and contain commonly used mathematical expressions.
Compiling a high-level language program into machine language can take a considerable amount of computer time. Interpreter programs were developed to execute high-level language programs directly (without the need for compilation), although more slowly than compiled programs. Scripting languages such as the popular web languages JavaScript and PHP are processed by interpreters.
C was implemented in 1972 by Dennis Ritchie at Bell Laboratories. ◦ Initially became widely known as the UNIX operating system’s development language. ◦ Today, most of the code for general-purpose operating systems is written in C or C++. C++ evolved from C, which is available for most computers and is hardware independent.
C11 ◦ Latest ANSI standard for the language. ◦ Developed to evolve the C language to keep pace with increasingly powerful hardware and ever more demanding user requirements. ◦ Makes C more consistent with C++. C++, an extension of C, was developed by Bjarne Stroustrup in 1979 at Bell Laboratories. C++ provides a number of features that “spruce up” the C language. C++ also provides capabilities for object-oriented programming that were inspired by the Simula simulation programming language.
C++ Standard Library ◦ C++ programs consist of pieces called classes and functions. ◦ Most C++ programmers take advantage of the rich collections of classes and functions in the C++ Standard Library. ◦ Two parts to learning the C++ “world.” The C++ language itself (the core language), and How to use the classes and functions in the C++ Standard Library. ◦ Many special-purpose class libraries are supplied by independent software vendors.
C++ systems generally consist of three parts: a program development environment, the language and the C++ Standard Library. C++ programs typically go through six phases: edit, preprocess, compile, link, load and execute.
Phase 1 consists of editing a file with an editor program, normally known simply as an editor. ◦ Type a C++ program (source code) using the editor. ◦ Make any necessary corrections. ◦ Save the program. ◦ C++ source code filenames often end with the .cpp, .cxx, .cc or .C extensions, which indicate that a file contains C++ source code.
Linux editors: vi and emacs. You can also use a simple text editor, such as Notepad in Windows, to write your C++ code. integrated development environments (IDEs) ◦ Provide tools that support the software-development process, including editors for writing and editing programs and debuggers for locating logic errors—errors that cause programs to execute incorrectly.
In phase 2, you give the command to compile the program. ◦ A preprocessor program executes automatically before the compiler’s translation phase begins (so we call preprocessing Phase 2 and compiling Phase 3). ◦ The C++ preprocessor obeys commands called preprocessing directives, which indicate that certain manipulations are to be performed on the program before compilation. ◦ These manipulations usually include (copy into the program file) other text files to be compiled, and perform various text replacements.
Phase 4 is called linking. ◦ The object code produced by the C++ compiler typically contains “holes” due to these missing parts. ◦ A linker links the object code with the code for the missing functions to produce an executable program. ◦ If the program compiles and links correctly, an executable image is produced.
Phase 5 is called loading. ◦ Before a program can be executed, it must first be placed in memory. ◦ This is done by the loader, which takes the executable image from disk and transfers it to memory. ◦ Additional components from shared libraries that support the program are also loaded.
Phase 6: Execution ◦ Finally, the computer, under the control of its CPU, executes the program one instruction at a time. ◦ Some modern computer architectures often execute several instructions in parallel.
Problems That May Occur at Execution Time ◦ Programs might not work on the first try. ◦ Each of the preceding phases can fail because of various errors that we’ll discuss throughout this book. ◦ If this occurred, you’d have to return to the edit phase, make the necessary corrections and proceed through the remaining phases again to determine that the corrections fixed the problem(s). ◦ Most programs in C++ input or output data.
◦ Certain C++ functions take their input from cin (the standard input stream; pronounced “see-in”), which is normally the keyboard, but cin can be redirected to another device. ◦ Data is often output to cout (the standard output stream; pronounced “see-out”), which is normally the computer screen, but cout can be redirected to another device. ◦ When we say that a program prints a result, we normally mean that the result is displayed on a screen.
◦ Data may be output to other devices, such as disks, hardcopy printers or even transmitted over the Internet. ◦ There is also a standard error stream referred to as cerr. The cerr stream is used for displaying error messages.
In this section, you’ll compile, run and interact with your first C++ application. ◦ Guess-the-number game, which picks a number from 1 to 1000 and prompts you to guess it. ◦ If your guess is correct, the game ends. ◦ If your guess is not correct, the application indicates whether your guess is higher or lower than the correct number. There is no limit on the number of guesses you can make.
[Note: For this test drive only, we’ve modified this application from the exercise you’ll be asked to create in Chapter 6, Functions and an Introduction to Recursion. Normally this application randomly selects the correct answer as you execute the program. The modified application uses the same correct answer every time the program executes (though this may vary by compiler), so you can use the same guesses we use in this section and see the same results as we walk you through interacting with your first C++ application.]
In this section, you’ll run a C++ program on Windows using Microsoft Visual Studio 2015 Community Edition. There are several versions of Visual Studio available—on some versions, the options, menus and instructions we present might differ slightly. From this point forward, we’ll refer to Visual Studio 2015 Community Edition simply as “Visual Studio” or “the IDE.”
Step 1: Checking Your Setup ◦ It’s important to read this book’s Before You Begin section to make sure that you’ve installed Visual Studio and copied the book’s examples to your hard drive correctly.
Step 2: Launching Visual Studio ◦ Open Visual Studio from the Start menu. ◦ The IDE displays the Start Page (Fig. 1.12), which provides links for creating new programs, opening existing programs and learning about the IDE and various programming topics. ◦ Close this window for now by clicking the X in its tab—you can access this window any time by selecting View > Start Page. We use the > character to indicate selecting a menu item from a menu. For example, the notation File > Open indicates that you should select the Open menu item from the File menu.
Step 3: Creating a Project ◦ A project is a group of related files, such as the C++ source- code files that compose an application. ◦ Visual Studio organizes applications into projects and solutions, which contain one or more projects. ◦ Multiple-project solutions are used to create large-scale applications. ◦ Each application in this book will be a solution containing a single project.
Step 3: Creating a Project ◦ This book’s examples are Win32 Console Application projects that you’ll execute from the IDE. ◦ Select File > New > Project…. ◦ At the New Project dialog’s left side, select the category Installed > Templates > Visual C++ > Win32 (Fig. 1.13).
Step 3: Creating a Project ◦ In the New Project dialog’s middle section, select Win32 Console Application. ◦ Provide a name for your project in the Name field—we specified Guess Number—then click OK to display the Win32 Application Wizard window, then click Next > to display the Application Settings step. ◦ Configure the settings as shown in Fig. 1.14 to create a solution containing an empty project, then click Finish.
Step 3: Creating a Project ◦ At this point, the IDE opens the window in Fig. 1.15 and creates your project and places its folder in C:\Users\YourUserAccount\Documents\Visual Studio 2015\Projects ◦ This window displays editors as tabbed windows (one for each file) when you’re editing code. ◦ Also displayed is the Solution Explorer in which you can view and manage your application’s files. You’ll typically place each program’s code files in the Source Files folder. ◦ If the Solution Explorer is not displayed, you can display it by selecting View > Solution Explorer.
Step 4: Adding the GuessNumber.cpp File into the Project ◦ Next, add GuessNumber.cpp to the project you created in Step 3. ◦ In Windows Explorer (Windows 7) or File Explorer (Windows 8 and 10), open the ch01 folder in the book’s examples folder, then drag GuessNumber.cpp onto the Source Files folder in the Solution Explorer.
Step 5: Compiling and Running the Project ◦ To compile and run the project so you can test-drive the application, select Debug > Start without debugging or simply type Ctrl + F5. ◦ If the program compiles correctly, the IDE opens a Command Prompt window and executes the program (Fig. 1.16) We changed the Command Prompt’s color scheme to make the screen captures more readable. ◦ The application displays "Please type your first guess.", then displays a question mark (?) as a prompt on the next line.
Step 6: Entering Your First Guess ◦ Type 500 and press Enter. ◦ The application displays "Too high. Try again." (Fig. 1.17), meaning that the value you entered is greater than the number the application chose as the correct guess.
Step 7: Entering Another Guess ◦ At the next prompt, enter 250 (Fig. 1.18). ◦ The application displays "Too high. Try again.", because the value you entered once again is greater than the correct guess.
Step 8: Entering Additional Guesses ◦ Continue to play the game (Fig. 1.19) by entering values until you guess the correct number. ◦ When you guess correctly, the application displays "Excellent! You guessed the number."
Step 9: Playing the Game Again or Exiting the Application ◦ After you guess the correct number, the application asks if you’d like to play another game. ◦ At the "Would you like to play again (y or n)?" prompt, entering the one character y causes the application to choose a new number and displays the message "Please type your first guess." followed by a question-mark prompt so you can make your first guess in the new game. ◦ Entering the character n terminates the application. ◦ Each time you execute this application from the beginning (Step 5), it will choose the same numbers for you to guess.
C++11 (formerly called C++0x) was published by ISO/IEC in 2011. The main goals were to ◦ make C++ easier to learn, ◦ improve library building capabilities ◦ increase compatibility with the C programming language. This version of the standard extended the C++ Standard Library and includes several features and enhancements to improve performance and security.
The current C++ standard, C++14, was published by ISO/IEC in 2014. Added several language features and C++ Standard Library enhancements, and fixed bugs from C++11. For a list of C++11 and C++14 features and the compilers that support them, visit ◦ http://en.cppreference.com/w/cpp/compiler_support The next version of the C++ standard, C++17, is currently under development. ◦ https://en.wikipedia.org/wiki/C%2B%2B17