Master C++ the right way: From the software engineering perspective!
Build more powerful, robust, maintainable C++ software!
For developers with experience in any language, Victor Shtern's Core C++ teaches C++ the right way: by applying the best software engineering practices and methodologies to programming in C++. Even if you've already worked with C++, this comprehensive book will show you how to build code that is more robust, far easier to maintain and modify, and far more valuable.
Shtern's book teaches object-oriented principles before teaching the language, helping you derive all the power of object-oriented development to build superior software. Learn how to make design decisions based on key criteria such as information hiding and pushing responsibilities from clients down to server classes. Then, master every key feature of ANSI/ISO C++ from a software engineer's perspective: classes, methods, const modifiers, dynamic memory management, class composition, inheritance, polymorphism, I/O, and much more.
If you want to build outstanding C++ software, coding skill isn't enough. Objects aren't enough. You must design, think, and program using today's best software engineering practices and with Core C++, you will.
So, Core C++ delivers:
Every core series book:
Click here for a sample chapter for this book: 0130857297.pdf
(Note: All chapters conclude with a Summary.)
I. INTRODUCTION TO PROGRAMMING WITH C++.1. Object-Oriented Approach: What's So Good about It?
The Origins of the Software Crisis. Remedy 1: Eliminating Programmers. Remedy 2: Improved Management Techniques. The Waterfall Method. Rapid Prototyping. Remedy 3: Designing a Complex and Verbose Language. The Object-Oriented Approach: Are We Getting Something for Nothing? What Does the Designer Do? Design Quality: Cohesion. Design Quality: Coupling. Design Quality: Binding Together Data and Functions. Design Quality: Information Hiding and Encapsulation. Design Issue: Name Conflicts. Design Issue: Object Initialization. What Is an Object, Anyway? Advantages of Using Objects. Characteristics of the C++ Programming Language. C Objectives: Performance, Readability, Beauty, and Portability. C++ Objectives: Classes with Backward Compatibility with C.2. Getting Started Quickly: A Brief Overview of C++.
The Basic Program Structure. Preprocessor Directives. Comments. Declarations and Definitions. Statements and Expressions. Functions and Function Calls. Classes. Dealing with Program Development Tools.3. Working with C++ Data and Expressions.
Values and Their Types. Integral Types. Integer Type Qualifiers. Characters. Boolean Values. Floating Point Types. Working with C++ Expressions. High-Priority Operators. Arithmetic Operators. Shift Operators. Bitwise Logical Operators. Relational and Equality Operators. Logical Operators. Assignment Operators. Conditional Operator. Comma Operator. Mixed Expressions: Hidden Dangers.4. C++ CONTROL Flow.
Statements and Expressions. Conditional Statements. Standard Forms of Conditional Statements. Common Errors in Conditional Statements. Nested Conditionals and Their Optimization. Iteration. The Use of the WHILE Loop. Iterations with the DO-WHILE Loop. Iterations with the FOR Loop. C++ Jump Statements. The BREAK Statement. The CONTINUE Statement. The GOTO Statement. The RETURN and EXIT Jumps. The SWITCH Statement.5. Aggregation with Programmer-Defined Data Types.
Arrays as Homogeneous Aggregates. Arrays as Vectors of Values. Defining C++ Arrays. Operations over Arrays. Index Validity Checking. Multidimensional Arrays. Defining Character Arrays. Operations on Character Arrays. String Functions and Memory Corruption. Two-Dimensional Character Arrays. Array Overflow in Insertion Algorithms. Defining Array Types. Structures as Heterogeneous Aggregates. Defining Structures as Programmer-Defined Types. Creating and Initializing Structure Variables. Hierarchical Structures and Their Components. Operations on Structure Variables. Defining Structures in Multifile Programs. Unions, Enumerations, and Bit Fields. Unions. Enumerations. Bit Fields.6. Memory Management: The Stack and the Heap.
Name Scope as a Tool for Cooperation. C++ Lexical Scopes. Name Conflicts Within the Same Scope. Using Same Names in Independent Scopes. Using Same Name in Nested Scopes. Scope of Loop Variables. Memory Management: Storage Classes. Automatic Variables. External Variables. Static Variables. Memory Management: Using Heap. C++ Pointers as Typed Variables. Allocating Memory on the Heap. Arrays and Pointers. Dynamic Arrays. Dynamic Structures. Input and Output with Disk Files. Output to File. Input from File. Input/Output File Objects.
II. OBJECT-ORIENTED PROGRAMMING WITH C++.7. Programming with C++ Functions.
C++ Functions as Modularization Tools. Function Declarations. Function Definitions. Function Calls. Argument Promotions and Conversions. Parameter Passing in C++. Calling by Value. Calling by Pointer. Parameter Passing in C++: Calling by Reference. Structures. Arrays. More on Type Conversions. Returning a Value from a Function. Inline Functions. Parameters With Default Values. Function Name Overloading.8. Object-Oriented Programming with Functions.
Cohesion. Coupling. Implicit Coupling. Explicit Coupling. How to Reduce the Intensity of Coupling. Data Encapsulation. Information Hiding. A Larger Example of Encapsulation. Shortcomings of Encapsulation with Functions.9. C++ Class as a Unit of Modularization.
Basic Class Syntax. Binding Together Data and Operations. Elimination of Name Conflicts. Implementing Member Functions Outside of Class. Defining Class Objects of Different Storage Classes. Controlling Access to Class Members. Initialization of Object Instances. Constructors as Member Functions. Default Constructors. Copy Constructors. Conversion Constructors. Destructors. Timing of Constructor and Destructor Invocations. Class Scope and the Overriding of Names in Nested Scopes. Memory Management with Operators and Function Calls. Using Returned Objects in Client Code. Returning Pointers and References. Returning Objects. More on the const Keyword. Static Class Members. Using Global Variables as Class Characteristics. The Fourth Meaning of the static Keyword. Initialization of Static Data Members. Static Member Functions.10. Operator Functions: Another Good Idea.
Overloading of Operators. Limitations on Operator Overloading. What Operators Cannot Be Overloaded. Limitations on Return Types. Limitations on the Number of Parameters. Limitations on Operator Precedence. Overloaded Operators as Class Members. Replacing a Global Function with a Class Member. Using Class Members for Chain Operations. Using the const Keyword. Case Study: Rational Numbers. Mixed Types as Parameters. Friend Functions.11. Constructors and Destructors: Potential Trouble.
More on Passing Objects by Value. Operator Overloading for Nonnumeric Classes. The String Class. Dynamic Management of Heap Memory. Protecting Object Heap Data from Client Code. Overloaded Concatenation Operator. Preventing Memory Leaks. Protecting Program Integrity. How to Get There from Here. More on the Copy Constructor. Remedies for the Integrity Problem. Copy Semantics and Value Semantics. Programmer-Defined Copy Constructor. Return by Value. Limits for Copy Constructor Effectiveness. Overloading the Assignment Operator. Problems with System-Supplied Assignment Operator. Overloaded Assignment: The First Version (Memory Leak). Overloaded Assignment: The Next Version (Self-Assignment). Overloaded Assignment: Another Version (Chain Expression). Performance Considerations. First Remedy: More Overloading. Second Remedy: Return by Reference. Practical Considerations: What to Implement.
III. OBJECT-ORIENTED PROGRAMMING WITH AGGREGATION AND INHERITANCE.12. Composite Classes: Pitfalls and Advantages.
Using Class Objects as Data Members. C++ Syntax for Class Composition. Access to Data Members of Class Data Members. Access to Data Members of Method Parameters. Initialization of Composite Objects. Using the Components' Default Constructors. Using the Member Initialization List. Data Members with Special Properties. Constant Data Members. Reference Data Members. Using Objects as Data Members of Their Own Class. Using a Static Data Member as a Member of Its Own Class. Container Classes. Nested Classes. Friend Classes.13. Similar Classes: How to Treat Them.
Treating Similar Classes. Merging Subclass Features into One Class. Pushing Responsibility for Program Integrity to the Server. Separate Classes for Each Kind of Server Object. Using C++ Inheritance to Link Related Classes. Syntax of C++ Inheritance. Different Modes of Derivation from the Base Class. Defining and Using Objects of Base and Derived Classes. Accessing Base and Derived Class Services. Accessing Base Components of a Derived Class Object. Public Inheritance. Protected Inheritance. Private Inheritance. Adjusting Access to Base Members in the Derived Class. Default Inheritance Mode. Scope Rules and Name Resolution Under Inheritance. Name Overloading and Name Hiding. Calling a Base Method Hidden by the Derived Class. Using Inheritance for Program Evolution. Constructors and Destructors for Derived Classes. Using Initialization Lists in Derived Class Constructors. Destructors Under Inheritance.14. Choosing between Inheritance and Composition.
Choosing a Technique for Code Reuse. Example of a Client-Server Relationship between Classes. Reuse Through Human Intelligence: Just Do It Again. Reuse through Buying Services. Code Reuse Through Inheritance. Inheritance with Redefined Functions. Pluses and Minuses of Inheritance and Composition. Unified Modeling Language. The Goals of Using UML. Basic UML: Notation for Classes. Basic UML: Notation for Relationships. Basic UML: Notation for Aggregation and Generalization. Basic UML: Notation for Multiplicity. Case Study: A Rental Store. Classes and Their Associations. On Class Visibility and Division of Responsibilities. Class Visibility and Class Relationships. Pushing Responsibilities to Server Classes. Using Inheritance.
IV. ADVANCED USES OF C++.15. Virtual Functions and Other Advanced Uses of Inheritance.
Conversions between Nonrelated Classes. Strong Typing and Weak Typing. Conversion Constructors. Casts between Pointers (or References). Conversion Operators. Conversions between Classes Related Through Inheritance. Safe and Unsafe Conversions. Conversions of Pointers and References to Objects. Conversions of Pointer and Reference Arguments. Virtual Functions: Yet Another New Idea. Dynamic Binding: Traditional Approach. Dynamic Binding: Object-Oriented Approach. Dynamic Binding: Virtual Functions. Dynamic and Static Binding. Pure Virtual Functions. Virtual Functions: Destructors. Multiple Inheritance: Several Base Classes. Multiple Inheritance: Access Rules. Conversions between Classes. Multiple Inheritance: Constructors and Destructors. Multiple Inheritance: Ambiguities. Multiple Inheritance: Directed Graph. Multiple Inheritance: Is It Useful?16. Advanced Uses of Operator Overloading.
Operator Overloading: A Brief Overview. Unary Operators. Increment and Decrement Operators. Postfix Overloaded Operators. Conversion Operators. Subscript and Function Call Operators. The Subscript Operator. Function Call Operator. Input/Output Operators. Overloading Operator >>. Overloading Operator <<.17. Templates: Yet Another Design Tool.
A Simple Example of a Class Design Reuse. Syntax of Template Class Definition. Template Class Specification. Template Instantiation. Implementing Template Functions. Nested Templates. Template Classes with Several Parameters. Several Type Parameters. Templates with Constant Expression Parameters. Relations between Instantiations of Template Classes. Template Classes as Friends. Nested Template Classes. Templates with Static Members. Template Specializations. Template Functions.18. Programming with Exceptions.
A Simple Example of Exception Processing. Syntax of C++ Exceptions. Throwing an Exception. Catching an Exception. Claiming an Exception. Rethrowing an Exception. Exceptions with Class Objects. Syntax of Throwing, Claiming, and Catching Objects. Using Inheritance with Exceptions. Standard Library Exceptions. Type Cast Operators. The static_cast Operator. The reinterpret_cast Operator. The const_cast Operator. The dynamic_cast Operator. The typeid Operator.19. What We Have Learned.
C++ as a Traditional Programming Language. C++ Built-in Data Types. C++ Expressions. C++ Control Flow. C++ as a Modular Language. C++ Aggregate Types: Arrays. C++ Aggregate Types: Structures, Unions, Enumerations. C++ Functions as Modularization Tools. C++ Functions: Parameter Passing. Scope and Storage Class in C++. C++ as an Object-Oriented Language. C++ Classes. Constructors, Destructors, and Overloaded Operators. Class Composition and Inheritance. Virtual Functions and Abstract Classes. Templates. Exceptions. C++ and Competition. C++ and Older Languages. C++ and Virtual Basic. C++ and C. C++ and Java.Index.
Congratulations! You have opened one of the most useful C++ books on the market! It will teach you the strengths and weaknesses of C++, and it will do this better than any other book I have seen. And I have seen a lot of C++ books.
Of course, any author can claim that his or her book is one of the best on the market. What sets this book apart is its software engineering and maintenance perspective on writing C++ code. Very few C++ books (if any) do that.
Why is the software engineering and maintenance approach important? The point is that C++ changed not only the way we write computer programs, it also changed the way we learn programming languages. In the "good old days," you would spend a day or two looking at the basic syntax of the language, then you would try your hand at simple programming problems. Then you would learn more-complex syntax and would tackle more-complex problems. In a week or two (or in three or four weeks for a really complex language), you would have seen it "all" and could pose as an "expert."
It's different with C++; a very large and very complex language. Granted, it is a superset of C, and you can learn to write simple C programs (and, hence, C++ programs) very quickly. But things are different for complex programs. If the programmer does not know C++ well, a complex C++ program will not be portable; its code will be difficult to reuse, and it will be difficult to maintain.
C++ is a great languageit was created as a general-purpose engineering language, and its design is a clear success. Today, C++ is a language of choice for business, engineering, and even real-time applications. Significant effort was spent on the design of the language, to ensure that C++ programs provide great performance, that they support dynamic memory management, and that different parts of programs could be made relatively independent. Yet in all three areas, things can potentially go wrong even with a syntactically correct and thoroughly tested C++ program:
How important is this? If you are writing a small program that will be used only for a short time, then execution speed, memory management, maintainability, and reusability may not be of great importance. All that counts is your ability to quickly produce a solution. If this solution is not satisfactory, you can cut your losses by throwing the program away and writing another one. For this, any C++ book would do (but hey, you can still buy this one and enjoy its informal style and original insights into the language and its usage).
However, if you are working in a group, creating large applications that cannot be easily discarded and will be maintained for a long time, everything matters. The software engineering and maintenance approach I am advancing in this book is very useful and quite unique. Most books on the market do not mention these issues at all. (Just check their indexes and see for yourself.) When they do, they fail to spell out the techniques that can remedy a tough situation.
Another important characteristic of this book is its approach to the presentation of the material. There are many books on the market that do a good job enumerating the features of the language but do a mediocre job teaching you how to use the language. This is similar to learning a natural language. If you read a French grammar book, will it enable you to speak French? I did not study French, but I did study English, and I knowreading grammar books does not help to develop language fluency. In this book, I will show you how to use and how not to use the language, especially from the point of view of reusability and future maintenance.
Another teaching issue is that C++ features are so intertwined that it is hard to present C++ in a linear fashion, from simple to more complex. Many C++ authors do not even try. They say that these efforts "offend the intelligence of the reader." As a result, they might mention in Chapter 3 a concept that is explained only in Chapter 8, leaving the reader intimidated and frustrated.
My approach to teaching C++ is different. I introduce topics cyclically, first as a general overview and then again at a greater depth, with bells and whistles, and nowhere will your understanding depend on material in later chapters.
I developed my approach through years of teaching working software professionals. At Boston University Metropolitan College, most of my students hold professional jobs and come to classes in the evening in search of professional growth. I also taught numerous professional development seminars and on-site training courses. I developed great empathy for the students and their struggle with language concepts and programming techniques, and I translated this experience into a well-thought-out sequence of topics, examples, counterexamples, and recommendations. I think that my approach to teaching C++ is fairly unique, and you will benefit from it.
This book is written for professionals who are looking for a no-nonsense presentation of practical details combined with a deep understanding of C++ subtleties.
This book is written for you if you are looking for practical details of new technologies with a thorough discussion of their use.
It is written for you if you have experience in other languages and are moving to C++. If you are an experienced C++ programmer, you will find this book useful and sometimes an eye-opener. If this is your first programming book (and this is perfectly all right if it is), you will be well rewarded for the effort spent on reading it.
I decided not to follow other authors who give you a detailed tour of their books, explaining what is covered and where. Unfamiliar terms, concepts and techniques will not make much sense to you now and will probably be quite boring. This is why I put the summary of the book into its final chapter, Chapter 19, "What We Have Learned," and you can read it if you are interested. It makes more sense there.
Instead, let me tell you what parts of the book might be of interest to you, depending on your background and experience.
It is important to practice when you learn a language. Studying C++ without practicing it is as effective as taking a driver education course without driving: You'll learn a lot of useful things about driving, but you will not be able to drive. I strongly recommend that you experiment with the programs discussed in this book. The source code for all the listings can be found at the following site:
This book was thoroughly reviewed, carefully edited, and meticulously proofread. Still, some errors might remain.
In my drive to produce a unique book, I might have made statements that are groundless, unjustified, or plain erroneous. Or, they could be controversial and you might want to debate them.
Please do not hesitate to contact me at the following e-mail address:
For each typo or error that is pointed out to me or for each valid point regarding a discussion in the book, I promise to mention the names of the first two people who do so in the next edition of this book.