Crash Course in C++ Borland C++ 4

download Crash Course in C++ Borland C++ 4

of 289

  • date post

    17-Feb-2018
  • Category

    Documents

  • view

    214
  • download

    0

Embed Size (px)

Transcript of Crash Course in C++ Borland C++ 4

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    1/289

    Con

    compiler

    /* Styl e */

    x posi ti on

    , , ,

    , , ,

    , , ,

    ,

    ,

    ,

    void TB

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    2/289

    ii Contents

    Crash Course in Borland C++ 4

    1994 by Que Corporation

    All rights reserved. Printed in the United States of America. No part of this

    book may be used or reproduced in any form or by any means, or stored in

    database or retrieval system, without prior written permission of the pub-

    lisher except in the case of brief quotations embodied in critical articles an

    reviews. Making copies of any part of this book for any purpose other than

    your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 W. 103rd Street, Indianapolis, IN

    46290.

    Library of Congress Catalog No.: 94-65520

    ISBN: 1-56529-773-3

    This book is sold as is,without warranty of any kind, either express or im-

    plied, respecting the contents of this book, including but not limited to im

    plied warranties for the books quality, performance, merchantability, or

    fi f i l i h i i d l

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    3/289

    Conte

    Dedication

    To my special new fr iends Drew and M arlene Braytenbah

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    4/289

    iv Contents

    Credits

    Acquisitions EditorAngela Lee

    Product DirectorBryan Gambrel

    Production EditorsLorna Gentry

    i d if

    Cover DesignerJean Bisesi

    Editorial AssistantMichelle Williams

    Production TeamGary Adair, Brad Chinn, Kim Co

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    5/289

    Cont

    About the Author

    Namir C. Shammasis a software engineer and an expert in object-oriented

    programming. He has authored and coauthored 30 books that deal with vari-

    ous programming languages. His other Que publications include Using

    Borland C++and Using V isual C++.

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    6/289

    vi Contents

    AcknowledgmentsI would like to thank many people at Que for working with me on this bo

    Many thanks to Joe Wikert, who contacted me to write the book and was

    actively involved in editing it. I thank the technical editor, Robert Zigon, f

    his valuable comments and corrections. My gratitude to editor Lorna Gent

    for ensuring the clarity of the text. Finally, many thanks to all the people a

    Que who were involved with the book.

    Trademarks

    All terms mentioned in this book that are known to be trademarks or serv

    marks have been appropriately capitalized. Que cannot attest to the accura

    of this information. Use of a term in this book should not be regarded as

    affecting the validity of any trademark or service mark.

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    7/289

    Conte

    Contents at a GlanceIntroduction ............................................................................................ 1

    1 The Borland C++4 IDE ............................................................................ 5

    2 Getting Started....................................................................................... 17

    3 Variables and Operators ........................................................................ 29

    4 Managing I/O ........................................................................................ 43

    5 Decision Making.................................................................................... 51

    6 Loops ..................................................................................................... 63

    7 Enumerated and Structured Data Types................................................ 79

    8 Pointers.................................................................................................. 89

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    8/289

    viii Contents

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    9/289

    Conte

    Contents

    Introduction 1

    Who Should Use This Book? ........................................................1

    What You Should Know to Use This Book...................................1Organization of This Book............................................................1Conventions Used ........................................................................3And Now... ....................................................................................4

    1 The Borland C++ 4 IDE 5

    C++History and Basics .................................................................5The Borland C++4 IDE .................................................................6Summary.....................................................................................14

    2 Getting Started 17

    A Simple C++Program................................................................17

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    10/289

    x Contents

    5 Decision Making

    The Single-AlternativeifStatement .........................................The Dual-Alternativeif-elseStatement ...................................The Multiple-Alternativeif-elseStatement .............................The Multiple-AlternativeswitchStatement ..............................Summary....................................................................................

    6 Loops

    The forLoop ..............................................................................

    Arrays .........................................................................................Using forLoops to Create Open Loops.....................................Skipping Loop Iterations ...........................................................Exiting Loops .............................................................................

    The do-whileLoop .....................................................................The whileLoop ..........................................................................Summary....................................................................................

    7 Enumerated and Structured Data Types

    Type Definition in C++..............................................................Enumerated Data Types.............................................................Structures ...................................................................................

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    11/289

    Conte

    Using Structures as Arguments.................................................113Passing Arguments by Reference ..............................................114

    Accessing Command-Line Arguments .....................................117Pointers to Functions................................................................118Summary...................................................................................121

    10 Building Classes 123

    Basics of Object-Oriented Programming ..................................123Declaring Base Classes ..............................................................124Constructors .............................................................................130

    Destructors ................................................................................132Static Members..........................................................................138Friend Functions.......................................................................142Operators and Friend Operators...............................................146Summary...................................................................................153

    11 Advanced Object-Oriented Programming 155

    Declaring a Class Hierarchy ......................................................155

    Virtual Functions ......................................................................160Abstract Classes.........................................................................165Overloading Member Functions and Operators.......................168Nested Data Types 172

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    12/289

    xii Contents

    14 Using ObjectWindows Library 2

    The ObjectWindows Hierarchy ................................................2Sending and Responding to Messages......................................2

    The Minimal OWL Application ................................................2A Simple Interactive Windows Application .............................2

    The CALC Calculator Application ............................................2The Text Editor Application .....................................................2Summary...................................................................................2

    Index 2

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    13/289

    Introduction

    This book teaches you to program with C++in general and with Borland C++

    4 in particular. As part of the Crash Coursebook series, this book presents its

    topics at a fast pace, using relatively few and short examples.

    This book takes a no frills approach to teaching the most important aspects

    of Borland C++4. You start learning the language in the first chapter. This

    book focuses on the key features of Borland C++4 so that the reader can

    begin writing practical applications in the shortest amount of time possible.

  • 7/23/2019 Crash Course in C++ Borland C++ 4

    14/289

    2 Introduction

    Chapter 2 presents the first C++program and explains the