Unformatted text preview:

IntroductionPart ZeroIntroductionSuppose we want to write a function that computes the average of a list of numbers. One implementation is given here: double GetAverage(double arr[], int numElems) { double total = 0.0; for(int h = 0; h < numElems; ++h) total += arr[h] / numElems; return total; }An alternative implementation is as follows: template <typename ForwardIterator> double GetAverage(ForwardIterator begin, ForwardIterator end) { return accumulate(begin, end, 0.0) / distance(begin, end); }Don't panic if you don't understand any of this code – you're not expected to at this point – but even without an understanding of how either of these functions work it's clear that they are implemented dif -ferently. Although both of these functions are valid C++ and accurately compute the average, experienced C++ programmers will likely prefer the second version to the first because it is safer, more concise, and more versatile. To understand why you would prefer the second version of this function requires a solid understanding of the C++ programming language. Not only must you have a firm grasp of how all the lan-guage features involved in each solution work, but you must also understand the benefits and weaknesses of each of the approaches and ultimately which is a more versatile solution.The purpose of this course is to get you up to speed on C++'s language features and libraries to the point where you are capable of not only writing C++ code, but also critiquing your design decisions and arguing why the cocktail of language features you chose is appropriate for your specific application. This is an am -bitious goal, but if you take the time to read through this reader and work out some of the practice prob -lems you should be in excellent C++ shape. Who this Course is ForThis course is designed to augment CS106B/X by providing a working knowledge of C++ and its applica-tions. C++ is an industrial-strength tool that can be harnessed to solve a wide array of problems, and by the time you've completed CS106B/X and CS106L you should be equipped with the skill set necessary to identify solutions to complex problems, then to precisely and efficiently implement those solutions in C++.This course reader assumes a knowledge of C++ at the level at which it would be covered in the first two weeks of CS106B/X. In particular, I assume that you are familiar with the following:- 2 - Introduction0. How to print to the console (i.e. cout and endl)1. Primitive variable types (int, double, etc.)2. The string type.3. enums and structs.4. Functions and function prototypes.5. Pass-by-value and pass-by-reference.6. Control structures (if, for, while, do, switch).7. CS106B/X-specific libraries (genlib.h, simpio.h, the ADTs, etc.)If you are unfamiliar with any of these terms, I recommend reading the first chapter of Programming Ab-stractions in C++ by Eric Roberts and Julie Zelenski, which has an excellent treatment of the material. These concepts are fundamental to C++ but aren't that particular to the language – you'll find similar con-structs in C, Java, Python, and other languages – and so I won't discuss them at great length. In addition to the language prerequisites, you should have at least one quarter of programming experience under your belt (CS106A should be more than enough). We'll be writing a lot of code, and the more programming savvy you bring to this course, the more you'll take out of it.How this Reader is OrganizedThe course reader is logically divided into six sections:0. Introduction: This section motivates and introduces the material and covers information neces-sary to be a working C++ programmer. In particular, it focuses on the history of C++, how to set up a C++ project for compilation, and how to move away from the genlib.h training wheels we've provided you in CS106B/X.1. A Better C: C++ supports imperative programming, a style of programming in which programs are sequences of commands executed in order. In this sense, C++ can be viewed as an extension to the C programming language which makes day-to-day imperative programming more intuitive and easier to use. This section of the course reader introduces some of C++'s most common libraries, including the standard template library, and shows how to use these libraries to build imperative programs. In addition, it explores new primitives in the C++ language that originally appeared in the C programming language, namely pointers, C strings, and the preprocessor.2. Data Abstraction. What most distinguishes C++ from its sibling C is the idea of data abstraction, that the means by which a program executes can be separated from the ways in which program-mers talk about that program. This section of the course reader explores the concept of abstrac-tion, how to model it concretely in C++ using the class keyword, and an assortment of language fea-tures which can be used to refine abstractions more precisely.3. Object-Oriented Programming. Object-oriented programming is an entirely different way of thinking about program design and can dramatically simplify complex software systems. The key concepts behind object-orientation are simple, but to truly appreciate the power of object-oriented programming you will need to see it in action time and time again. This section of the course read-er explores major concepts in object-oriented programming and how to realize it in C++ with in-heritance and polymorphism. 4. Generic Programming. Generic programming is a style of programming which aims to build soft-ware that can tackle an array of problems far beyond what it was initially envisioned to perform. While a full treatment of generic programming is far beyond the scope of an introductory C++ pro-gramming class, many of the ideas from generic programming are accessible and can fundament -ally change the ways in which you think about programming in C++. This section explores theIntroduction - 3 -main ideas behind generic programming and covers several advanced C++ programming tech-niques not typically found in an introductory text. 5. More to Explore. C++ is an enormous language and there simply isn't enough time to cover all of its facets in a single course. To help guide further exploration into C++ programming, this course reader ends with a treatment of the future of C++ and a list of references for further reading.Notice that this


View Full Document

Stanford CS 106L - Part Zero- Introduction

Download Part Zero- Introduction
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Part Zero- Introduction and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Part Zero- Introduction 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?