By
Robert Burns, Diablo Valley College
Copyright 2014 by Robert Burns.
Library of Congress Control Number: 2014912474
ISBN: Hardcover 978-1-4990-4482-9
Softcover 978-1-4990-4483-6
eBook 978-1-4990-4481-2
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without permission in writing from the copyright owner.
Any people depicted in stock imagery provided by Thinkstock are models, and such images are being used for illustrative purposes only.
Certain stock imagery Thinkstock.
Rev. date: 05/13/2015
Xlibris
1-888-795-4274
www.Xlibris.com
626893
Table of Contents
Computers are great! You give them input and they produce output. Whether its typing an Internet address and then seeing a web page with the latest news, or entering tax information and then printing a 1040 form with all supporting documents, or using an app on a smart phone to pay for your coffee, computers offer various ways for us to specify input and they have a variety of different ways to express their output.
Computers are fast and accurate. Humans, by comparison are slow and, lets face it, we make mistakes. Have you ever added the same list of numbers twice and gotten two different totals? But humans have something that computers do not intellig ence .
Computers did not design the Internet, nor did they devise the tax code. Computers did not design how smart phone touch screens work. Its the humans who figured out all of that. Somewhere along the line, a human must have explained to a computer just exactly what to do with the input in order to turn it into output. A human had to tell the computer how to decide if a password is valid or not. A human had to explain how to calculate amortization. A human had to define the pinch motion and decide how it should zoom out on a photo. Same for the shake motion that shuffles songs in an MP3 player.
So its the human+computer team that makes all of this work. Using their intelligence, the humans have the ideas and figure out recipes for converting input into output. Using their speed and accuracy, the computers faithfully and reliably perform the conversions.
The humans on such teams are called programmers. The purpose of this book is to turn you into a programmer.
This book is organized into three parts, separated by major milestones in gaining programming knowledge.
Part 1 shows how to apply basic concepts of programming. It goes through the details of writing programs using freely available editor and compiler software. It shows how to store data in variables for use in calculations, and how to produce nice-looking output. These chapters teach all that is needed to create simple interactive programs that gather input, perform calculations based on the input, and display output using calculated results.
Part 2 adds elements of logic to the simple programs of part 1. Ways are presented for making programs selectively use different sets of instructions, based on circumstances. Ways to get a program to repeat itself are also presented, allowing things to be done more than once without duplicating the steps. These chapters teach what is needed to create more sophisticated programs with branching and looping logic, such as would be required for computer games and almost every other useful program.
Part 3 introduces the powerful concept of single variables that can store multiple values all at the same time. Array variables can store multiple values of the same type, and are suitable for dealing with lists, such as lists of test scores, high temperatures, or names of students. Object variables can store multiple values of related information, and are suitable for dealing with data records, such as student accounts with names, IDs, and addresses. Chapters 13-15 introduce advanced applications of arrays and objects, for the purpose of first exposure to some advanced computer science concepts, but primarily to provide an opportunity to apply the language elements learned in parts 1 and 2.
The Appendix contains excerpts from the C++ library with references to all of the library elements used in this book. There is also an Index at the end.
Besides correcting typos from the first edition, this second edition replaces the Microsoft Visual Studio compiler with an open source C++ compiler. Its a much easier and lighter install, and it allows the command-line compile commands to be consistent between PCs and Macs. It also makes the command-line experience very nearly the same among systems (PC and Mac) and languages (C++, Java, and Python the languages in this book series) so that the focus can be on concepts instead of logistics.
This new edition is also more Mac friendly it includes screen shots from both PC and Mac examples.
The e-books code listings are now hyperlinks to copy/pasteable code, so students dont have to type every keystroke of every sample code listing. The Java and Python editions of the e-book had this feature, but the 1 st C++ edition did not.
Another change is that references to the command-line commands cl and g++ are both now just c++ .
Unlike many of todays introductory textbooks, this book does not jump into object-oriented programming right away. Objects are introduced and used, particularly for such language features as input/output. Objects that encapsulate data are presented as variables that store multiple values at the same time. But a development of objects that encapsulate data and code and interact with other objects is specifically avoided. Instead, the focus is on the fundamental concepts of data, statements, expressions, and flow of control. These need to be well-understood in order to be used in the programming of classes and objects anyway, so rather than make the student pick up both concepts at once, objects are left for another, later level of study.
Emphasis is on command-line compiling instead of IDEs, because it is a lowest common denominator of programming. In this way, C++ program development is consistent among different systems such as PCs, Macs, and UNIX/Linux. But just about any IDE can be can be used in following this book, if that is the instructors or students choice. But because an IDE usually combines the editing and compiling steps into a single program, its likely that the unique separation of these programming steps will be lost on the beginning student. Command-line compiling makes the student more aware of the files and folders used on the host system, and differences among operating systems become not so great.
This book makes a distinction between expressions and statements . Expressions are the building blocks of statements, and as such, they individually and separately resolve to simple values that get substituted in full statements. They are likened to phrases in the spoken language. Language elements that are expressions are specifically pointed out as such, and are specially notated for example, x + y . Full statements, usually ending in a semicolon, are likened to sentences in the spoken language. They are notated like this: x = 1; .
It may be surprising to see linked lists, collections, and recursion addressed in the later chapters. But the purpose is not to make the student proficient in the use or understanding of these concepts. Rather it is to provide a vehicle for practicing what was learned about variables, branching, loops, functions, arrays, and objects. And in so doing, the student is also introduced to ideas that will reappear in future studies in computer science, and be better prepared to learn them at that time.
Next page