Lean &Mean Borland C++

16
Lean &Mean Borland C++ тшшж Bruneau Babet ШШШШЯЯШШШШШШШШШШШШШШШШШШШШШШШШШШ IIIIBrady

Transcript of Lean &Mean Borland C++

Page 1: Lean &Mean Borland C++

Lean &Mean Borland C++

тшшж Bruneau Babet

ШШШШЯЯШШШШШШШШШШШШШШШШШШШШШШШШШШ

IIIIBrady

Page 2: Lean &Mean Borland C++

Contents 1 Borland C++Basics 1

Using the Tools 2

Understanding the Role of Compilers,

Librarians, and Linkers 2

Using the Borland C++ IDE 4

Using the IDE with a Single Module 6

Projects and Multimodule Targets 7

Building More Than One Target 10

A Project with Executable and DLL.... 12

Using Command Line Tools 15

Configuration Files 17

Targeting Windows 18

Creating a Windows Application 18

Windows 3.1 (16) 19

Win32 20

Creating a Windows Dynamic Link

Library 21

Resource Compilers and Linkers 22

Help Compiler and Tools 23

Updating Existing Code for Borland C++ 24

Three char Types 25

Lean & Mean Borland C++ I

Page 3: Lean &Mean Borland C++

Array Version of the New and Delete Operators 25

Operator New and Exceptions 26

Using the longjmp and setjmp Functions 27

Renamed Global Variables 28

Summary 29

2 The С Language 31

A Simple С Program 32

Components of the Program 35

Comments 35

#include and Header Files 35

The main Function 36

Format of a Function 36

Representing Information in С 37

Constants 37

Simple Data Types 39

Variables 40

Typed Constants 41

Functions 42

Input and Output 42

Conversion Specification 43

Escape Sequences 46

The Functions scanf, gets, atoi, atol, and atof 47

Lean & Mean Borland C++

Page 4: Lean &Mean Borland C++

Sample Function 49

Function Prototype 49

Function Definition 50

Expressions and Operators 52

Conditional Statements and Loops 56

Using if and else Statements 56

Using switch and case Statements 59

Using the while Keyword 61

Using the Null Statement 62

Using the for Keyword 62

Using a do/while Loop 64

Interrupting a Block 65

Using goto and label Statements 68

Variable Scope 69

Local Variables 69

Global Variables 70

Variable Visibility 71

Variable Duration 72

Variable Modifiers 72

Volatile Variables 73

Arrays 74

Pointers 76

Array-Pointer Relationship 79

User-Defined Types 81

Redefining Data Types 81

Lean & Mean Borland C++ j

Page 5: Lean &Mean Borland C++

Enumerated Types 82

Structures 84

Unions 85

Bit Fields 87

Summary 88

3 Preprocessor Directives 91

Macros: #define 92

Nesting Macros 93

Line Continuation Character 93

Undefining a Macro 93

Compiler Settings 94

