1. Trang chủ
  2. » Công Nghệ Thông Tin

Theory and problems of programming with c++ 1996

446 959 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 446
Dung lượng 41,6 MB

Nội dung

Like all Schaum’s Outline Series books, this is intended to be used primarily for self study, preferably in conjunction with a regular course in C++ Programming. The book covers nearly all aspects of ANSIIS0Standard C++. It includes over 200 examples and solved problems. The author firmly believes that programming is best learned by practice, following a wellconstructed collection of examples with complete explanations. This book is designed to provide that support. C++ was created by Bjarne Stroustrup in the 1980s. Based upon C and Simula, it has become the most popular language for objectoriented programming. The final ANSIIS0Standard was just recently completed, so some of the standard features described in this book may not yet be available on all compilers. In particular, the powerful Standard Template Library is just now becoming available from some vendors. Although most people who undertake to learn C++ have already had some previous programming experience, this book assumes none. It approaches C++ as one’s first programming language. Therefore, those who have had previous experience may need only skim the first few chapters. C++ is a difficult language for at least two reasons. It inherits from the C language economy of expression that novices often find cryptic. And as an objectoriented language, widespread use of classes and templates presents a formidable challenge to those who have thought in those terms before. It is the intent of this book to provide the assistance necessary firsttime programmers to overcome these obstacles.

