Register your product to gain access to bonus material or receive a coupon.
52594-9
“Show me the code!” The easy, visual approach to C++.
Finally, there's a book that presents C++ the way you want to learn it --from real, working code! C++ Programmer's Notebook is so visual, so sensible, so convenient, you'll wonder why nobody thought of it before. See for yourself!
And if you want to know more, it's right there for you on the adjacent left-hand page.
C++ Programmer's Notebook isn't just convenient: it's comprehensive. And it's carefully, logically organized to build your expertise one step at a time. It covers all the C++ concepts you need to know, including:
There's even a programmer's checklist for writing efficient, reliable code.
It's a complete course in C++ without the pain! Whether you're a professional programmer learning C++, a C++ programmer needing a quick reference, or a student, this is the C++ book you'll use every day!
Preface.
1. Working with Variables.
Naming Variables. Using Variables. Integer Variables. Character Variables. Escape Sequences. Floating-Point Variables. Inside Exponential Notation. Signed and Unsigned Variables. Data Type Conversion. Assigning Values to Variables. Local Variables. Global Variables. Local Variables vs. Global Variables. Storage Specifiers. static. Register. auto. Storage Specifier Rules.
Expressions. Arithmetic Operators. The +=, -=, *=, /= Operators. The % Operator. The ++, -- Operators. Relational Operators. The <=, ==, != Operators. Logical Operators. Bitwise Operators. Bitwise AND. Bitwise OR. Bitwise XOR (exclusive OR). Bitwise NOT. The Shift Left Operator. The Shift Right Operator. The ? Operator (if...the...else). The & and * Pointer Operators. A Brief Look at Pointers. The Precedence Table. Working with Expressions.
Arrays and Strings. Other Ways to Assign Values to an Array. Loop. Multidimensional Arrays. Other Ways to Assign Values to a Multidimenional Array. Loop. Strings. Copying a String to Another String scanf(). Reading a String from the Keyboard gets(). Another Way to Read a String from the Keyboard scanf(). Still Another Way to Read a String from the Keyboard with the Extraction Operator. Comparing Two Strings strcmp(). Displaying a String printf(). Another Way to Display a String puts(). Still Another Way to Display a String with the Insertion Operator. Concatenating Strings strcat(). Determining the Length of a String strlen(). Determining the Length of a Substring strcspn(). Another Way to Determine the Length of a Substring strspn(). Concatenating a Substring strncat(). Comparing Substrings strncmp(). Locating a Character within a String strpbrk(). Locating the First Occurrence of a Substring in a String strstr(). Dividing a String into Several Substrings strtok().
Structures. A Structure at Work. Running the Sample Program. Another Way to Declare an Instance. Assigning Values to Structure Members. Multiple Instances. Assigning Values to Multiple Instances. Structures within Structures. Passing Elements of a Structure to a Function. Passing by Address. Passing the Value of a Structure to a Function. Passing the Address of a Structure to a Function. Unions. Other Ways to Declare an Instance of a Union. Enumerations. Typedefine.
Anatomy of a C++ Function. The main( ) Functions. How to Use a function. Passing Arguments. Passing Multiple Arguments. Passing Arguments to the main( ) Function. Returning Values. Functions Calling Functions. Building a Program Using Functions. Program Statements. White Space Characters. Preprocessor Directives. More Preprocessor Directives. #define with Arguments. #if, #else, #elif, #endif. #ifdef, #ifndef, #undef. #line, #error and #pragma. Making a C++ Program.
Program Control Statements. If. if ... else. The ? Operator. Nested if. if ... elseif. Multiple Conditions. switch ... case. Tricks of the Trade. for loop. Decrementing the for Loop Counter. Another Way to Use a for Loop. A Nested for Loop. An Infinite Loop. while Loop. do. . . . while Loop. Break. exit( ). Continue. Goto.
Classes. Declaring an Instance of a Class. Hiding Data Using the Access Specifier. The Constructor. Overloading the Constructor. The Destructor. Defining a Function Member Outside the Class Definition.
Overloading. Overloading a Function Using Different Data Types. Overloading a Unary Operator. Overloading a Binary Operator. Operators that Can and Cannot be Overloaded.
Inheritance. Access Specifiers. More About Inheritance. Multiply Inheritance. Ambiguity in Multiple Inheritance. Containership Class. Levels of Inheritance.
Pointers. Using Pointers in Expressions. Incrementing Pointers. Decrementing Pointers. Pointer Math. Tricks of the Trade. An Array of Pointers. Pointers-to-Pointers. A Pointer-to-a-Function. Tricks of the Trade.
Creating a Virtual Function. Without Redefining a Virtual Function. Creating a Pure Virtual Function. More about Virtual Functions. Friend Functions. Creating a Friend Function from a Function Member. Forward Reference of a Class.
Keyboard Input/Screen Output. Reading a Character from the Keyboard without Echo getch(). Display a Character on the Screen putchar(). Reading a String from the Keyboard with Less Overhead gets(). Reading a String from the Keyboard with Greater Control scanf(). Reading a String scanf(). Limiting the Number of Characters Read from the Keyboard. Receiving More than One Value at the Same Time. Creating Your Own Separator. Another Way to Create Your Own Separator. Displaying a String on the Screen with Less Overhead puts(). Displaying a String on the Screen with Greater Control printf(). Displaying a String printf(). Displaying an Integer printf(). Padding the Data with Spaces. Specifying the Minimum and Maximum Width of Data. Adding Padding to the End of the Data. Specifying the Number of Decimal Places. Displaying a String Constant in C++ cout<<. Displaying a String Variable Using cout. Displaying a Non-String Variable Using cout. Displaying Strings And Non-Strings Together Using cout. Reading a String from the Keyboard Using C++ cin>>. Manipulators.
File Input/Output. Mode Specifiers for the fopen() Function. Closing a File. Writing a Character to a File putc(). Reading a Character from a File getc(). Tricks of the Trade. Writing a String to a File fputs(). Reading a String from a File fgets(). Writing an Integer to a File putw(). Reading an Integer from a File getw(). Writing a Block of Data to a File fwrite(). Reading a Block of Data from a File fread(). Creating a Database File. Reading a Record. Writing Various Data Types to a File fprintf(). Using the fprintf() Function. Reading Various Data Types from a File fscanf(). Using the fscanf() Function. Moving to Specific Locations in a File fseek(). Returning to the Beginning of a File rewind(). Deleting a File unlink(). Using a File in C++. Writing Information to a File Using C++. Reading a String from a File Using C++. Writing a Single Character to a File Using C++. Reading a Single Character from a File Using C++. Writing an Object to a File. Reading an Object from a File. Specifying the File Mode in C++.
Computer Memory. Storing Data on the Stack. Storing Data on the Heap. Storing an Array of Objects on the Heap. Reallocating Heap Memory. Another Way of Storing Data on the Heap. Using a Dynamic Array.
The Bubble Sort. Inside the Bubble Sort. The Linear Search. The Binary Search. A Close Look at the Binary Search Routine.
Linked Lists. Adding a Node to a Linked List. A Closer Look at Adding a Node. Deleting a Node from a Linked List. A Closer Look at Deleting a Node. Stacks. A Closer Look at the push() Function. The pop() Function. A Closer Look at the pop() Function. Queues. A Closer Look at the enterqueue() Function. The exitqueue() Function. A Closer Look at the exitqueue() Function.
Templates. Templates with Multiple Parameters. Class Template.
General. Keywords, Variables, Arrays, and Constants. Expressions and Operators. Program Control. Functions. Classes. Input/Output.
Preface
Software developers strive to build complex computer applications quickly, accurately, and at a reasonable expense. The methodology that is used by many developers is modular programming. This is a technique where the developer divides the application into logical components, each of which can be built independently and reused in other applications.
These modules are referred to as subroutines, procedures, or functions, depending on which computer language is used. In the C++ programming language as well as in C, these modules are called functions. Regardless of the name, however, modules are limited. The modular programming approach to software design, at times, makes modeling into computer code a real life problem and a difficult task. A real life problem involves real objects. For example, tracking college registration requires that a registration form be used. This registration form is an object. In fact, the form is actually composed of many objects, such as places to enter data and text that provides instructions to the applicant. A real object has both data and procedures associated with an object. For example, a single copy (called an instance) of the registration has the student's name, course information, and other data that is directly related to only that copy of the form. Also associated with the form are the steps that are required to enter data onto the form and to process that copy of the form.
Traditional modularity does not lend itself to using an object-oriented design approach when translating a real life problem into a computer application. Object-oriented design requires software developers to visualize a real life object as an object and not as a set of procedures. For example, a college registration form is a physical object that is composed of other objects, such as a place to enter data and labels that provide directions for completing the form.
The C programming language provided flexibility for developers. They could write low-level applications, such as an operating system using C. C was also a language that could be used to create a simple business application. However, C could not properly handle an object-oriented design specification of an application.
In the early 1980s at Bell Laboratories, the birthplace of the C programming language, Bjarne Stroustrup created an enhancement of C called C++. Stroustrup set out to give the C language the capability of using code to better represent a real life object. He transformed the C programming language from a procedural language into an object-oriented language. C++ is a superset of C. That is, C++ has all the capabilities as C plus more features.
A Touch of Class
A real life object has both data and procedures associated with the object. This association is represented in C++ code through the use of a class. A class defines a real life object as having a set of data (called data members) and procedures (called function members). A copy of the class is created by the software developer by using the name of the class to declare an instance of the class. The instance is given a unique name. This technique is illustrated later in this book. The instance of the class has its own copy of data and functions. Stroustrup also gave C++ the capability to share classes (called inheritance). This enables software developers to use other classes (called base classes) to build new classes (called derived classes). Return to the example of the registration form. The form is a new object that is composed of data (called an edit object) and text (called label objects). An edit object relates the data and the function members to accept, edit, and validate the data. A label object can store text used in the label and has function members that handle fonts, and other characteristics that can be used for the label. Therefore, when the software developer needs to build a form, such as the student registration form, the developer can create instances of the edit and label objects. All of the text editing capabilities (i.e., insertion and deletion of characters) are inherited.
The Picture Book Approach
C++, as any computer language, is complex and has many rules that must be obeyed. Learning those rules can be time-consuming, especially for readers who already know how to program in a language other than C++. Those readers want to jump into the language and begin writing simple code almost immediately. Many programmers who learn C++ as their second language have their own philosophy about learning the language. "Show me sample code and I'll figure out the rest," is a statement that summarizes their approach. And that's what I do in this book.
The picture book concept places the focus of the book on a picture of the code. Around this picture are callouts that describe each keyword and statement. The rules are presented in tables that are positioned near the picture. Furthermore, there is a picture for each variation of the topic that is discussed in the chapter. A reader who wants to jump into C++ can study the picture, then copy the code into a compiler and make the executable program without having to sift through pages of text. The rules can be referenced later, when the reader needs to expand this use of the routine.
This approach is not intended to circumvent a thorough presentation of the C++ language. In fact, this book presents C++ in its completion. Instead, the picture-book approach presents material in the way programmers want to learn a new computer language.
Navigating This Book
I organized this book into traditional chapters. Each chapter covers a topic of C++ in a logical progression. So, if you are not familiar with the basics of C++, then begin with the first chapter and continue through each chapter in progression. At the end of the last chapter you will have a good foundation in C++. However, these chapters can be used also for quick reference. Jump to the chapter that discusses the topic that you want to review. The topic within the chapter is presented in its entirety with a focus on examples of code.
Each chapter is further divided into two-page spreads. That is, careful attention is given to the relationship between the left and right pages. The left page contains text that describes the topic that is illustrated on the right page. The right page focuses on C++ code that contains callouts describing each facet of the code example.
The most efficient way to use a two-page spread is to first study the example on the right page. If you understand the function of each statement in the example, then you can continue and write your own program. However, if a statement or keyword is confusing, then read the callout that describes the item. Still confused? Read the text on the left page.
Careful attention is given to clarity of the code example, the right page. You will notice that the syntax of the C++ code is shown in color. Parts of the statements that are not colored are pieces that the programmer creates. For example, the statement char code; declares a character variable. char and the semicolon (;) are colored. They are part of the syntax of C++. The word code, however, is the name of a variable that can be any name that complies with the rules of C++. This style is similar to the concept used with Integrated Development Environments (IDE) such as used in Borland C++ and Microsoft Visual C++.
All code in this book can be found at the following FTP site:
/ftp/pub/ptr/professional_computer_science.w.-022/keogh/c++_notebook.