Stringizing (#) 94

Token Pasting (##) 95

Macro Caveats 96

The #include Directive 98

Conditional Compilation 99

The defined Operator 101

#ifdef and #ifndef 102

The #error Directive 102

The #line Directive 103

The #pragma Directive 103

Predefined Macros 108

ANSI Macros 108

Borland C++ Macros 110

I Lean & Mean Borland C++

Page 6: Lean &Mean Borland C++

Common Use of Preprocessor Directives I l l Preventing Multiple Inclusion of

Header Files 112

Easily Commenting Out Sections of Code 112

Ensuring Proper Compiler Settings 113

Diagnostic Macros 114

Summary 115

4 The С Language Extensions 117

Segments 118

Memory Models 120

Near and Far Pointers 121

Huge Pointers 123

Huge Pointers under DOS 124

Huge Pointers under Windows 125

Macros To Manipulate Pointers 126

Which Segment? 127

Variable Modifiers 129

far 129

_ Jmge 131

Function Modifiers 131

_ _interrupt 133

_ _saveregs, _ _loadds 135

export 136

Calling Conventions 136

cdecl 137

Lean & Mean Borland C++ С

Page 7: Lean &Mean Borland C++

pascal 137

__fastcall 139

__stdcall 140

Inline Assembly 141

Pseudo Registers 144

Summary 146

5 Moving to C++ 147

How C++ Differs from ANSI С 148

C++ Keywords 148

Function Prototypes 149

void* 149

Global const Variables and Linkage 150

Character Constant Type 151

Skipping Initialization 151

C++ as an Improved С 152

Default Arguments 152

Reference 154

Reference Parameters 155

Function with Reference Return Type .... 158

Inline Functions 160

Restrictions on Inline Functions 160

Inline Function Definition 161

The :: Operator 162

Page 8: Lean &Mean Borland C++

Overloaded Functions 163

Restrictions 164

Implementation: Name Mangling 165

Variable Declaration 166

Const Values 167

Enum, struct, and union Tags 167

Anonymous Unions 168

Flexible Memory Allocation Operators 169

Interfacing C++ to C, Assembly, and Pascal 176

Linkage Specification 176

Header Files 178

Summary 179

Object Oriented Programming in C++ 181

C++ Class 182

Defining a Class 182

Access Control 184

Classes, Structures, and Unions 185

Class Members 186

Data Members 186

Member Functions 186

Class Scope 189

Accessing Data Members 190

Calling Member Functions 191

Using Pointers to Member Functions 192

The this Pointer 194

Lean & Mean Borland C++

Page 9: Lean &Mean Borland C++

Special Member Functions 195

Constructor 196

Member Initializer List 198

Const, Reference, and Object

Data Members 199

Copy Constructor 200

Assignment Operator 201

Destructor 204

Class's new Operator 204

Class's delete Operator 204

Conversion Functions 209

Conversion Constructors 209

Cast Operators 210

Friends 212

Friend Classes 212

Friend Functions 212

Rules about Friends 213

Overloading Member Functions 213

Overloading Operators 214

Rules 215

Samples 216

Static Members 219

Static Data Members 219

Static Member Functions 220

const Objects and const Member Functions 221

Э Lean & Mean Borland C++

Page 10: Lean &Mean Borland C++

Class Inheritance 223

Public, Protected, and Private

Base Classes 224

Simple Inheritance 226

Constructors and Destructors

in Inheritance 230

Virtual Functions 231

Implementation 235

Polymorphism and Late Binding 235

Multiple Inheritance 237

Ambiguity and Scope Resolution 240

Virtual Base Class 241

Abstract Classes and Pure

Virtual Functions 243

Summary 245

C++ Stream Classes 247

IOStream Header Files 248

Predefined Stream Objects 249

Insertion and Extraction Operators 251

Operators Overloaded for

Built-in Types 252

Chaining Operator Calls 253

Adding Stream Support for User-Defined Types 254

Lean & Mean Borland C++ j

Page 11: Lean &Mean Borland C++

э

Formatting 258

Formatting Member Functions 258

Field Width 258

Padding Character 261

Digits of Floating Point Precision 262

Formatting Flags 264

Manipulators 270

Simple Manipulators 270

Parametized Manipulators 272

Stream Errors 274

Querying and Setting a Stream's State 276

Common Manipulation of a

Stream's State 277

File I/O with C++ Streams 278

File Stream Constructors 278

Opening a File 279

Open Modes 283

Using Open Modes 284

Changing a Stream's Buffer 286

Closing a File 286

Unformatted I/O 287

Binary File I/O 287

Reading Raw Data 289

Writing Raw Data 290

Reading a Character 292

Lean & Mean Borland C++

Page 12: Lean &Mean Borland C++

Reading a String 294

get 294

getline 296

Commonly Used Stream Functions 297

Skipping Characters on Input 298

Requesting an Extraction Count 298

Peeking at the Next Character 298

Putting Back an Extracted Character 299

Seeking within a Stream 299

Telling a Stream's Position 299

InCore Formatting 300

istrstream 300

ostrstream 301

Summary 305

C++Templates 307

Function Templates 308

Function Template: Syntax 308

Defining Function Templates 309

Using Function Templates 312

Overloading Function Templates 318

Specializing Function Templates 320

Resolving a Reference to a Function 322

Class Templates 322

Class Template: Syntax 322

Page 13: Lean &Mean Borland C++

Defining Class Templates 323

Using Class Templates 328

Specializing Class Templates 333

Templates and Compiler Switches 338

Smart Templates 339

Global and External Templates 340

Drawbacks of Templates 343

Summary 344

9 Exception Handling 345

Using Exceptions and the Stack 346

Working with C++ Exception Handling 350

Using try 350

Using Catch 351

Using throw 353

throw with operand 353

throw with No Operand 354

Catching a throw 354

Matching Exception Types 360

Using terminate() and Unhandled

Exceptions 363

Working with Exception Specifications 364

Working with Unexpected Exceptions 365

Working with Constructors

and Exceptions 366

Local Objects 366

Dynamic Objects 369

Lean & Mean Borland C++

Page 14: Lean &Mean Borland C++

Working with Exception Hierarchies 374

Working with Predefined

Exception Classes 376

xmsg 376

xalloc 378

Bad_cast and Bad_typeid 380

Using Exception Local Information 380

Using Exceptions and Compiler Options 383

Examining Structured Exception Handling 385

Using Frame-based Exception

Handling (_ _try/_ _except) 386

Raising an Exception 386

Flow of Control 387

Filter Expression 390

Catching Processor Exceptions 392

Using Termination Handlers ( try/ finally) 394

Normal versus Abnormal Termination 395

Using Structured and C++ Exception

Handling 398

Summary 399

Runtime Type Information and

Typecast Operators 401

Programming with RTTI 402

Using typeid Operator and Type_info Class 402

Page 15: Lean &Mean Borland C++

Bad_typeid Exception 403

Using typeid to Compare Types 403

Using Type_info 406

Type_info: :before

(const Type_info&) 407

Type_info::name() 408

Using RTTI and Compiler Options 408

Using the _ _rtti Modifier 409

Using New Style Casts 411

Examining New Style Casts 412

Using dynamic_cast 416

Looking at a dynamic_cast Example 416

Downcasting from a

Virtual Base Class 419

Cross-Hierarchy Casting 420

Using static_cast 423

Using const_cast 426

Using reinterpret_cast 427

Summary 428

A Name Mangling Scheme of Borland C++ 429

General Overview of Scheme 430

@[classname@] 430

EncodedFuncName 431

SqEncodedArgType 433

User-Defined Types and Arrays 435

Lean & Mean Borland C++

Page 16: Lean &Mean Borland C++

В RTL Helper Functions 437

Generating Assembly Language Output Files 439

Allocation Arrays of Classes 440

Copying Structures 441

Checking for Stack Overflow 443

Summary 444

Index 445

Lean & Mean Borland C++ 1 XXI