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

Tài liệu Object-Oriented Programming in C++, 3rd Edition docx

1,1K 661 2

Đ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 1.120
Dung lượng 9,56 MB

Nội dung

Object-Oriented Programming in C++, Third Edition (Publisher: Macmillan Computer Publishing) Author(s): Robert Lafore ISBN: 157169160x Publication Date: 11/25/98 Introduction About the Author Preface CHAPTER 1—THE BIG PICTURE Why Do We Need Object-Oriented Programming? Procedural Languages The Object-Oriented Approach Characteristics of Object-Oriented Languages Objects Classes Inheritance Reusability Creating New Data Types Polymorphism and Overloading C++ and C Laying the Groundwork Summary Questions CHAPTER 2—C++ PROGRAMMING BASICS Getting Started Basic Program Construction Functions Program Statements Whitespace Output Using cout String Constants Directives Preprocessor Directives Header Files The using Directive Comments Comment Syntax When to Use Comments Alternative Comment Syntax Integer Variables Defining Integer Variables Object-Oriented Programming in C++, Third Edition (Publisher: Macmillan Computer Publishing) Author(s): Robert Lafore ISBN: 157169160x Publication Date: 11/25/98 Introduction About the Author Preface CHAPTER 1—THE BIG PICTURE Why Do We Need Object-Oriented Programming? Procedural Languages The Object-Oriented Approach Characteristics of Object-Oriented Languages Objects Classes Inheritance Reusability Creating New Data Types Polymorphism and Overloading C++ and C Laying the Groundwork Summary Questions CHAPTER 2—C++ PROGRAMMING BASICS Getting Started Basic Program Construction Functions Program Statements Whitespace Output Using cout String Constants Directives Preprocessor Directives Header Files The using Directive Comments Comment Syntax When to Use Comments Alternative Comment Syntax Integer Variables Defining Integer Variables Declarations and Definitions Variable Names Assignment Statements Integer Constants Output Variations The endl Manipulator Other Integer Types Character Variables Character Constants Initialization Escape Sequences Input with cin Variables Defined at Point of Use Cascading << Expressions Precedence Floating Point Types Type float Type double and long double Floating-Point Constants The const Qualifier The #define Directive Type bool The setw Manipulator Cascading the Insertion Operator Multiple Definitions The IOMANIP Header File Variable Type Summary unsigned Data Types Type Conversion Automatic Conversions Casts Arithmetic Operators The Remainder Operator Arithmetic Assignment Operators Increment Operators Library Functions Header Files Library Files Header Files and Library Files Two Ways to Use #include Summary Questions Exercises CHAPTER 3—LOOPS AND DECISIONS Relational Operators Loops The for Loop Debugging Animation for Loop Variations The while Loop Precedence: Arithmetic and Relational Operators The do Loop When to Use Which Loop Decisions The if Statement The if else Statement The else if Construction The switch Statement The Conditional Operator Logical Operators Logical and Operator Logical OR Operator Logical NOT Operator Precedence Summary Other Control Statements The break Statement The continue Statement The goto Statement Summary Questions Exercises CHAPTER 4—STRUCTURES Structures A Simple Structure Declaring the Structure Defining a Structure Variable Accessing Structure Members Other Structure Features A Measurement Example Structures Within Structures A Card Game Example Structures and Classes Enumerations Days of the Week One Thing or Another Organizing the Cards Specifying Integer Values Not Perfect Other Examples Summary Questions Exercises CHAPTER 5—FUNCTIONS Simple Functions The Function Declaration Calling the Function The Function Definition Comparison with Library Functions Eliminating the Declaration Passing Arguments to Functions Passing Constants Passing Variables Passing by Value Structures as Arguments Names in the Declaration Returning Values from Functions The return Statement Returning Structure Variables Reference Arguments Passing Simple Data Types by Reference A More Complex Pass by Reference Passing Structures by Reference Notes on Passing by Reference Overloaded Functions Different Numbers of Arguments Different Kinds of Arguments Inline Functions Default Arguments Variables and Storage Classes Automatic Variables External Variables Static Variables Storage Returning by Reference Function Calls on the Left of the Equal Sign Don’t Worry Yet const Function Arguments Summary Questions Exercises CHAPTER 6—OBJECTS AND CLASSES A Simple Class Classes and Objects Declaring the Class Using the Class Calling Member Functions C++ Objects As Physical Objects Widget Parts as Objects Circles as Objects C++ Objects As Data Types Constructors A Counter Example A Graphics Example Destructors Objects as Function Arguments Overloaded Constructors Member Functions Defined Outside the Class Objects As Arguments The Default Copy Constructor Returning Objects from Functions Arguments and Objects A Card–Game Example Structures and Classes Classes, Objects, and Memory Static Class Data Uses of Static Class Data An Example of Static Class Data Separate Declaration and Definition const and Classes const Member Functions const Objects What Does It All Mean? Summary Questions Exercises CHAPTER 7—ARRAYS AND STRINGS Array Fundamentals Defining Arrays Array Elements Accessing Array Elements Averaging Array Elements Initializing Arrays Multidimensional Arrays Passing Arrays to Functions Function Declaration with Array Argument Arrays of Structures Arrays As Class Member Data Arrays of Objects Arrays of English Distances Arrays of Cards C-Strings C-string Variables Avoiding Buffer Overflow String Constants Reading Embedded Blanks Reading Multiple Lines Copying a String the Hard Way Copying a String the Easy Way Arrays of Strings Strings As Class Members A User-Defined String Type The Standard C++ string Class Defining and Assigning string Objects Input/Output with string Objects Finding string Objects Modifying string Objects Comparing string Objects Accessing Characters in string Objects Other string Functions Summary Questions Exercises CHAPTER 8—OPERATOR OVERLOADING Overloading Unary Operators The operator Keyword Operator Arguments Operator Return Values Nameless Temporary Objects Postfix Notation Overloading Binary Operators Arithmetic Operators Concatenating Strings Multiple Overloading Comparison Operators Arithmetic Assignment Operators The Subscript Operator [ ] Data Conversion Conversions Between Basic Types Conversions Between Objects and Basic Types Conversions Between Objects of Different Classes Conversions: When to Use What Pitfalls of Operator Overloading and Conversion Use Similar Meanings Use Similar Syntax Show Restraint Avoid Ambiguity Not All Operators Can Be Overloaded Keywords explicit and mutable Preventing Conversions with explicit Changing const Object Data Using mutable Summary Questions Exercises CHAPTER 9—INHERITANCE Derived Class and Base Class Specifying the Derived Class Accessing Base Class Members The protected Access Specifier Derived Class Constructors Overriding Member Functions Which Function Is Used? Scope Resolution with Overridden Functions Inheritance in the English Distance Class Operation of ENGLEN Constructors in DistSign Member Functions in DistSign Abetting Inheritance Class Hierarchies “Abstract” Base Class Constructors and Member Functions Inheritance and Graphics Shapes Public and Private Inheritance Access Combinations Access Specifiers: When to Use What Levels of Inheritance Multiple Inheritance Member Functions in Multiple Inheritance private Derivation in EMPMULT Constructors in Multiple Inheritance Ambiguity in Multiple Inheritance Containership: Classes Within Classes Inheritance and Program Development Summary Questions Exercises CHAPTER 10—POINTERS Addresses and Pointers The Address-of Operator & Pointer Variables Syntax Quibbles Accessing the Variable Pointed To Pointer to void Pointers and Arrays Pointer Constants and Pointer Variables Pointers and Functions Passing Simple Variables Passing Arrays Sorting Array Elements Pointers and C-type Strings Pointers to String Constants Strings As Function Arguments Copying a String Using Pointers Library String Functions The const Modifier and Pointers Arrays of Pointers to Strings Memory Management: new and delete The new Operator The delete Operator A String Class Using new Pointers to Objects Referring to Members Another Approach to new An Array of Pointers to Objects A Linked List Example A Chain of Pointers Adding an Item to the List Displaying the List Contents Self-Containing Classes Augmenting linklist Pointers to Pointers Sorting Pointers The person** Data Type Comparing Strings A Parsing Example Parsing Arithmetic Expressions The PARSE Program Simulation: A HORSE Race Debugging Pointers Summary Questions Exercises CHAPTER 11—VIRTUAL FUNCTIONS Finding An object’s class with TYPEID( )11 Virtual Functions Normal Member Functions Accessed with Pointers Virtual Member Functions Accessed with Pointers Late Binding Abstract Classes and Pure Virtual Functions Virtual Functions and the person Class Virtual Functions in a Graphics Example Virtual Destructors Virtual Base Classes Friend Functions Friends As Bridges Breaching the Walls English Distance Example friends for Functional Notation friend Classes Static Functions Accessing static Functions Numbering the Objects Investigating Destructors Assignment and Copy Initialization Overloading the Assignment Operator The Copy Constructor A Memory-Efficient String Class The this Pointer Accessing Member Data with this Using this for Returning Values Revised strimem Program Dynamic Type Information Checking the Type of a Class with dynamic_cast Changing Pointer Types with dynamic_cast The typeid Operator Summary Questions Exercises CHAPTER 12—STREAMS AND FILES Stream Classes Advantages of Streams The Stream Class Hierarchy The ios Class The istream Class The ostream Class The iostream and the _withassign Classes Stream Errors Error-Status Bits Inputting Numbers Too Many Characters No-Input Input Inputting Strings and Characters Error-Free Distances Disk File I/O with Streams Formatted File I/O Strings with Embedded Blanks Character I/O Binary I/O The reinterpret_cast Operator Closing Files Object I/O I/O with Multiple Objects File Pointers Specifying the Position Specifying the Offset The tellg() Function Error Handling in File I/O Reacting to Errors Analyzing Errors File I/O with Member Functions Objects That Read and Write Themselves Classes That Read and Write Themselves Overloading the Extraction and Insertion Operators Overloading for cout and cin Overloading for Files Memory As a Stream Object Command-Line Arguments Printer Output Summary Questions Exercises [...]... Lafore has been writing books about computer programming since 1982 His best-selling titles include Assembly Language Programming for the IBM PC, C Programming Using Turbo C++, C++ Interactive Course, and Data Structures and Algorithms in Java Mr Lafore holds degrees in mathematics and electrical engineering, and has been active in programming since the days of the PDP-5, when 4K of main memory was considered... Dividing a program into functions and modules is one of the cornerstones of structured programming, the somewhat loosely defined discipline that influenced programming organization for several decades before the advent of Object-Oriented Programming Problems with Structured Programming As programs grow ever larger and more complex, even the structured programming approach begins to show signs of strain... into functions, but how it will be divided into objects Thinking in terms of objects, rather than functions, has a surprisingly helpful effect on how easily programs can be designed This results from the close match between objects in the programming sense and objects in the real world This process is described in detail in Chapter 16, Object-Oriented Design.” What kinds of things become objects in. .. exists in a procedural language Classes In OOP we say that objects are members of classes What does this mean? Let’s look at an analogy Almost all computer languages have built -in data types For instance, a data type int, meaning integer, is predefined in C++ (as we’ll see in Chapter 3, “Loops and Decisions”) You can declare as many variables of type int as you need in your program: int int int int day;... in computer games Cars in an auto race Positions in a board game (chess, checkers) Animals in an ecological simulation Opponents and friends in adventure games The match between programming objects and real-world objects is the happy result of combining data and functions: The resulting objects offer a revolution in program design No such close match between programming constructs and the items being... we mention here will come into focus as we demonstrate them in detail in subsequent chapters Why Do We Need Object-Oriented Programming? Object-Oriented Programming was developed because limitations were discovered in earlier approaches to programming To appreciate what OOP does, we need to understand what these limitations are and how they arose from traditional programming languages Procedural Languages... cpp Files More Simplifications Interacting with the Program Prototyping Final Thoughts Summary Questions Projects Appendix A Appendix B Appendix C Appendix D Appendix E Appendix F Appendix G Appendix H Index Previous Table of Contents Next Introduction Object-Oriented Programming (OOP) is the most dramatic innovation in software development in the last decade It ranks in importance with the development... functions are said to be encapsulated into a single entity Data encapsulation and data hiding are key terms in the description of object-oriented languages If you want to modify the data in an object, you know exactly what functions interact with it: the member functions in the object No other functions can access the data This simplifies writing, debugging, and maintaining the program A C++ program typically... helps maintain the integrity of the information used by the company The accounting department, for instance, is responsible for the payroll data If you’re a sales manager, and you need to know the total of all the salaries paid in the southern region in July, you don’t just walk into the accounting department and start rummaging through file cabinets You send a memo to the appropriate person in the... write and maintain The Object-Oriented Approach The fundamental idea behind object-oriented languages is to combine into a single unit both data and the functions that operate on that data Such a unit is called an object An object’s functions, called member functions in C++, typically provide the only way to access its data If you want to read a data item in an object, you call a member function in the . string Objects Input/Output with string Objects Finding string Objects Modifying string Objects Comparing string Objects Accessing Characters in string Objects Other. Variables Passing Arrays Sorting Array Elements Pointers and C-type Strings Pointers to String Constants Strings As Function Arguments Copying a String Using Pointers Library

Ngày đăng: 21/02/2014, 06:20

TỪ KHÓA LIÊN QUAN

w