Programming with ANSI C++

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Designed to serve as a textbook for undergraduate engineering, BCA, and MCA students, Programing with ANSI C++ provides an in-depth coverage of elementary as well as advanced topics of the language. The book starts with a philosophical background and an overview of C++ followed by a systematic discussion of concepts such as classes, objects, functions, constructors, destructors, overloading, standard template library, and templates. An in-depth discussion of topics such as exception handling, in heritance, polymorphism, RTTI, file handling, and namespaces has been presented with suitable examples. To reinforce the understanding of concepts, diagrams and program listings have been included wherever appropriate. The theory is well supported with plenty of application-oriented programs, complete with their source codes and test cases, and exercises in each chapter.

Author(s): Bhushan Trivedi
Edition: 2
Publisher: Oxford University Press, USA
Year: 2014

Language: English
Pages: 648

Front Matter
Dedication
Preface to the First Edition
Prefaces
Preface to the Second Edition
Table of Contents
1. Object-Oriented Programming and C++
1.1 Need for C++
1.1.1 Limitations of C
1.1.1.1 Lack of Global View
1.1.1.2 Not Designed for Reusability
1.1.2 Object-Oriented Programming
1.1.3 Object-Based and Object-Oriented Design
1.1.3.1 Differences between Object-Based and Object-Oriented Programming
1.1.4 C++ - Not Completely Object-Oriented
1.2 Concepts of Object-Oriented Programming
1.3 Classes and Objects
1.3.1 Object-Oriented View of Classes and Objects
1.3.2 Abstract Data Type
1.4 Methods and Messages
1.4.1 Message Passing
1.5 Abstraction and Encapsulation
1.6 Inheritance
1.6.1 Advantages of Deploying Inheritance
1.7 Abstract Classes
1.8 Polymorphism
1.8.1 Ad-Hoc Polymorphism at Compile Time
1.8.2 Dynamic Polymorphism at Run-Time
1.8.3 Parametric Polymorphism
1.9 Object-Oriented Design and C++
1.10 Principle of Information Hiding
1.11 Differences between C and C++
1.11.1 Philosophical Differences
1.11.1.1 C++ is Not Just Extended C
1.11.1.2 Implementation of Information Hiding in C++
1.11.1.3 Availability of Off-the-Shelf Objects
1.11.1.4 Standard Template Library
1.11.1.5 Similarity of User-Defined Types with Built-in Objects
1.11.1.6 Operator Overloading
1.11.2 Syntactical Differences
1.11.2.1 Comments
1.11.2.2 Input/Output Operators
1.11.2.3 C++ Headers
1.11.2.4 Return Types
1.11.2.5 Compiling the C++ Program
1.11.2.6 Support for Both Object-Based and Object-Oriented Programming
1.11.3 Exception-Based Design
1.11.4 Templates as Generic Programming Elements
1.12 C++ Object Model
1.13 Variations of C++
1.14 Applications of C++
Recapitulation
Keywords
Exercises
2. Overview of the C++ Language
2.1 Identifiers and Constants Literals
2.2 Keywords
2.3 Data Types
2.3.1 Borrowed from C
2.3.2 Borrowed from C with Modifications
2.3.3 Newly Added Data Types
2.3.3.1 Abstract Data Types
2.4 Pointers
2.4.1 Void Pointer
2.4.2 Constant Pointer
2.4.3 Pointer to Constant
2.4.4 Use of Constant Pointers and Pointers to Constant
2.4.4.1 How the Program Works
2.4.4.2 Pointer to Constant
2.4.4.3 How the Program Works
2.5 Reference Variables
2.5.1 Using Standalone Reference Variables
2.5.2 Reference Variables as Dummy Parameters for Functions
2.5.2.1 How the Program Works
2.5.3 Reference Variables as Return Types
2.5.3.1 How the Program Works
2.5.4 Chaining Inputs Using Reference Variables
2.5.5 More on Reference Variables
2.6 Access Modifiers
2.6.1 const
2.6.2 volatile
2.7 Storage Class Specifiers
2.8 Initialization
2.8.1 Normal Initialization
2.8.2 Variable Initialization
2.9 Operators
2.9.1 Scope Resolution Operator
2.9.1.1 How the Program Works
2.9.2 new and delete Operators
2.9.2.1 How the Program Works
2.9.3 Placement new
2.9.4 new vs malloc
2.9.5 Significance of delete Operator
2.9.6 New Casting Operators, typeid, and throw
2.10 Conditional Structures and Looping Constructs
Recapitulation
Keywords
Exercises
3. Classes and Objects
3.1 Introduction to Class and Object
3.2 Classes and Their Attributes
3.3 Anatomy of Class
3.4 Access Specifiers
3.4.1 Expression Parser
3.4.1.1 How the Program Works
3.5 Storage Requirements
3.6 Difference between Structure and Class in C++
3.7 Difference between Unions and Classes
3.8 Defining Function Members Outside Class
3.9 Static Data Members
3.10 Arrays of Objects
3.11 Pointer to Objects and Pointer to Members of a Class
3.12 Nested Classes
3.13 Local Classes
3.14 Assigning Objects
3.15 Constant Objects
Recapitulation
Keywords
Exercises
4. Functions
4.1 Introduction
4.2 Similarities with C Functions
4.3 main in C++
4.4 Inline Functions
4.5 Default Arguments
4.5.1 Using Static Variable as Default Argument to a Function
4.5.2 Functions with Objects as Parameters
4.5.2.1 How the Program Works
4.5.3 NRV Optimization
4.6 Call by Reference
4.7 Returning a Reference
4.8 Prototyping and Overloading
4.8.1 Prototyping
4.8.2 Function Overloading
4.8.2.1 How the Program Works
4.8.2.2 Methods of Function Overloading
4.8.2.3 Function Overloading and Polymorphism
4.9 Program Readability and Default Arguments
4.10 Member and Non-Member Functions
4.10.1 Deciding to Make a Function a Member or a Non-Member
4.11 Friend Functions
4.11.1 Need for Friend Functions
4.11.1.1 Situations Where Friend Functions are Preferred
4.11.1.2 Member of Another Class as a Friend
4.11.1.3 Friend Class
4.11.1.4 Differentiating Implementation with the Interface
4.12 const and volatile Functions
4.12.1 const Functions
4.12.2 Mutable Data Members
4.12.3 volatile Functions
4.13 Static Functions
4.13.1 Restrictions on Static Function
4.14 Private and Public Functions
4.15 Functions That Return Objects
4.16 Function Pointers
4.17 Using Pointer to Member Function
4.18 Linkage Specification
Recapitulation
Keywords
Exercises
5. Constructors and Destructors
5.1 Similar-to-Built-in Behaviour Concept
5.2 Need for Object Initialization
5.3 Introduction to Constructors
5.4 Rules for Defining Constructors
5.5 Default Constructors
5.5.1 Compiler-Defined Default Constructor
5.5.2 User-Defined Default Constructor
5.5.2.1 Cases Where a Default Constructor is Not Needed
5.5.2.2 Cases Where a Default Constructor Must Be Present
5.5.2.3 How the Program Works
5.6 Constructors with One Parameter
5.7 Explicit Constructors
5.8 Parameterized Constructors
5.9 Multiple Constructors
5.9.1 Constructors with Default Arguments
5.9.1.1 Semantic Correctness
5.9.1.2 Normal Constructors and Default Argument Constructors
5.9.2 Dynamic Initialization and Assignment Operator
5.10 Constructor with Dynamic Allocation
5.11 Copy Constructors
5.11.1 Object Initialization and Object Assignment
5.11.1.1 Case 1
5.11.1.2 Case 2
5.11.1.3 Case 3
5.11.2 Providing Copy Constructors
5.11.2.1 Changes in This Program
5.12 Private Copy Constructors
5.12.1 Member Initialization List
5.12.1.1 Need for Initialization List
5.12.1.2 Readability
5.12.1.3 Efficiency
5.12.1.4 Initializing Constants, References, and Member Objects Using MIL
5.12.2 MIL as Replacement for Constructors
5.12.3 Order of Initialization
5.12.3.1 How the Program Works
5.13 Destructors
5.13.1 Use of Destructors
5.13.1.1 How the Program Works
5.13.1.2 Processing Objects at the End of Their Lifetime
5.13.1.3 Destructor Code
5.14 Usage of Constructors and Destructors for Constant Objects
5.15 Synthesis and Execution of Constructors and Destructors
5.16 Implementation of the Important Trio
Recapitulation
Keywords
Exercises
6. Operator Overloading and User-Defined Conversions
6.1 Introduction
6.2 Restrictions under Operator Overloading
6.2.1 Operators That Cannot Be Overloaded
6.2.2 Operators That Cannot Be Overloaded as Friends
6.3 Operator Overloading through Member Function
6.4 Situations Where Operator Overloading is Useful
6.5 Overloading Unary Operators
6.5.1 Postfix Versions of ++ and -- Operators
6.5.1.1 How the Program Works
6.6 Overloading Binary Operators
6.6.1 Overloading Shorthand Operators while Overloading Arithmetic Operators
6.7 Operator Overloading through Friend Functions
6.8 Using Friends to Overload << and Operators
6.8.1 Overloading Assignment Operator =
6.8.1.1 Need for Overloading Assignment Operator
6.8.1.2 Precautions while Overloading an Operator
6.8.2 Array Subscript Operator []
6.8.2.1 How the Program Works
6.8.3 Function Call Operator
6.9 Function Objects
6.10 Overloading new and delete
6.10.1 Overloading new and delete Using malloc and free
6.10.1.1 How the Program Works
6.10.2 Overloading new and delete Using :: new
6.10.2.1 How the Program Works
6.11 User-Defined Conversions
6.11.1 Implicit Conversions
6.11.2 Built-in Data Type to Object
6.11.3 Object to Built-in Data Type
6.11.3.1 How the Program Works
6.11.4 Wrapper Classes
6.11.4.1 How the Program Works
6.11.5 Conversion of Object Type Using Constructors
6.11.5.1 How the Program Works
6.11.6 Conversion of Object Type Using Conversion Functions
6.11.6.1 How the Program Works
6.11.7 Constructor vs Operator Functions for Conversion
6.11.8 Choosing an Appropriate Conversion Method
Recapitulation
Keywords
Exercises
7. Templates
7.1 Introduction
7.2 Function Templates
7.2.1 Drawbacks of Using Macros
7.2.2 Single-Argument Function Templates
7.2.2.1 How the Program Works
7.2.3 Instantiation
7.2.4 Generic Sorting and Need for Operator Overloading
7.2.5 Sorting Employee Objects Using Generic Bubble Sort
7.2.5.1 How the Program Works
7.2.6 Function Templates with Multiple Arguments
7.2.6.1 How the Program Works
7.2.7 Function Templates with Two Generic Arguments
7.2.8 Non-Generic Parameters in Template Functions
7.2.8.1 How the Program Works
7.2.8.2 Alternative Solution to Avoid Passing 'Size'
7.2.8.3 How the Program Works
7.2.9 Types of Non-Generic Arguments
7.2.10 Template Argument Deduction
7.2.10.1 Process of Deduction
7.2.11 Template Function and Specialization
7.2.12 Overloading a Template
7.2.12.1 How the Program Works
7.2.12.2 Need for Overloading Templates
7.2.12.3 How the Program Works
7.2.12.4 Template Specialization vs Non-Generic Function
7.2.13 Overloading One Generic Function with Another
7.2.14 Manually Overloaded Functions vs Template Instantiations
7.2.15 Default Arguments to Function Templates
7.2.15.1 Efficiency
7.2.15.2 Flexibility
7.3 Class Templates
7.3.1 Defining Functions of Class Templates Outside the Class
7.3.1.1 How the Program Works
7.3.2 Classes with Multiple Generic Data Types
7.3.3 Using Non-Type Arguments
7.3.4 Using Default Arguments
7.3.5 Static Data Members
7.3.6 Friends of Class Template
7.3.7 Primary and Partial Specialization
7.4 Compilation Models for Templates
7.4.1 Inline vs Non-Inline Function Calls in Multiple Files
7.4.2 Template Instantiations in Multiple Files
7.4.2.1 export Keyword
7.5 Use of typename
Recapitulation
Keywords
Exercises
8. Exception Handling
8.1 Introduction
8.2 Traditional Error Handling
8.2.1 Returning Error Number
8.2.2 Global Flag Manipulation
8.2.3 Abnormal Termination of Program
8.3 Need for Exception Handling
8.3.1 Dividing Error Handling
8.3.2 Unconditional Termination
8.3.3 Separating Error Reporting and Error Handling
8.3.4 Problem of Destroying Objects
8.4 Components of Exception Handling Mechanism
8.4.1 try Block
8.4.2 catch Block
8.4.3 throw Expression
8.4.3.1 How the Program Works
8.5 Challenges in the New Approach
8.5.1 Finding Proper Handlers
8.5.2 Finding Proper Handlers for Polymorphic Objects
8.5.3 Backtracking Until Beginning of try Block
8.6 Throwing within and Outside Functions
8.6.1 Handling Exceptions
8.7 Throwing Variables other than Objects
8.8 Using Multiple catch
8.9 Catch All
8.10 Restricting Exceptions from Functions: Exception Specification
8.11 Rethrowing Exceptions
8.12 terminate and unexpected Functions
8.13 uncaught_exception Function
8.14 Exception Handlers and Debuggers
8.15 Drawbacks of Exception Handling
8.16 Exception Class
Recapitulation
Keywords
Exercises
9. Inheritance
9.1 Introduction
9.2 Advantages of Using Inheritance
9.2.1 Avoiding Creation of Objects from Scratch
9.2.2 Avoiding Redundancy and Maintaining Consistency
9.2.3 Mapping a Real-World Hierarchy
9.3 Is-a and Part of Relationships
9.4 Defining Derived Classes
9.4.1 Derivation Using Public Access Modifier
9.4.1.1 How the Program Works
9.4.2 Derivation Using Private Access Modifier
9.4.2.1 How the Program Works
9.4.3 Protected Access Specifier
9.4.3.1 How the Program Works
9.4.4 Derivation Using Protected Access Modifier
9.5 Inheritance in C++ Object Model
9.6 Different Ways to Derive Classes
9.6.1 Public Derivation
9.6.2 Private Derivation
9.6.3 Protected Derivation
9.6.4 Public and Private Derivation of Protected Access Specifier
9.7 Access Control
9.8 Access Declaration
9.9 Deriving Multiple Classes from a Single Class
9.10 Multiple Inheritance
9.10.1 Problems in Multiple Inheritance
9.10.1.1 From the Compiler's Angle
9.10.1.2 From the Designer's Angle
9.11 Deriving a Class from an Already Derived Class
9.12 Virtual Base Class
9.13 Applications of Constructors and Destructors
9.14 Exception Handling in Case of Derivation
9.15 Composite Objects Container Objects
Recapitulation
Keywords
Exercises
10. Run-Time Polymorphism by Virtual Functions
10.1 Introduction
10.2 Compile Time and Run-Time Polymorphism
10.3 Pointer to Object
10.4 this Pointer
10.5 Compatibility of Derived and Base Class Pointers
10.5.1 Subobject Concept
10.6 Base Class and Derived Class Member Functions
10.7 Virtual Functions
10.7.1 Static vs Dynamic Binding
10.7.2 Default Arguments to Virtual Functions
10.7.2.1 How the Program Works
10.7.2.2 Dummy Default Arguments to the Rescue
10.7.2.3 How the Program Works
10.7.3 Advantages of Using Virtual Functions
10.7.4 Virtual Destructors
10.7.4.1 How the Program Works
10.8 Use of Virtual Functions
10.9 Pure Virtual Functions
10.9.1 Static Invocation of Virtual Function
10.9.1.1 How the Program Works
Recapitulation
Keywords
Exercises
11. Run-Time Type Information and Casting Operators
11.1 Introduction
11.1.1 Polymorphic Objects
11.1.2 Need for RTTI
11.1.2.1 How the Program Works
11.1.3 typeinfo Object and typeid Operator
11.1.4 Using typeid for Non-Polymorphic Objects
11.1.5 Using typeid for Polymorphic Objects
11.1.5.1 How the Program Works
11.1.6 Using typeid for Solution
11.1.6.1 How the Program Works
11.1.7 Applying typeid to Class Names and Objects
11.1.8 Cases Where RTTI is Useful
11.1.9 Problems with typeid
11.2 Dynamic Casting Using dynamic_cast
11.2.1 Using dynamic_cast
11.2.1.1 How the Program Works
11.2.2 Using dynamic_cast to Replace typeid
11.2.2.1 How the Program Works
11.2.3 Using dynamic_cast to Solve Problems with typeid
11.2.3.1 How the Program Works
11.3 Casting Using const_cast
11.4 Casting Using static_cast
11.5 Casting Using reinterpret_cast
11.6 RTTI and Templates
11.6.1 Using typeid
11.6.1.1 How the Program Works
11.6.2 Compatibility and Efficiency Issues
11.7 Cross Casting
11.8 Downcasting
Recapitulation
Keywords
Exercises
12. Streams and Formatted Input/Output
12.1 Introduction
12.2 I/O Streams of C vs C++
12.3 Old C++ I/O vs ANSI C++ I/O
12.4 Predefined and Wide Character Streams
12.5 C++ Stream Classes Hierarchy
12.6 Formatted and Unformatted I/O
12.6.1 put and get Functions for cout
12.6.2 getline, read, and write Functions
12.6.2.1 How the Program Works
12.7 Formatting I/O
12.7.1 Member Functions of ios
12.7.1.1 width
12.7.1.2 precision
12.7.1.3 fill
12.7.1.4 How the Program Works
12.8 Setting and Clearing Format Flags
12.9 Using setf Function with Two Arguments
12.10 Examining and Clearing Flags
12.11 Manipulators
12.11.1 Using Manipulators Instead of ios Functions
12.11.2 Differences between Manipulators and ios Functions
12.12 Using Manipulators
12.12.1 Setting and Testing Flags Using Manipulators
12.12.2 Manipulators for Toggle Effect
12.12.3 Shorthand Manipulators
12.12.3.1 How the Program Works
12.13 User-Defined Manipulators
12.13.1 Passing and Returning Streams as Reference
12.13.2 Using a Function for Formatting
Recapitulation
Keywords
Exercises
13. Using Files for Input/Output
13.1 Specialty of Input/Output
13.2 Process of Input/Output
13.3 File I/O Programming
13.4 Text and Binary Streams
13.5 Opening and Closing Files
13.6 Text Files
13.6.1 Defining Files
13.6.2 Opening Files
13.6.3 Reading from and Writing to Files
13.6.4 Closing Files
13.6.5 Using Text Files
13.6.5.1 How the Program Works
13.6.6 Using get and put
13.6.6.1 How the Program Works
13.6.7 Using getline
13.6.7.1 How the Program Works
13.7 Binary Files
13.7.1 Opening a Binary File
13.7.2 Reading from and Writing to Binary Files
13.7.3 Closing Binary Files
13.7.4 Using Binary Files
13.7.4.1 How the Program Works
13.8 End of File
13.9 Random Access Using seek
13.9.1 seekg and seekp
13.9.1.1 How the Program Works
13.9.2 tellg and tellp
13.9.2.1 How the Program Works
13.10 I/O Modes
13.11 Object Persistence and Serializing
13.12 I/O Errors
Recapitulation
Keywords
Exercises
14. Namespaces
14.1 Name Conflict Problem
14.1.1 Global Namespace
14.1.2 Logical Grouping
14.1.3 Fully Qualified Names
14.2 Ways of using Namespaces
14.2.1 using Syntax
14.2.1.1 using Declaration
14.2.1.2 using Directive
14.2.1.3 Code without using Directive or Declaration
14.3 Defining a Namespace
14.3.1 Defining Variables inside a Namespace
14.3.2 Defining Functions inside a Namespace
14.3.3 Defining Classes inside a Namespace
14.3.3.1 How the Program Works
14.3.4 Declaring inside and Defining Outside the Namespace
14.3.4.1 How the Program Works
14.3.5 Extending a Namespace
14.3.5.1 How the Program Works
14.3.6 Using Namespaces in .h Files
14.4 Unnamed Namespaces
14.5 Nested Namespaces
14.6 Namespace Aliases
14.7 std Namespace
14.8 Koenig Lookup
14.9 Overhead with Namespaces
Recapitulation
Keywords
Exercises
15. String Objects
15.1 Introduction
15.2 Operations on String Objects
15.2.1 Creating Strings
15.2.2 Substring Operations
15.2.2.1 How the Program Works
15.2.3 Operations Involving Multiple Strings
15.2.4 String Characteristics
15.2.4.1 How the Program Works
Recapitulation
Keywords
Exercises
16. Standard Template Library
16.1 Introduction
16.2 Generic Programming
16.3 Generic Software Components
16.4 Generic Algorithms
16.5 Iterators
16.6 Containers and Their Types
16.6.1 Vectors
16.6.1.1 How the Program Works
16.6.2 List
16.6.2.1 How the Program Works
16.6.2.2 List vs Vector
16.6.2.3 How the Program Works
16.6.3 Deque
16.6.3.1 How the Program Works
16.6.4 Sorted Associative Containers
16.6.5 Maps
16.6.5.1 How the Program Works
16.6.6 Multimap
16.6.6.1 How the Program Works
16.6.7 Sets
16.6.7.1 How the Program Works
16.6.8 Multiset
16.6.8.1 How the Program Works
16.6.9 Adapted Containers
16.7 Generic Algorithms
16.7.1 find Algorithm
16.7.1.1 How the Program Works
16.7.2 copy Algorithm
16.7.2.1 How the Program Works
16.7.3 sort Algorithm
16.7.3.1 How the Program Works
Recapitulation
Keywords
Exercises
Features of the Book
Appendix: Case Study
A.1 Introduction
A.2 Marks Entry Problem
A.3 Program Construct
A.4 Explanation
A.5 Improving the Program
Index
A
B
C
D
E
F
G
I
K
L
M
N
O
P
R
S
T
U
V
W