c/ SCHAUM’S OUTLINE OF THEORY AND PROBLEMS of PROGRAMMING WITH c++ JOHN R HUBBARD, Ph.D Professor of Mathematics and Computer Science University of Richmond SCHAUM’S OUTLINE SERIES M C GRAW-HILL New York St Louis San Francisco Auckla& .l&go.,t$ carqpgs Lisbon London Madrid Mexico City ’ ‘luri&n* Montreal ’ New Delhi San Juan Singapore Sydn& ,?i?jkyo , TGroqc & L JOHN R HUBBARD is Professor of Mathematics and Computer Science at the University of Richmond He received his Ph.D from The University of Michigan (1973) and has been a member of the Richmond faculty since 1983 His primary interests are in numerical algorithms and database systems Dr Hubbard is the author of several other books, including A Gentle Introduction to the VAX System and The VAX Book Schaum’s Outline of Theory and Problems of PROGRAMMING WITH C++ Copyright 1996 by The McGraw-Hill Companies, Inc All rights reserved Printed in the United States of America Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a data base or retrieval system, without the prior written permission of the publisher 234567891011121314151617181920 PRS PRS 9 ISBN o-07-030837-3 Sponsoring Editors: John Aliano and Arthur Biderman Production Supervisor: Donald F Schmidt Editing Supervisor: Maureen Walker Library of Congress Cataloging-in-Publication Data Hubbard, J R (John Rast), date Schaum’s outline of theory and problems of programming with C++ / John Hubbard cm - - (Schaum’s outline series) P* Includes index ISBN o-07-030837-3 C++ (Computer program language) I Title QA76.76.Cl5H835 1996 005.13’3 - - dc20 96-13964 CIP McGraw-Hill A Division of The McGrawHill Companies Preface Like all Schaum’s Outline Series books, this is intended to be used primarily for self study, preferably in conjunction with a regular course in C++ Programming The book covers nearly all aspects of ANSI/IS0 Standard C++ It includes over 200 examples and solved problems The author firmly believes that programming is best learned by practice, following a well-constructed collection of examples with complete explanations This book is designed to provide that support C++ was created by Bjarne Stroustrup in the 1980s Based upon C and Simula, it has become the most popular language for object-oriented programming The final ANSI/IS0 Standard was just recently completed, so some of the standard features described in this book may not yet be available on all compilers In particular, the powerful Standard Template Library is just now becoming available from some vendors Although most people who undertake to learn C++ have already had some previous programming experience, this book assumes none It approaches C++ as one’s first programming language Therefore, those who have had previous experience may need only skim the first few chapters C++ is a difficult language for at least two reasons It inherits from the C language an economy of expression that novices often find cryptic And as an object-oriented language, its widespread use of classes and templates presents a formidable challenge to those who have not thought in those terms before It is the intent of this book to provide the assistance necessary for first-time programmers to overcome these obstacles Readers may download the source code for the examples and solved problems in this book from the author’s World Wide Web home page: http : / /WWW richmond edu/-hubbard/ I wish to thank all my friends, colleagues, students, and McGraw-Hill staff who have helped me with the critical review of this manuscript, including John Aliano, Arthur Biderman, Peter Dailey, Chris Hanes, Walker Holt, John B Hubbard, Arni Sigurjonsson, Andrew Somers, Maureen Walker, and Nat Withers Their debugging skills are gratefully appreciated Finally I wish to express my gratitude to my wife and colleague, Anita Hubbard, who reviewed the complete manuscript and worked through most of the problems, including many that she contributed herself I am greatly in her debt JOHN R HUBBARD Richmond, Virginia 111 Dedicated to Anita H Hubbard 11 SIMPLE PROGRAMS 1.2 THE OUTPUT OPERATOR 13 CHARACTERS AND STRING LITERALS 14 STRING LENGTH 15 1.6 COMMENTS VARIABLES, OBJECTS, AND THEIR DECLARATIONS 1.7 KEYWORDS AND IDENTIFIERS 18 INITIALIZING IN THE DECLARATION 10 19 CHAINED ASSIGNMENTS 11 1.10 THE SEMICOLON 12 1.11 PROGRAM STYLE 12 1.12 INTEGER TYPES 13 1.13 SIMPLE ARITHMETIC OPERATORS 14 1.14 OPERATOR PRECEDENCE AND ASSOCIATIVITY 16 1.15 THE INCREMENT AND DECREMENT OPERATORS 17 1.16 COMPOUND ASSIGNMENT EXPRESSIONS 20 1.17 INTEGER OVERFLOW AND UNDERFLOW 20 1.18 THE char TYPE 21 2.1 INPUT 31 2 THE if STATEMENT 33 THE if else STATEMENT 34 RELATIONAL OPERATORS 35 COMPOUND STATEMENTS 36 KEYWORDS 37 COMPOUND CONDITIONS 38 BOOLEAN EXPRESSIONS 40 NESTED CONDITIONALS * 41 2.10 THE switch STATEMENT 43 2.11 THE CONDITIONAL EXPRESSION OPERATOR 44 2.12 SCOPE 2.13 ENUMERATION TYPES 2.14 INTEGER TYPE CONVERSIONS V 44 45 47 CONTENTS vi Chapter Iteration and Floating Types 31 3 3 39 3’10 3’11 3’12 Chapter Chapter THE while STATEMENT THE while STATEMENT THE for STATEMENT THE break STATEMENT THE cant inue STATEMENT THE goto STATEMENT REAL NUMBER TYPES TYPE CONVERSIONS ROUNDOFF ERROR THE E-FORMAT FOR FLOATING-POINT VALUES CONSTANTS, VARIABLES, AND OBJECTS GENERATING PSEUDO-RANDOM NUMBERS Functions 4 4 4 49 4’10 4’11 4’12 4’13 4’14 4’15 4’16 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 57 57 58 59 61 62 63 66 68 69 70 70 71 89 STANDARD C LIBRARY FUNCTIONS 89 USER-DEFINED FUNCTIONS 92 TEST DRIVERS 92 FUNCTION DECLARATIONS AND DEFINITIONS ’ 94 SEPARATE COMPILATION 96 LOCAL VARIABLES AND FUNCTIONS 97 void FUNCTIONS 99 BOOLEAN FUNCTIONS * 100 I/O FUNCTIONS 103 PASSING BY REFERENCE 105 PASSING BY CONSTANT REFERENCE 108 INLINE FUNCTIONS 109 SCOPE 110 OVERLOADING 111 THE main( ) AND exit () FUNCTIONS 112 DEFAULT ARGUMENTS 113 Arrays 51 5 5 5 59 5’10 5’11 oeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeoeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee INTRODUCTION 127 127 127 INITIALIZING AN ARRAY 129 PASSING AN ARRAY TO A FUNCTION 131 C++ DOES NOT CHECK THE RANGE OF AN ARRAY INDEX 132 THE LINEAR SEARCH ALGORITHM 133 THE BUBBLE SORT ALGORITHM 134 THE BINARY SEARCH ALGORITHM 136 USING ARRAYS WITH ENUMERATION TYPES 137 TYPE DEFINITIONS 139 M U L T I D I M E N S I O N A L A R R A Y S 140 PROCESSING ARRAYS vii CONTENTS Chapter Pointers and References eeeeeeeeeeeeeeeeeeeeeeoeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeoeeeeeeeeeeeeeee 157 61 INTRODUCTION 157 62 REFERENCES 158 63 POINTERS 159 64 DERIVED 161 65 OBJECTS AND LVALUES 161 66 RETURNING A REFERENCE 162 ARRAYS AND POINTERS 163 THE new OPERATOR 166 69 THE delete O P E R A T O R 6’10 6’11 DYNAMIC ARRAYS 167 167 USING const WITH POINTERS ARRAYS OF POINTERS AND POINTERS TO ARRAYS 169 6’13 POINTERS TO POINTERS 170 6’14 POINTERS TO FUNCTIONS 171 6’15 NUL, NULL,mD void 172 6’12 Chapter Strings eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 71 73 169 185 INTRODUCTION 185 REVIEW OF POINTERS * * * 185 7:4 STRINGS 188 STRING I/O 189 75 SOME tin MEMBER FUNCTIONS * 7:6 7.7 CHARACTER FUNCTIONS DEFINED IN ectype h> 194 ARRAYS OF STRINGS * 195 THE C-STRING HANDLING LIBRARY 198 Chapter TYPES 190 Classes eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 220 81 INTRODUCTION CLASS 220 83 DECLARATIONS C O N S T R U C T O R S 220 8 CONSTRUCTOR 225 85 ACCESS FUNCTIONS 226 86 PRIVATE MEMBER 227 THE COPY CONSTRUCTOR 227 8 THE CLASS DESTRUCTOR 89 CONSTANT 230 231 8’10 8’11 231 8’12 STRUCTURES POINTERS TO OBJECTS STATIC DATA MEMBERS 8’13 static 236 INITIALIZATION LISTS FUNCTIONS OBJECTS FUNCTIONMEMBERS 223 232 234 CONTENTS Vlll Chapter Overloading Operators 91 92 93 94 95 96 97 98 99 9'10 Chapter 10 Chapter 11 bbbbbbebeobobbbbbob*bbbbob*bbbb*bb*obbbbbbbbbbbbbob**bbbbbbbbbbbbbbbbbbbb**bbbbbbbbbbb 249 249 INTRODUCTION 249 OVERLOADING THE ASSIGNMENT OPERATOR 250 THE this POINTER 251 OVERLOADING ARITHMETIC OPERATORS OVERLOADING THE ARITHMETIC ASSIGNMENT OPERATORS .253 254 OVERLOADING THE RELATIONAL OPERATORS 254 OVERLOADING THE STREAM OPERATORS 256 CONVERSION OPERATORS OVERLOADING THE INCREMENT AND DECREMENT OPERATORS .258 260 OVERLOADING THE SUBSCRIPT OPERATOR A String Class bb*bb*b**bo*b**b*bbbbbbbbbbbbbb*bbbbbbbbbbb*bbbb*bbbbbbb*b*oob*obo*bb***bbbbbbbb*bbbbbbbbbbbbbb 271 10.1 INTRODUCTION 10.2 THE String CLASS INTERFACE 10.3 THE CONSTRUCTORS AND DESTRUCTOR 10.4 THE COPY CONSTRUCTOR 10.5 THE ASSIGNMENT OPERATOR 10.6 THE ADDITION OPERATOR 10.7 AN APPEND OPERATOR 10.8 ACCESS FUNCTIONS 10.9 THE COMPARISON OPERATORS 10.10 STREAM OPERATORS 271 271 272 275 276 279 281 282 284 285 Composition and Inheritance 295 *************** ***********.****************.*********.******************** 295 11.1 INTRODUCTION 295 11.2 COMPOSITION 298 11.3 INHERITANCE 299 11.4 protected CLASS MEMBERS 11.5 OVERRIDING AND DOMINATING INHERITED MEMBERS 30 11.6 private ACCESS VERSES protected ACCESS 304 305 11.7 virtual FUNCTIONS AND POLYMORPHISM 308 11.8 VIRTUAL DESTRUCTORS 310 11.9 ABSTRACT BASE CLASSES 313 11.10 OBJECT-ORIENTED PROGRAMMING Chapter 12 Stream I/O 12.1 12.2 12.3 12.4 12.5 12.6 12.7 12.8 ***.***********.************.******.*******************.*.**************************m********************* 328 STREAM CLASSES 328 328 THE ios CLASS 332 ios FORMAT FLAGS 336 ios STATE VARIABLES 339 THE istreamAND ostream CLASSES 340 UNFORMATTED INPUT FUNCTIONS 345 UNFORMATTED OUTPUT FUNCTIONS 347 S T R E A M M A N I P U L A T O R S CONTENTS Chapter 13 Templates and Iterators 13.1 13.2 13.3 13.4 13.5 13.6 13.7 13.8 Chapter 14 *b***bbbbbbbbbbbbbbbbbbbbbbbbbbo*bbbb*bbbbb*****bbb**bbb**b**bo****b***bb*bbbbo****b ix 355 INTRODUCTION 355 FUNCTION TEMPLATES 355 CLASS TEMPLATES 358 CONTAINER CLASSES * * 360 SUBCLASS TEMPLATES 362 PASSING TEMPLATE CLASSES TO TEMPLATE PARAMETERS 364 A CLASS TEMPLATE FOR LINKED LISTS 365 ITERATOR CLASSES 369 Libraries b**b**b***bbbbb*b****b******bbbb*********bbbbb*bbbo****bbb**bbbb*bb***b**b**bbb*bbb*****b**bbb*bbb**bb*****bb* 386 386 14.2 THE STANDARD C++ LIBRARY 386 ** * * 14.3 PROPRIETARY LIBRARIES 388 14.1 INTRODUCTION 14.4 CONTENTS OF THE STANDARD C HEADERS 389 14.5 STRING STREAMS 393 14.6 FILE PROCESSING 395 14.7 THE STANDARD TEMPLATE LIBRARY ~~ ~~~~~.~ ~~~~ ~~ 399 Appendix A The ASCII Code Appendix B C++ Keywords Appendix C C++ Operators Appendix D C++ Types Appendix E References b**ob*b*obbbboob*bboo*bbbbbbb*bb**o*bb***bb*bb***bbbbbbb**b*bbb**bb***bb***bb**bb**bbb*b*bbb*bbb* bbbbbb****oo*obbo**bbbb***bbobo*bbb***bb*****b**bbbbb*bbb*o*bbbbbbbbb*bbb**b*b**bb*bbbbbbb****bbbb*b bobbbbbo*bbbo**bbbbbbbbbbbbbbob**bbbbbbbbbb*b**mo**bb**bbeb*b*bb*****b*bbb*b*beb*ee**ee**bbb*bb***b* *e***e*bbbbe*bbebb***bebe*bb*b*eb**eee**bbb***beee**ee**beebbbbbb****eb*bbbb*bbbb*bbb**ee***b**b*****e** *bbb******bbbe**b*bb****b*b*bbb****bbe**bbbb*bbbb*bb**ee****e*bbbbbb**ee***ee***e**bb***e********bbbb**ee** Appendix F Pre-Defined Functions Appendix G Hexadecimal Numbers Index beebbbbbbbb**beeeee*bbbebbb*be****eee*ebebeeeeee*beebbbbbbb*bbbe**ee*e*e**bbbbbbb**bee* b**bbbb*bbb*b*bbbb****b*b****b***bbb***bbbbbbbbb***bbbb*bbb*bbbbbbb*****b**bb*b***e***b ****bb**bbbb***b****b*b****eeb**bbeb*b*b*b*bbb*e**bbbbb*bb*bb********e**bbbb*b*b*e**bb***e****b*********b**be******* 405 409 411 413 414 41s 423 425 422 [APP F PRE-DEFINED FUNCTIONS labs0 long labs(long n); Returns absolute value of n cstdlib.h> log0 double log(double x); Returns the natural logarithm (base e) of x log10 (> double loglO(double x); Returns the common logarithm (base 10) of x cmath.h> memchr() void* memchr(const void* s, int c, size-t k); Searches the k bytes of memory beginning at s for character c If found, the address of its first occurrence is returned Returns NULL otherwise cstring.h> memcmp() int memcmp(const void* sl, const void* s2, size-t k); Compares the k bytes of memory beginning at sl with the k bytes of memory beginning at s2 and returns a negative, zero, or a positive integer according to whether the first string is lexicographically less than, equal to, or greater than the second string cstring.h> memcpy ( > void* memcpy(const void* sl, const void* s2, size-t k); Copies the k bytes of memory beginning at s2 into memory location sl and returns sl cstring.h> memmove() int memmove(const void* sl, const void* s2, size-t k); Same as memcpy() except strings may overlap cstring.h> open ( void fstream: :open(const char* f, int m, int p=filebuf::openprot); void ifstream: :open(const char* f, int m=ios::in, int p=filebuf::openprot); void ofstream: :open(const char* f, int m=ios::out, int p=filebuf::openprot); Opens the file f in mode m with protection p cfstream.h> peek ( int istream:: peek(); Returns next character (or EOF) from stream without extracting it POW0 double pow(double x, double y); Returns x raised to the power y (X Y) cmath.h> precision0 int ios: :precision(); int ios: :precision(int k); Returns the current precision for the stream The second version changes the current precision to k and returns the old precision tolower- int tolower(int c); Returns the lowercase version of c if c is an uppercase alpha- cctype.h> betic character; otherwise returns c toupper ( > int toupper(int c); Returns the uppercase version of c if c is a lowercase alphabetic character; otherwise returns c AppendixG Hexadecimal Numbers Humans normally use the base 10 numbering system This is called the decimal system for the Greek word deka for “ten.” Our ancient ancestors learned it by counting with their 10 fi.ngers Computers have only fingers (Le., there are only possible values for each bit), so the binary system works well for computers But the trouble with binary numbers is that their representations require long strings of bits For example, 1996 is represented as IIIIIOOIIOO in binary Most humans find long strings like that difficult to process Binary numbers are easy to convert to other bases if the base is a power of For example, conversion between binary and octal (base = 23) merely requires grouping the binary bits into groups of and interpreting each triplet as an octal digit For example, to convert the binary numeral IIIIIOOIIOO write ~I,~II,OOI,IOO = 3714 Here, 11 converts to 3, III converts to 7, o o converts to I, and I o o converts to Conversion from octal back to binary is just as simple For example, o converts to I o 11 o o o o 0, which is 1448 in decimal Note that octal numerals use only the first decimal digits: 0, I, 2, 3, 4, 5, 6, After 8, the next power of is 16 Using that base makes the numerals even shorter This is called the hexadecimal system (from the Greek hex + deka for “six” + “ten”) Conversion between binary and hexadecimal is just as simple as it is between binary and octal For example, to convert the binary numeral ~OII~OIOIOO to hexadecimal, group the bits into groups of (from right to left) and then translate each group into the corresponding hexadecimal digit: 101,11~1,0100 = 5d4 Here, I o I converts to 5, I I o I converts to 11, and o I o o converts to The hexadecimal digits I 0, I 1, I 2, I 3, I 4, and I are denoted by the first six letters of the alphabet: a, b, c, d, e, f The output manipulators dec, hex, and act are used for converting different bases: EXAMPLE G.1 This shows how both the value and the address of a variable can be printed: #include main0 { int n = 1492; // tout [...]... c++ ... algorithms and database systems Dr Hubbard is the author of several other books, including A Gentle Introduction to the VAX System and The VAX Book Schaum’s Outline of Theory and Problems of PROGRAMMING. .. Supervisor: Maureen Walker Library of Congress Cataloging-in-Publication Data Hubbard, J R (John Rast), date Schaum’s outline of theory and problems of programming with C++ / John Hubbard cm - - (Schaum’s... conjunction with a regular course in C++ Programming The book covers nearly all aspects of ANSI/IS0 Standard C++ It includes over 200 examples and solved problems The author firmly believes that programming

Ngày đăng: 04/02/2016, 11:13

TỪ KHÓA LIÊN QUAN

w