NeilΓÇÖs Guide to C++

©opyright 2001, 2002 By Neil C. Obremski

 

Legalities ΓÇô Please Read!

 

This is an ongoing rough draft of an eventual book by Neil C. Obremski.  All documents part of this draft should be considered work in progress and as such should be taken lightly, as it is unfinished and possibly not wholly accurate.  By reading this you are acknowledging that I, Neil C. Obremski, am not liable for any damages incurred by this material.

 

All content contained therein is the property of myself, Neil C. Obremski, the copyright owner, and is under protection by common law copyright.  I hereby give all persons the right to redistribute this draft in an electronic or hardcopy form so long as they are distributed in whole and not trimmed, maimed, "corrected", or otherwise mutilated in any way.  Any recipients of the distribution must acknowledge Neil C. Obremski as the author, content creator, and copyright owner.  Persons distributing drafts must notify recipients of these legalities and any new ones that should appear.  This is to ensure credit where credit is due.

 

Hardcopies must begin with this document (Preface.doc) so that the legalities are easily visible to readers and recipients.

 

For the latest draft, information, and current legalities, please visit http://www.neilstuff.com/ or email webmaster@neilstuff.com .

 


Preface

 

Introduction

 

I've written this book as a starting point for programming using the C++ language.  You may reread key sections now and then to refresh your memory or even use it as a handy reference, but the primary goal is to bust open your brain and stuff the seeds of C++ into your consciousness.

 

It may take you a week to read this, if you finish it, or a year.  Mastery of the C++ language does not come from any single source, be it book or tutor.  It comes from years of experience, experimentation, and mingling with your programming brothers and sisters.  Know ye that I thought to forsake this timeless advice and bound ahead with great strides.  But lo!  After starting to learn and use C++ in the fall of 1996 I am still discovering things.  Do not be discouraged; expect neither too little nor too much, but be pleasantly surprised by both.

 

References in this genre of programming are plentiful and generally complete, but the city of building block books is sporadically filled with useful, useless, and flawed explanations.  Authors have sown their work not only with knowledge, but with their self-same biases, prejudices, hyped enthusiasm, or lack thereof, and coding habits for good or ill.  The blood and brains of scholars is fired and filled by the quirks of people they have never met in person.

 

Newbies, I have seen or talked to, wander lost among the maze of boundless and circular information.  Prizes of cheese escape them beyond barriers unbroken, unexplained.  I write this book to bring YOU to the cheese and send you home happy!

 

Why Choose C++?

 

Because that's what this book is about; deal with it!

 

Target Audience

 

This book is for those who wish to learn the C++ language, but I’d be an Oz Wizard if I said that this book was for everyone with this goal.  No book can hope to target that broad of an audience.  It is written as plainly as possible and helps you up from no programming or computing experience.  If you are struggling with, intimidated by, or wary of this subject then I suggest you give this book a go.  I do not intend to make this book a full C++ reference (though that is what I intend for NeilStuff.com) nor do I expect experts to read this without cringing at the simplistic, ye almost human, explanations of their closely guarded secrets.

 

I do not provide inline notes for differences between C++ and other languages.  If you are coming from a different programming language, you should recognize some things and be unfamiliar with others.  But regardless you will see the differences yourselves.

 

Organization

 

This book is written with the beginner in mind.  As such I’ve tried to organize this book so each new chapter is a slightly more advanced topic than the last.  The first chapters will teach the most basic and fundamental concepts and as the book progresses the features of the language will shine through.

 

I’ve also tried to write this without comparison to other languages.  That kind of thing, in my opinion, is best saved for a separate chapter or something.  What you want to learn is C++, so that’s what I’m going to teach you.  I won’t spend time comparing it to language ‘X’ because I can’t assume that you know that language or that you even care about it.

 

Part 1 is an introduction to the world of computers.  It requires no computer to read through or do the exercises; it is all concepts.  The part welcomes to the readers to the technobabble world of buzzwords, heaps of useless and redundant technology, and the diamonds in the rough waiting to be found out.  Feel free to skip this if you have previous programming or computer operation experience.

 

Part 2 is a guide through the basics of the C++ language.  This part will school you enough to get by with your programming.

 

