c for artist

Upload: drvdev3

Post on 14-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 C for Artist

    1/644

  • 7/29/2019 C for Artist

    2/644

    C++ For Artists

  • 7/29/2019 C for Artist

    3/644

  • 7/29/2019 C for Artist

    4/644

    C++ For ArtistsThe Art, Philosophy, and Science of Object-Oriented Programming

    Pulp Free PressFalls Church, Virginia

    RicK Miller

  • 7/29/2019 C for Artist

    5/644

    Pulp Free Press, Falls Church, Virginia 22042www.pulpfreepress.com

    [email protected]

    2003 Richard Warren Miller & Pulp Free Press All Rights Reserved

    No part of this book may be reproduced in any form without prior written permission from the publisher.

    First edition published 2003

    16 14 12 10 08 06 04 10 9 8 7 6 5 4 3 2

    Pulp Free Press offers discounts on this book when ordered in bulk quantities. For more information regarding

    sales contact [email protected].

    The eBook/PDF edition of this book may be licensed for bulk distribution. For whole or partial content licensing

    information contact [email protected] .

    Publisher Cataloging-in-Publication Data: Prepared by Pulp Free Press

    Miller, Rick, 1961 -C++ For Artists: The Art, Philosophy, and Science of Object-Oriented

    Programming.p. cm.Includes bibliographical references and index.ISBN: 1-932504-02-8 (pbk)

    1. C++ (Computer program language) 2. Object-Oriented Programming(Computer Science) I. Title.QA76.73.C153M555 2003005.13'3--dc21 2003093826

    CIP

    The source code provided in this book is intended for instructional purposes only. Although every possible measure

    was made by the author to ensure the programming examples contain source code of only the highest caliber, no

    warranty is offered, expressed, or implied regarding the quality of the code.

    All product names mentioned in this book are trademark names of their respective owners.

    C++ For Artists was meticulously crafted on a Macintosh PowerMac G4 using Adobe FrameMaker, Adobe Illustra-

    tor, Macromedia Freehand, Adobe Photoshop, Adobe Acrobat, Embarcadero Technologies Describe, ObjectPlant,

    Microsoft Word, Maple, and VirtualPC. Photos were taken with a Nikon F3HP, a Nikon FM, a Nikon CoolPix 800,

    and a Contax T3. C++ source code examples prepared using Metrowerks CodeWarrior for Macintosh, Apple OSX

    developer tools, and Tenon Intersystems CodeBuilder. Java code examples prepared using Suns Java 2 Standard

    Edition (J2SE) command line tool suite. Assembly language examples prepared using Microsoft Macro Assembler(MASM).

    ISBN 1-932504-00-1 First eBook/PDF Edition

    ISBN 1-932504-01-X First CD ROM Edition

    ISBN 1-932504-02-8 First Paper Back Edition

  • 7/29/2019 C for Artist

    6/644

    To Coralie and Kyle for their eternal patience

  • 7/29/2019 C for Artist

    7/644

  • 7/29/2019 C for Artist

    8/644

    C++ For Artists

    2003 Rick Miller All Rights Reserved

    vii

    Preface

    Welcome And Thank You! .................................................................................................................................xlviiTarget Audience ...................................................................................................................................................xlviiApproach ............................................................................................................................................................xlviiArrangement ........................................................................................................................................................xlviii

    Part I: The C++ Student Survival Guide ................................................................................................... ...........................................xlviii

    Chapter 1: An Approach To The Art Of Programming .......................................................................................................................................................xlviiiChapter 2: Small Victories: Creating Projects With IDEs ..................................................................................................................................................xlviiiChapter 3: Project Walkthrough: An Extended Example .....................................................................................................................................................xlviiiChapter 4: Computers, Programs, and Algorithms ............................................................................................................................................................xlviii

    Part II: C++ Language Fundamentals ....................................................................................................................................................xlix

    Chapter 5: Simple Programs................................................................................................................................................................................................xlixChapter 6: Controlling The Flow Of Program Execution..................................................................................................................................................xlix

    Chapter 7: Pointers And References....................................................................................................................................................................................xlixChapter 8: Arrays................................................................................................................................................................................................................xlixChapter 9: Functions ...........................................................................................................................................................................................................xlixChapter 10: Toward Problem Abstraction: Creating New Data Types................................................................................................................................xlixChapter 11: Dissecting Classes .............................................................................................................................................................................................lChapter 12: Compositional Design......................................................................................................................................................................................lChapter 1 3: Extending Class Functionality Through Inheritance..........................................................................................................................................l

    Part III: Implementing Polymorphic Behavior ........................................................................................... ........................................... l

    Chapter 14: Ad Hoc Polymorphism: Operator Overloading .............................................................................................................................................lChapter 15: Static Polymorphism: Templates ........................................................................................................................................................................lChapter 16: Dynamic Polymorphism: Object-Oriented Programming .................................................................................................................................li

    Part IV: Intermediate Concepts ............................................................................................................................................................li

    Chapter 17: Well-Behaved Objects: The Orthodox Canonical Class Form..........................................................................................................................liChapter 18: Mixed Language Programming .........................................................................................................................................................................liChapter 19: Three Design Principles....................................................................................................................................................................................liChapter 20: Using A UML Modeling Tool .........................................................................................................................................................................li

    How To Read C++ For Artists ...........................................................................................................................liiPedagogy .............................................................................................................................................................liii

    Chapter Layout ....................................................................................................................................................................................liii

    Learning Objectives..............................................................................................................................................................................................................liiiIntroduction.........................................................................................................................................................................................................................liiiContent ................................................................................................................................................................................................................................liiiQuick Reviews ......................................................................................................................................................................................................................liiiSummary ...............................................................................................................................................................................................................................liiiSkill-Building Exercises .........................................................................................................................................................................................................liiiSuggested Projects...............................................................................................................................................................................................................liiiSelf-Test Questions ................................................................................................................................................................................................................liiiReferences.............................................................................................................................................................................................................................liiiNotes ....................................................................................................................................................................................................................................liii

    CD-ROM .............................................................................................................................................................livSupportSite Website ........................................................................................................................................livProblem Reporting ..............................................................................................................................................livAcknowledgements ...............................................................................................................................................lv

    Part I: The C++ Student Survival Guide

    1 An Approach To The Art Of Programming

    Introduction ........................................................................................................................................................4

    Detailed Contents

  • 7/29/2019 C for Artist

    9/644

    Detailed Contents

    viii

    2003 Rick Miller All Rights Reserved

    C++ For Artists

    The Difficulties You Will Encounter Learning C++ .................................................................................................... .........................4

    Required Skills .....................................................................................................................................................................................................................4The Planets Will Come Into Alignment..................................................................................................................................................................................4How This Chapter Will Help You ........................................................................................................................................................................................5

    Project Management .............................................................................................................................................5

    Three Software Development Roles ......................................................................................................................................................5

    Analyst ..................................................................................................................................................................................................................................5Architect...............................................................................................................................................................................................................................5Programmer .........................................................................................................................................................................................................................6

    A Project Approach Strategy ......................... ............................................................................................................ .........................6

    You Have Been Handed A Project Now What?...............................................................................................................................................................6Strategy Areas of Concern..................................................................................................................................................................................................6Think Abstractly....................................................................................................................................................................................................................7

    The Strategy In A Nutshell ....................................................................................................................................................................7Applicability To The Real World ............................................................................................. ............................................................. 7

    The Art of Programming .....................................................................................................................................8

    Dont Start At The Computer ..............................................................................................................................................................8Inspiration Strikes At The Weirdest Time ..............................................................................................................................................8Own Your Own Computer .................................................................................................................................................................8

    You Ei ther Have Time and No Money, or Money and No Time ..........................................................................................................................................8The Family Computer Is Not Going To Cut It! ....................................................................................................................................................................9

    Set The Mood ......................................................................................................................................................................................9

    Location, Location, Location ..............................................................................................................................................................................................9

    Concept Of The Flow ..........................................................................................................................................................................9

    The Stages of Flow ...............................................................................................................................................................................................................9

    Be Extreme ............................................................................................................................................................................................10

    The Programming Cycle.......................................................................................................................................................................................................10The Programming Cycle Summarized....................................................................................................................................................................................11

    A Helpful Trick: Stubbing ....................................................................................................... ............................................................. 11Fix The First Compiler Error First ......................................................................................................................................................11

    Managing Project Complexity ..............................................................................................................................11

    Split Even Simple Projects Into Multiple Source Code Files ....................................................................................................... .......12

    Separating a Classs Interface from its Implementation .........................................................................................................................................................12Benefits of Separating Interface from Implementation ...........................................................................................................................................................12Helpful Preprocessor Directives..........................................................................................................................................................................................13The Final Word on Preprocessor Directive Behavior ........................................................................................................................................................14

    Project File Format ..............................................................................................................................................................................14

    Header File ...........................................................................................................................................................................................................................14Implementation File................................................................................................................................................................................................................15Main File...............................................................................................................................................................................................................................15

    Commenting .........................................................................................................................................................................................16

    C-Style Comments.................................................................................................................................................................................................................16C++-style Comments .............................................................................................................................................................................................................17Write Self-Commenting Code: Give Identifiers Meaningful Names ........................................................................................................................................17Adopt A Convention And Stick With It...............................................................................................................................................................................19Restrict The Number of Global Variables.............................................................................................................................................................................19Minimize Coupling, Maximize Cohesion...............................................................................................................................................................................19

    Textbooks, Reference Books, and Quick Reference Guides ....................................................................................19Summary ..............................................................................................................................................................20Skill Building Exercises ........................................................................................................................................20Suggested Projects ..............................................................................................................................................21Self Test Questions ..............................................................................................................................................22References ............................................................................................................................................................22Notes ...................................................................................................................................................................23

    2 Small Victories: Creating Projects With IDEs

    Introduction ........................................................................................................................................................26The Program Creation Process ...........................................................................................................................26Integrated Development Environments ...................................................................................................................27

    Metrowerks CodeWarrior ..................................................................................................................................................................28Microsoft Visual C++ ..........................................................................................................................................................................32Intermission ..........................................................................................................................................................................................35Tenon Intersystems MachTen CodeBuilder ........................................................................................................................................36

  • 7/29/2019 C for Artist

    10/644

    Detailed Contents

    C++ For Artists

    2003 Rick Miller All Rights Reserved

    ix

    Attention Linux Users ...........................................................................................................................................................................................................36Organizing Project Files......................................................................................................................................................................................................37Creating Source Files ..........................................................................................................................................................................................................37Creating makefile ..................................................................................................................................................................................................................37

    Summary ..............................................................................................................................................................39Skill Building Exercises ........................................................................................................................................39Suggested Projects ..............................................................................................................................................40Self Test Questions ..............................................................................................................................................40

    References ............................................................................................................................................................40Notes ...................................................................................................................................................................41

    3 Project Walkthrough: An Extended Example

    Introduction ........................................................................................................................................................44The Project Approach Strategy ..........................................................................................................................44The Development Cycle .........................................................................................................................................45The Project Specification .....................................................................................................................................46

    Analyzing The Project Specification ...................................................................... ...............................................................................47

    Requirements.........................................................................................................................................................................................................................47Problem Domain ..................................................................................................................................................................................................................48Language Features.................................................................................................................................................................................................................50

    Design (First Iteration) ........................................................................................................... ............................................................. 51Implementation (First Iteration) .............................................................................................................................................................53Testing (First Iteration) .........................................................................................................................................................................55Integration (First Iteration) ..................................................................................................................................................................55Design (Second Iteration) ....................................................................................................................................................................56

    Function Stubbing ...............................................................................................................................................................................................................56Other Considerations ..........................................................................................................................................................................................................56

    Implementation (Second Iteration) ........................................................................................................................................................57Testing (Second Iteration) ....................................................................................................................................................................59Integration (Second Iteration) .............................................................................................................................................................60Design (Third Iteration) .......................................................................................................................................................................60Implementation (Third Iteration) ...........................................................................................................................................................60Testing (Third Iteration) ........................................................................................................................................................................62Integration (Third Iteration) .................................................................................................................................................................64

    Design (Fourth Iteration) .....................................................................................................................................................................64Implementation (Fourth Iteration) .........................................................................................................................................................66Testing (Fourth Iteration) .....................................................................................................................................................................68Integration (Fourth Iteration) ..............................................................................................................................................................68Design (Fifth Iteration) .........................................................................................................................................................................68Implementation (Fifth Iteration) .............................................................................................................................................................70Testing (Fifth Iteration) .........................................................................................................................................................................70Integration (Fifth Iteration) ..................................................................................................................................................................71

    Wrapping Up The Project ...................................................................................................................................72Complete Robot Rat Source Code Listing ...........................................................................................................72Summary ..............................................................................................................................................................76Skill Building Exercises ........................................................................................................................................76Suggested Projects ..............................................................................................................................................76Self Test Questions ..............................................................................................................................................76

    References ............................................................................................................................................................77Notes ...................................................................................................................................................................77

    4 Computers, Programs, & Algorithms

    Introduction ........................................................................................................................................................80What Is A Computer? ..........................................................................................................................................80

    Computer vs. Computer System ...........................................................................................................................................................80

  • 7/29/2019 C for Artist

    11/644

    Detailed Contents

    x

    2003 Rick Miller All Rights Reserved

    C++ For Artists

    Computer System..................................................................................................................................................................................................................80Processor ............................................................................................................................................................................................................................82

    Three Aspects of Computer Architecture .............................................................................................................................................83

    Feature Set............................................................................................................................................................................................................................83Feature Set Implementation ..................................................................................................................................................................................................83Feature Set Accessibility .......................................................................................................................................................................................................83

    What Is A Program? ...........................................................................................................................................83

    Two Views of a Program ........................................................................................................ .............................................................84

    Concept of Observable Behavior ........................................................................................................................................................84

    The C++ Program Transformation Process .........................................................................................................85

    Phase 1..................................................................................................................................................................................................................................85Phase 2.................................................................................................................................................................................................................................85Phase 3 .................................................................................................................................................................................................................................85Phase 4 ................................................................................................................................................................................................................................86Phase 5 .................................................................................................................................................................................................................................86Phase 6.................................................................................................................................................................................................................................86Phase 7 .................................................................................................................................................................................................................................86Phase 8.................................................................................................................................................................................................................................87Phase 9.................................................................................................................................................................................................................................87

    The Processing Cycle ...........................................................................................................................................87

    Fetch ....................................................................................................................................................................................................88Decode .................................................................................................................................................................................................88Execute .................................................................................................................................................................................................88Store ....................................................................................................................................................................................................88

    Memory Organization ...........................................................................................................................................88

    Memory Basics .....................................................................................................................................................................................88

    Memory Hierarchy ...............................................................................................................................................................................................................89Bits, Bytes, Words ...............................................................................................................................................................................................................89

    Alignment and Addressability ............................................................... ................................................................................................. 90

    Algorithms ..........................................................................................................................................................91

    Good vs. Bad Algorithms ....................................................................................................................................................................92

    Dont Reinvent The Wheel! ...................................................................................................................................................................................................94

    Summary ..............................................................................................................................................................94Skill Building Exercises ........................................................................................................................................94Suggested Projects ..............................................................................................................................................94Self Test Questions ..............................................................................................................................................95References ............................................................................................................................................................95Notes ...................................................................................................................................................................95

    Part II: C++ Language Fundamentals

    5 Simple Programs

    Introduction ........................................................................................................................................................100A Minimal C++ Program ......................................................................................................................................100

    Disassembly is a Great Learning Tool ....................................................................................................... ........................................... 101

    Another C++ Program ........................................................................................................................................102

    Parts of the Program ...........................................................................................................................................................................103

    Comments .............................................................................................................................................................................................................................103Preprocessor Directive .......................................................................................................................................................................................................103Libraries ...............................................................................................................................................................................................................................103Using Directive.....................................................................................................................................................................................................................103main() function......................................................................................................................................................................................................................103Constants .............................................................................................................................................................................................................................103Variables ...............................................................................................................................................................................................................................103Statements and Expressions ..................................................................................................................................................................................................103

    Keywords ............................................................................................................................................................104Fundamental Types ...............................................................................................................................................104

    Determining Your Data Type Ranges ................................ ............................................................................................................ .......105Determining Data Type Size with thesi zeof

    Operator ..................................................................................................................106

  • 7/29/2019 C for Artist

    12/644

    Detailed Contents

    C++ For Artists

    2003 Rick Miller All Rights Reserved

    xi

    Literals ................................................................................................................................................................106

    Integer Literals ......................................................................................................................................................................................107

    Decimal.................................................................................................................................................................................................................................107Octal ....................................................................................................................................................................................................................................107Hexadecimal ..........................................................................................................................................................................................................................107

    A Word of Caution .............................................................................................. ............................................................................... 107Character Literals ................................................................................................................................................................................108

    Single Character Literals......................................................................................................................................................................................................108

    Multiple Character Literals...................................................................................................................................................................................................108Escape Sequences .................................................................................................................................................................................................................109

    Floating Point Literals ..........................................................................................................................................................................110String Literals ......................................................................................................................................................................................111Boolean Literals ...................................................................................................................................................................................111

    Expressions ..........................................................................................................................................................111Operators ...........................................................................................................................................................113

    Operator Precedence ..........................................................................................................................................................................114Use Parentheses ....................................................................................................................................................................................115Multiplicative Operators ......................................................................................................................................................................116

    Multiplication Operator.......................................................................................................................................................................................................116Division Operator ...............................................................................................................................................................................................................116Modulus Operator...............................................................................................................................................................................................................117

    Additive Operators ............................................................................................... ............................................................................... 117

    Addition Operator...............................................................................................................................................................................................................117Subtraction Operator .........................................................................................................................................................................................................118

    Shift Operators ....................................................................................................................................................................................118

    Left Shift Operator ..............................................................................................................................................................................................................118Right Shift Operator ...........................................................................................................................................................................................................119

    Relational Operators ............................................................................................................................................................................120

    Less Than Operator..............................................................................................................................................................................................................120Greater Than Operator........................................................................................................................................................................................................120Less Than or Equal To Operator .........................................................................................................................................................................................121Greater Than or Equal To Operator...................................................................................................................................................................................121

    Equality Operators ..............................................................................................................................................................................121

    Equal To Operator ..............................................................................................................................................................................................................121Not Equal To Operator.......................................................................................................................................................................................................121

    Bitwise AND Operator - & ..................................................................................................................................................................121Bitwise Exclusive OR Operator - ^ ......................................................................................................................................................122Bitwise Inclusive OR Operator - | ........................................................................................................... ........................................... 122Logical AND Operator - && ...............................................................................................................................................................123Logical OR Operator - || ..................................................................................................................................................................123

    Conditional Operator - ? : .................................................................................................... ............................................................. 123Assignment Operators ......................................................................... ................................................................................................. 124lvalue vs. rvalue .....................................................................................................................................................................................124

    Compound Assignment Operators.......................................................................................................................................................................................125

    Comma Operator - , ............................................................................................................................................................................125Increment and Decrement Operators (++, --) .......................................................................................................................................125

    Identifiers ............................................................................................................................................................126

    Identifier Naming Conventions .............................................................................................................................................................126

    Hungarian Notation .............................................................................................................................................................................................................126

    Constants ............................................................................................................................................................128Variables ..............................................................................................................................................................128

    Declaring .............................................................................................................................................................................................128Scope ...................................................................................................................................................................................................128

    Local Scope .........................................................................................................................................................................................................................129

    Function Scope....................................................................................................................................................................................................................130File Scope ............................................................................................................................................................................................................................130

    Multifile Variable Usage .......................................................................................................................................131

    Sharing File Scope Variables Across Multiple Files .............................................................................................................................131Limiting File Scope Variable Visibility to One File ...................................................................................................... .........................131

    The main() Function ..............................................................................................................................................132

    The Purpose of the main() Function .....................................................................................................................................................132Two Forms of main() ............................................................................................................................................................................132Exiting main() ........................................................................................................................................................................................133Calling Functions Upon Exiting main() ................................................................................................................................................133

  • 7/29/2019 C for Artist

    13/644

    Detailed Contents

    xii

    2003 Rick Miller All Rights Reserved

    C++ For Artists

    Simple Input and Output ......................................................................................................................................133

    cin ......................................................................................................................................................................................................134

    Trapping Bad Input ..............................................................................................................................................................................................................134

    cout ......................................................................................................................................................................................................135Learning More About cout and cin .....................................................................................................................................................135

    Summary ..............................................................................................................................................................135Skill Building Exercises ........................................................................................................................................136

    Suggested Projects ..............................................................................................................................................137Self Test Questions ..............................................................................................................................................138References ............................................................................................................................................................138Notes ...................................................................................................................................................................139

    6 Controlling The Flow of Program Execution

    Introduction ........................................................................................................................................................142Statements, Null Statements, and Compound Statements .......................................................................................142

    Statements .............................................................................................................................................................................................142Null Statements .....................................................................................................................................................................................142Compound Statements ..........................................................................................................................................................................143

    Selection Statements ............................................................................................................................................143

    if Statement ...........................................................................................................................................................................................143

    if Statements and Compound Statements ..............................................................................................................................................................................144

    if-else Statement .....................................................................................................................................................................................145

    Nesting if-else Statements .......................................................................................................................................................................................................146

    switch Statement ...................................................................................................................................................................................147

    Iteration Statements .............................................................................................................................................150

    while Statement .....................................................................................................................................................................................150

    Controlling while Statements with Sentinel Values ...............................................................................................................................................................151Nesting while Statements .......................................................................................................................................................................................................152Doing Something Forever ...................................................................................................................................................................................................152Exiting While Loops with the break Statement .....................................................................................................................................................................152

    do Statement .........................................................................................................................................................................................155

    Nesting do Statements ..........................................................................................................................................................................................................155

    for Statement ........................................................................................................................................................................................155

    Nesting for Statements..........................................................................................................................................................................................................157break ....................................................................................................................................................................................................................................157Doing Something Forever with a for Statement ..................................................................................................................................................................157

    continue ...............................................................................................................................................................................................158Avoiding break and continue ........ ............................................................................................................ ........................................... 158

    Writing Elegant Code ..........................................................................................................................................158Labeled Statements ...............................................................................................................................................159

    goto Statement .....................................................................................................................................................................................159Advice on Using Goto .........................................................................................................................................................................................................159

    Control Statement Usage Guide ...........................................................................................................................160Summary ..............................................................................................................................................................160Skill Building Exercises ........................................................................................................................................161Suggested Projects ..............................................................................................................................................162Self Test Questions ..............................................................................................................................................163References ............................................................................................................................................................164

    Notes ...................................................................................................................................................................164

    7 Pointers and ReferencesIntroduction ........................................................................................................................................................166But First, A Short Story ....................................................................................................................................166What is an object? ...............................................................................................................................................................................167What is a memory address? ..................................................................................................................................................................168How do you determine an objects memory address? ................................................................................................ .........................169

  • 7/29/2019 C for Artist

    14/644

    Detailed Contents

    C++ For Artists 2003 Rick Miller All Rights Reserved xiii

    What is a pointer? ...............................................................................................................................................................................171How do you declare a pointer? ..........................................................................................................................................................172How do you access the object a pointer points to? ......................... ................................................................................................. 173How do you dynamically create and delete objects? ............................................ ............................................................................... 174

    The new Operator................................................................................................................................................................................................................175A Neat Trick: Calling Object Constructors ........................................................................................................................................................................177

    Whats the difference between a pointer and a reference? ................................................................................................................... 177How do you declare and use references? ................................................................................................ ........................................... 178Summary ..............................................................................................................................................................178Skill Building Exercises ........................................................................................................................................179Suggested Projects ..............................................................................................................................................179Self Test ..............................................................................................................................................................180References ............................................................................................................................................................180Notes ...................................................................................................................................................................181

    8 ArraysIntroduction ........................................................................................................................................................184What Is An Array? ..............................................................................................................................................184Locating Array Elements .......................................................................................................................................................................185

    Declaring & Defining Statically Allocated Arrays ................................................................................................185Single-Dimensional Arrays ....................................................................................................................................................................185Accessing Array Elements ........................................................................................................ ............................................................. 186

    Subscript Method ................................................................................................................................................................................................................186Pointer Arithmetic Method ..................................................................................................................................................................................................187

    Beware the Uninitialized Array! ............................................................................................................................................................187Combining Array Definition with Array Declaration ................................................................................................. .........................187Arrays of Pointers ................................................................................................ ............................................................................... 188Multi-Dimensional Arrays ......................................................................................................................................................................189

    Arrays of Two Dimensions ...................................................................................................................................................................................................189Arrays of Three or More Dimensions ..................................................................................................................................................................................191

    Automatic Initialization of Multi-Dimensional Arrays .................................................................................. ........................................... 193Declaring and Defining Dynamic Arrays ...............................................................................................................196Dynamically Allocated Single Dimensional Arrays ................................................................................................................................196Dynamically Allocated Multi-Dimensional Arrays ..................................................................................................................................197Strings ................................................................................................................................................................200Summary ..............................................................................................................................................................200Skill Building Exercises ........................................................................................................................................201Suggested Projects ..............................................................................................................................................202Self Test Questions ..............................................................................................................................................203References ............................................................................................................................................................203Notes ...................................................................................................................................................................204

    9 FunctionsIntroduction ........................................................................................................................................................206What is a Function? .............................................................................................................................................206Interface vs. Implementation ..................................................................................................................................................................207

    Put Function Interface Declarations in Header Fi les............................................................................................................................................................207#ifndef...#define....#endif .....................................................................................................................................................................................................207Put Function Definitions in Implementation Files..................................................................................................................................................................207

    Characteristics of a Well-Written Function ..........................................................................................................................................208Declaring and Defining Functions .........................................................................................................................208Naming Functions .................................................................................................................................................................................208Function Declaration ...........................................................................................................................................................................209Function Definition ..............................................................................................................................................................................209Function Calling ..................................................................................................................................................................................209

  • 7/29/2019 C for Artist

    15/644

    Detailed Contents

    xiv 2003 Rick Miller All Rights Reserved C++ For Artists

    A Complete Example ...................... ............................................................................................................ ...........................................210Quick Review .......................................................................................................................................................................................211Local Function Variable Scoping ..........................................................................................................................212Declaring Local Variables ....................................................................................................................................................................212Hiding Global Variables with Local Variables .................. ............................................................................................................ .......212Using Scoping Blocks in Functions ....................................................................................................................................................212Static Function Variables .....................................................................................................................................................................213

    Scope of Function Parameters .............................................................................................................................................................214Quick Review .......................................................................................................................................................................................215Passing Arguments to Functions ..........................................................................................................................215Function Calling ..................................................................................................................................................................................215

    Responsibilities of the Calling Function ...............................................................................................................................................................................216Responsibilities of the Called Function ................................................................................................................................................................................216

    Passing Arguments by Value .................................................................................................................................................................216Another Example...................................................................................................................................................................................................................218

    Passing Arguments by Reference ..........................................................................................................................................................219Continuing The Story... .......................................................................................................................................................................................................220Passing Pointers...................................................................................................................................................................................................................220Passing References................................................................................................................................................................................................................221

    Passing Arrays to Functions ................................................................................................................................................................222Passing Multi-Dimensional Arrays To Functions ...................................................................................................................................223Another Example .......................................................................................................................................................... .........................224

    Using Function Return Values ..............................................................................................................................225Returning Objects ................................................................................................................................................................................226The return Keyword: Mantra on Proper Usage ..................................................................................................................................................................226Another Example...................................................................................................................................................................................................................227

    Returning Pointers ...............................................................................................................................................................................227How Not To Return a Pointer From A Function: Avoiding the Dangling Reference..........................................................................................................229

    Returning References ............................................................................................................................................................................229Quick Review .......................................................................................................................................................................................230Function Overloading ..........................................................................................................................................231Calling Functions Recursively ...............................................................................................................................232Another Example .......................................................................................................................................................... .........................233Function Pointers ................................................................................................................................................234Declaring Function Pointers ...............................................................................................................................................................235Assigning The Address of a Function to a Function Pointer ...................................................................................... .........................235Calling the Function via the Function Pointer ........................................................................................................... .........................236

    Arrays of Function Pointers ................................................................................................... .............................................................236Implementing Callback Functions with Function Pointers ............................................................................................................ .......237Creating A Function Library ................................................................................................................................239Steps to Creating a Library .................................................................................................................................................................239

    Create Empty Project...........................................................................................................................................................................................................239Add Implementation File ........................................................................................................................................................................................................240Set Library Target Settings...................................................................................................................................................................................................240Name Library and Set Project Type .....................................................................................................................................................................................241Build the Project..................................................................................................................................................................................................................241Use the Library.....................................................................................................................................................................................................................241

    Summary ..............................................................................................................................................................242Skill Building Exercises ........................................................................................................................................242Suggested Projects ..............................................................................................................................................243EISCS MKI Language Set .....................................................................................................................................................................244Sample Program ............................................................................................................................................................................