Professional C__ - Marc Gregoire [71]
For a number of reasons, it is difficult to sustain this level of stylistic consistency. In the case of const, sometimes programmers just aren’t educated about how to use it. You will eventually come across old code or a library function that isn’t const-savvy. A good programmer will use const_cast to temporarily suspend the const property of a variable, but an inexperienced programmer will start to unwind the const property back from the calling function, once again ending up with a program that never uses const.
Other times, standardization of style comes up against programmers’ own individual tastes and biases. Perhaps the culture of your team makes it impractical to enforce strict style guidelines. In such situations, you may have to decide which elements you really need to standardize (such as variable names and tabs) and which ones are safe to leave up to individuals (perhaps spacing and commenting style). You can even obtain or write scripts that will automatically correct style “bugs” or flag stylistic problems along with code errors.
SUMMARY
The C++ language provides a number of stylistic tools without any formal guidelines for how to use them. Ultimately, any style convention is measured by how widely it is adopted and how much it benefits the readability of the code. When coding as part of a team, you should raise issues of style early in the process as part of the discussion of what language and tools to use.
The most important point about style is to appreciate that it is an important aspect of programming. Teach yourself to check over the style of your code before you make it available to others. Recognize good style in the code you interact with and adopt the conventions that you and your organization find useful.
This chapter concludes the first part of this book which focuses on discussing design themes and coding styles on a high level. The next part delves into the implementation phase of the software engineering process with details of C++ coding.
PART II
C++ Coding the Professional Way
CHAPTER 6: Gaining Proficiency with Classes and Objects
CHAPTER 7: Mastering Classes and Objects
CHAPTER 8: Discovering Inheritance Techniques
CHAPTER 9: Understanding C++ Quirks and Oddities
CHAPTER 10: Handling Errors
CHAPTER 11: Delving into the Standard Library
CHAPTER 12: Understanding Containers and Iterators
CHAPTER 13: Mastering STL Algorithms
CHAPTER 14: Using Strings and Regular Expressions
CHAPTER 15: Demystifying C++ I/O
CHAPTER 16: Additional Library Utilities
CHAPTER 17: Customizing and Extending the STL
Chapter 6
Gaining Proficiency with Classes and Objects
WHAT’S IN THIS CHAPTER
How to write your own classes with methods and data members
How to control access to your methods and data members
How to use objects on the stack and on the heap
What the life cycle of an object is
How to write code that is executed when an object is created or destroyed
How to write code to copy or assign objects
As an object-oriented language, C++ provides facilities for using objects and for writing object definitions, called classes. You can certainly write programs in C++ without classes and objects, but by doing so, you do not take advantage of the most fundamental and useful aspect of the language; writing a C++ program without classes is like traveling to Paris and eating at McDonald’s. In order to use classes and objects effectively, you must understand their syntax and capabilities.
Chapter 1 reviewed the basic syntax of class definitions. Chapter 3 introduced the object-oriented approach to programming in C++ and presented specific design strategies for classes and objects. This chapter describes the fundamental concepts involved in using classes and objects, including writing class definitions, defining methods, using objects on the stack and the heap, writing constructors, default constructors, compiler-generated constructors, constructor initializers (known as ctor-initializers), copy constructors, initializer-list constructors, destructors, and assignment operators.