Part 3 dives into advanced concepts for C++, but tries to maintain the thorough explanations.  It also brings the reader up to date with newer additions like namespaces and the standard namespace; shining the light on "proper" C++ program structure.

 

Part 4 delves further into object-oriented and object-based programming with classes, polymorphism, abstraction, namespaces, static and dynamic libraries.

 

Part 5 is on the C++ Standard Template Library which is a collection of standard classes for common programming tasks.

 

I'd also like to include stub entries of things to set people on the path to GUI, multimedia, and low-level programming.

 

Opinions

 

My opinions, when I choose to disclose them, are clearly marked throughout the book.  I’ve tried to keep them separate from the text.  Every one has their own opinions and I don’t want my personal ones interfering with your learning.  You can choose to skip them if you wish.  Concepts that I find silly or disgusting may or may not be marked.  Please do not assume that everything I teach you in this book I particularly like.  This book is for learning the language, not my personal preferences.

 

Rules to Reading

 

  1. Be Patient

    C++ is a massive concept.  Many people will never wrap their mind around the entire language and for many more it will take years.  Do not let this discourage you!  A house can be built of brick, board, or even bones … but it does not have to use all of them!  Such is the case with this language.  Only those who have previous programming experience, are collecting what they missed when first learning the language, or simply brushing up may fly through this book.

  2. Accept Text

    If you are looking to be whizzed immediately off your eye balls and into a world drowned in high performance multimedia then please come down off your cloud.  My way of teaching C++ is from the beginning, so you’ll start with numbers and text in a plain monochrome format.  I will reach graphics in this book, but if you try reading that first you will be sorely unprepared.

 

Notes

 

  • The quality of an authorΓÇÖs work is not based on their knowledge alone, but their ability to communicate effectively to the reader.┬á That said I try not to assume that because I think a concept is simple it deserves less of an explanation.┬á My attempt has been to tackle every concept as though it boggles my fragile little mind.

  • This book is written for any decent, modern C++ compiler.┬á It was a difficult choice to either favor the recent C++ standard (completed November 1997) or a broader range of compilers that didnΓÇÖt necessarily follow the entire standard for various reasons.┬á Truly all C++ compilers will eventually support the standard, but where does that leave free, unattended compilers like TC-Lite which were built before then?┬á This book appears to strike a bargain between compiler compatibility and standard trueness.┬á However, later on in the book I will cover making programs more standardized, which isnΓÇÖt a difficult change over.┬á Also, all standard-abiding compilers also support the old ways simply because of the amount of code that has been written.┬á In short, the programs in this book will work with almost any C++ compiler and follow the standard as much as possible.

  • My approach to teaching the C++ language is skewed from most authors and verily, even teachers.┬á IΓÇÖve always felt that beginners are thrown head long into the strangest mess of technical bramble with the way things usually flow.┬á If you like the way most C++ books are laid out and it makes sense to you, this book may be a bit of a warped introduction.┬á However, if you find that you canΓÇÖt seem to learn C++ regardless of the book you pick up, this one may be uniquely tasteful.┬á At first I tried reading other C++ books to get an idea of how to write my own.┬á But now that I am actually writing it, IΓÇÖm finding I only use other books as references for specific concepts rather than structure.

  • I left ΓÇÿeasyΓÇÖ out of the title because it all depends on how well my writing reaches you.┬á A lot of publications (free or otherwise) make the claim that their content is ΓÇ£easyΓÇ¥ to digest; IΓÇÖve never seen any content that was easy for everyone.┬á Thus, your mileage may vary.

  • The content of chapters will build upon the knowledge of key points in previous chapters.┬á However, IΓÇÖve tried to organize this so you can enter in at any chapter without having to read the previous ones.┬á That said, there will be no examples that must be incrementally built through a standard progression of the chapters.┬á I always hated those programs that just kept getting bigger and bigger each new chapter and here I am trying to learn the language.┬á I will save ΓÇ£real worldΓÇ¥ examples for specific times and they may be utilized or skimmed over.

  • All explanations in this book apply to the C++ language unless otherwise explicitly stated.┬á They may, however, be applicable to a more general arena, but this is not guaranteed.

 

Table of Contents

 

