c++: the core language - gbv · implicit default constructors and destructors 73 beyond chaining 73...

6
C++ : The Core Languag e Gregory Satir and Doug Brown

Upload: others

Post on 28-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++: The Core Language - GBV · Implicit Default Constructors and Destructors 73 Beyond Chaining 73 Explicit Invocation 74 Flow of Control 76 Time of Invocation 77 7. Better Abstraction

C++ : The Core Language

Gregory Satir and Doug Brown

Page 2: C++: The Core Language - GBV · Implicit Default Constructors and Destructors 73 Beyond Chaining 73 Explicit Invocation 74 Flow of Control 76 Time of Invocation 77 7. Better Abstraction
Page 3: C++: The Core Language - GBV · Implicit Default Constructors and Destructors 73 Beyond Chaining 73 Explicit Invocation 74 Flow of Control 76 Time of Invocation 77 7. Better Abstraction

Preface - xi

1: Object-Oriented Programming with Classes 1

Object-Oriented Programming 1

Classes 7

2: C++ Without Classes 9

Three to Start 9

Function Changes 1 3

Stricter Typing 1 8

Dynamic Initialization of Globals 2 1

Some Final Lists 2 2

3: Abstraction with Member Functions i 25

The Abstraction 25

Using a struct 26

Using a struct with Interface Functions 27

Using a Class with Member Functions 30

Page 4: C++: The Core Language - GBV · Implicit Default Constructors and Destructors 73 Beyond Chaining 73 Explicit Invocation 74 Flow of Control 76 Time of Invocation 77 7. Better Abstraction

4: Encapsulation with Access Specifiers 41

Access Specifiers 4 1

A Member Can Have Any Access Level 4 3

Unlimited Access Specifiers 4 5

Private Does Not Mean Invisible 4 5

Friend Classes 4 6

5.- Hierarchy with Composition and Derivation 53

Composition 5 3

Derivation 5 4

Chaining 6 3

6: Better Abstraction with Constructorsand Destructors 65

The Default Constructor 65

The Destructor 6 8

Built-in Types 69

Hierarchy and Chaining 6 9

Implicit Default Constructors and Destructors 7 3

Beyond Chaining 7 3Explicit Invocation 7 4

Flow of Control 76

Time of Invocation 77

7.. Better Abstraction with new and delete 81

Dynamic Objects 8 1

Dynamic Arrays 82

Mixing Allocators 84

reallocO 84

Out of Memory 85

8: References 89

Parameter Versus Argument 89

Passing by Value in C and C++ 9o

Passing by Reference in C++ 9 1

Reference Versus Pointer 9 2

Returning by Reference in C++ 94

Page 5: C++: The Core Language - GBV · Implicit Default Constructors and Destructors 73 Beyond Chaining 73 Explicit Invocation 74 Flow of Control 76 Time of Invocation 77 7. Better Abstraction

Overloading : Reference Versus Value 9 5

Binding Problems 9 6

9: Better Abstraction with Other Specia lMember Functions 99

Assignment Operator 10 0

Copy Constructor 10 6

Printing an Object 11 3

Summary 11 7

10: An Example Class 119

Using an int 12 0

Using a Static Array 12 2

Using a Dynamic Array 12 3

Using a Reference-Counted String 12 6

Copy Constructor Quiz 13 0

Summary Table 13 1

11: Better Hierarchy with Templates 133

Defining a Template 13 3

Using a Template to Define an Object : 136

Careful with That Expansion, Eugene 13 7

Modifying Container Class Elements 13 8

12: Polymorphism with Virtual Functions 14 1

Polymorphism in C 14 2

Virtual Functions 144

Polymorphism in C++ 14 6

More About Virtual Functions 14 8

13: More About Polymorphism 153

Abstract Classes 15 3

Derivation Decisions 15 6

Virtual Decisions 15 8

Calling Virtual Functions from Constructors and Destructors 16 3

Under the Hood 16 5

Pitfalls 166

Page 6: C++: The Core Language - GBV · Implicit Default Constructors and Destructors 73 Beyond Chaining 73 Explicit Invocation 74 Flow of Control 76 Time of Invocation 77 7. Better Abstraction

14: Implementing an Object-Oriented Design 169

Implementation Table 170 •

Examples 17 1

Is-A, Has-A 173

Defensive Implementation 174

15: An Example Program 175

The String Class 176

The Rule, CwRule, and HwRule Classes 177

The Scanner Class 180

Using the Program 18 1

Example Code 182

16: What to Study Next 191

const 192

inline 192

Constructors with Parameters 192

Member Initialization Lists 192

Efficient Copy Constructor 19 3

Special Member Functions 19 3

A. C++ Operators 195

B: One Problem with Returning by Value 19 7

Bibliography 199

Index 201