This is not intended as a map of things that are already written, but a path of things to work on or that I already have.  It’s a huge task to tackle a book and there’s a lot of stuff I want to cover.  I even keep coming upon things I forgot to mention in earlier chapters so first I make a change here before going back (just in case I forget).  In other words, this is to keep my in line so I don’t ramble off into complete gibberish or start writing about things out of there chapter context.

 

Part 1: Introduction to Computers and Programming

1.      Machines: Input/Output, Storage, and Logic

a.       Introduction

b.      Digital Machines

c.       The Three Parts of Computers

d.      A Simple Example

e.       Input/Output

f.        Storage

g.       Logic

h.       Hardware

2.      Logic and Languages

a.       Software

b.      Operating Systems

c.       Languages

d.      Machine Language

e.       Assembly Language

f.        High-Level Languages

g.       Compiled Languages

h.       Interpreted Languages

i.         Methods of Programming

                                                                           i.      Procedural

                                                                         ii.      Object-Oriented

3.      Numbering Systems

a.       Introduction

b.      The Decimal Numbering System

c.       Bases Loaded

d.      The Imaginary Numbering System (Fivemal)

e.       Distinguishing Numbering Systems

f.        Converting Between Numbering Systems

g.       The Binary Numbering System (base 2)

h.       The Hexadecimal Numbering System (base 16)

i.         The Dead Numbering System (base 8)

4.      Storage

a.       Computers Use Binary

b.      N-Bit CPU

c.       Hexadecimal Saves Us

d.      Multiple Byte Data Storage

5.      The C++ Language ├ƒ Do I need this chapter?┬á I want to remove it

a.       Choosing a Language

b.      History of C++

Part 2: Programming in C++

1.      Preparation

a.       What You Need

b.      Text Editor

c.       C++ Software

d.      Command Console

2.      Syntax and Expressions

a.       Breaking into the Circle

b.      The ΓÇÿHello WorldΓÇÖ Program

c.       Syntax

d.      My Dear Aunt Sally (operator precedence)

e.       Outputting Numbers

f.        Integers

g.       Floating Point

h.       Limits

i.         White Space

j.        Comments

3.      Numbers and Values

a.       What is a Variable

b.      Data Types

c.       Declaring a Variable

d.      Declaring Multiple Variables

e.       Identifiers

f.        C++ Keywords

g.       Literals

h.       Constants

i.         Assignment

j.        Basic Arithmetic

k.      Casting

l.         Output

m.     Input

n.       Increment and Decrement

o.      Land Plot Analogy

4.      Blocks and Flow

a.       Statement Blocks

b.      Scope and Nesting

c.       Locals

d.      Globals

e.       Namespaces

f.        Flow Control

g.       If

h.       Relational and Equality

i.         Logical Operators

j.        Complex Conditions

k.      Else

l.         Looping

m.     While

n.       Do-While

o.      For

p.      Breaking and Continuing

5.      References and Pointers

a.       Introduction

b.      Reference Variables

c.       Memory Address

d.      Pointers

e.       Dereference

f.        Pointer Types

g.       Void Pointers

h.       Null Pointers

6.      Structures

a.       Introduction

b.      Structures

c.       Struct

d.      Creating Struct Variables

e.       Initialize Struct Variables

f.        Using Struct Variables

g.       References and Structures

h.       Pointers and Structures

i.         Bit-Fields

j.        Unions

k.      Structure Padding

7.      Functions

a.       Introduction

b.      Modularization

c.       Prototyping and Definition

d.      First Example

e.       Return

f.        Void

g.       Correct Main

h.       Parameters

i.         Passing by Value

j.        Passing by Reference

k.      Constant Parameters

l.         Scope

m.     Call Stack

8.      Arrays

a.       Introduction

b.      Array Declaration

c.       Looping Subscripts

d.      Initializing Arrays

e.       Declaring Arrays without Size

f.        Copying an Array

g.       Arrays in Memory

h.       Pointers to Arrays

i.         Pointer Arithmetic

j.        References to Arrays

k.      Constant Arrays

l.         Array Function Parameters

m.     Passing Arrays to Functions

n.       SizeOf Array

9.      Strings

a.       Text is Character Strings

b.      Character Literals

c.       ASCII and Assumptions

d.      Char

e.       Character Functions

f.        String Literals and Constants

g.       Variable Strings

h.       Initializing Strings

i.         Pointers to Strings

j.        Cin and Strings

k.      String Assignment

l.         SizeOf String

m.     Concatenation

n.       Comparing Strings

o.      Converting Strings to Numbers

p.      Changing String Case

q.      Unicode Strings

10.  Pre-Processing

a.       The ΓÇ£OtherΓÇ¥ Compiler

b.      #include

c.       Custom Headers

d.      Macros

e.       Destroying Macros

f.        Flow Control

g.       Existence Checking

h.       Parenthesis Unnecessary

i.         Raising Errors

j.        Concise Types

11.  Classes and Objects

a.       Blobs with Class

b.      Simple Declaration and Usage

c.       Member Functions

d.      Constructors and Destructors

e.       Custom Constructors

f.        Initializing Member Variables

g.       Inheritance

h.       Overloading

i.         Class Namespaces

j.        Static Members

k.      Access Modifiers

l.         Friends

m.     This

n.       Structures are Classes

12.  Files and I/O

a.       Console I/O (cout/cin)

b.      Escape Characters

c.       File I/O (ofstream/ifstream)

d.      Binary Files

e.       Reading in a Structure

Part 3: Glossary of Terms and Advanced Concepts

 

Topics Brainstorm

 

Some of the following things might never get included in the book, others will be absorbed by chapters already written.  But the following is basically a structured brainstorm of things I had originally planned to cover.  Why can’t I write about all of these?  The book would be enormous (due to its “guiding” quality rather than being a “reference”) and it would cost you a lot more.

 

1.      Memory

a.       Stack Memory / Call Stack

b.      Heap Memory

c.       Allocating and Free Blocks

d.      Allocating Object Instances

2.      Name Spaces

a.       Global

b.      std

3.      Advanced Flow Control

a.       Switch

b.      Else If

c.       Comma Operator

d.      Ternary Operator

4.      Bit Manipulation

a.       Bit Variables in Objects

b.      Bitwise Operators

c.       Shifting

d.      Making Flags

e.       Using Enum/Define for Flag Constants

5.      Modularization

a.       Header Files

b.      Multiple Source Files

c.       Static Code Libraries

d.      Dynamic Code Libraries (DLL, SO)

e.       extern Keyword

6.      Recursion

a.       Show Folder and all Sub-Folders

b.      Towers of Hanoi

7.      Advanced Functions

a.       Variable Arguments

b.      Pointers to Functions

c.       Callbacks

d.      Functions inside Functions

e.       Templates

f.        Function Overloading

g.       Operator Overloading

Part 4: Object Orientation with Classes

1.      Introduction to Classes

a.       Versus struct

2.      Member Functions

a.       Pointers to

3.      Protected, Private, and Public

4.      Member Scope

5.      Operator Overloading

a.       making my string class act like a char*

Part 5: Performance and Tuning

1.      Data Alignment

2.      Cached and Volatile Variables

a.       What is Caching?

b.      Cached Variables: register

c.       Non-Cached Variables: volatile

3.      Outline and Inline Functions

4.      Name Spaces

5.      Unicode

6.      Etiquette

a.       Conventions

Part 6: Debugging

1.      Tracing

2.      Assertions

3.      Using a Debugger

Part 7: Standard C++ and the STL

1.      Correct C++

a.       Default Name Space

b.      A Politically Correct ΓÇÿHello WorldΓÇÖ

2.      Intro to STL

3.      Vectors and Iterators

4.      Strings

Part 8: Multi-Threading

1.      Synchronization Nightmare

 


Disorganized List of Topics to Cover

 

  • Member function pointers
  • Multiple inheritance ΓǪ casting a pointer to a base class from a multiply-derived one
  • Static arrays of member function pointers (this will be good!)
  • C-Style Casting and ΓÇ£CleverΓÇ¥ Casting
  • Pointer-Pointers
  • Multi-Dimensional Arrays
  • Arrays of Strings
  • Templates: Classes and Functions
  • Exceptions
  • Casting Base Pointers to Derived Objects with Single and Multiple inheritance