This handout describes some of the differences between the general approach we have taken to C++ in this course and the standard practice that has evolved in the industry. This handout does not cover the conventional practice in any detail, but will alert you to what you need to look for as you continue to write C++ programs in other contexts.
We've trained you to includegenlib.h in every program, and if you've ever forgotten this, you were rewarded with a passel of compile errors. The contents ofgenlib.h is surprisingly small, but there is one key line stashed in there you need to know:
C++ allows you to definenamespaces, which allow you to group a set of symbols under a parent name in order to avoid name clashes. All C++ standard library features are grouped into a namespace calledstd. For example,cout andisupper andstring actually have the fully qualified namesstd::cout,std::isupper, andstd::string. Some C++ programmers make the habit of using the long names everywhere. Others find that tedious and instead introduce a
allows you to use the names from that namespace without qualification. Thegenlib.h header also includes<string> to make the declaration of the string class visible and declares the prototype for ourError function. That's it!
pseudo-random number generation functions. The standard functions are somewhat less convenient, i.e. your code must scale to desired range and convert to desired type (int/double/bool).
on astringstream object from<sstream>. Stringstreams are a stream variant that read/write to a string object instead of a file. Case conversion can be done in a loop using character functions from<cctype>.
We showed the use ofconst to declare constants, but it has a much larger applicability in the language. Theconst qualifier can be applied to any variable declaration, including parameters, return types, local variables and object data members, and it even can be used to tag member function declarations. Theconst qualifier is particularly useful in conjunction with reference parameters, because it allows you to clearly document when you are using pass-by-reference for efficiency only and not for mutability. For example, consider this function prototype:
TheBinky function takes two string references. When invokingBinky, no new string objects are created/copied, instead references are taken out to the two arguments passed by the caller. The first is a const reference, which means that the value ofs will not change within the call. The second is a non-const reference, so it is possible thatt's value will change. Both have the efficient performance of the reference parameter, but usingconst allows the prototype to more clearly document that the first parameter is passed by reference only for efficiency, and it would be assumed that the second non-const parameter is a reference because it is modified.
modify the receiver string) will not be allowed ons, but calls likelength orsubstr are ok. You might ask: how does the compiler know which member functions change the receiver and which don't? The member functions of a class can be declaredconst, which indicates that the member function does not change any state of the receiving object. When operating on a const object, you are only allowed to invoke those member functions that are declared const. For example, thesize andgetAt member functions of our Vector class should be declaredconst, whereassetAt should not, as shown in this excerpt below:
int size() const;
ElemType getAt(int index) const;
void setAt(int index, ElemType e);
Consistent use ofconst is a wonderful way to document what is being changed where. It is particularly helpful when passing pointers/references that would otherwise suggest mutability. However, it's practically impossible to useconst only a little bit. Once you start using it, it's rather contagious, and you find that you must follow-through everywhere and all functions/classes need to be designed for complete const-correctness. This is actually a great thing in production code, and a habit you will want to adopt in the future, but I feel the
C++ provides the ability to add new meanings for the built-in operators (such as+,=, and<) which is known as operator overloading. This makes it possible for user-defined types to have the same conveniences as the standard built-in types. Operator overloading is just a form of "syntactic sugar". It doesn't change the functionality available, but allows you to access functionality using a more convenient/familiar/aesthetic syntax. You could write anAdd function that takes two Fraction objects and returns their sum, or you could instead overload the + operator. The usage sum = Add(f1, f2) would be equivalent to sum = f1 + f2. An operator is overloaded by implementing a function namedoperatorXXX whereXXX is replaced by the operator (+,*,==, etc.) For a class member function, the left-hand side is the receiver object and any other operands are passed as arguments, like this:
You can overload almost any operator in C++ (including++ ,^,(),*, and so on). It can be abused to create cryptic and confusing code, so as a rule of thumb, operators should only be overloaded when the new use is obvious and consistent with existing meanings. For most classes, there are few reasons to overload operators (there is no obvious interpretation of< applied to two queues, or* on two maps) save a few operators that are quite commonly overloaded; most notablyoperator=, the assignment operator overloaded to add deep-copying behavior, and
structures and algorithms. There is basically a one-to-one correspondence between our CS106 classes and the STL equivalent. The STL version typically has a lowercase name and provides similar functionality but with slightly different syntax. All STL containers are const-correct and have appropriate deep-copying behavior. In general, the STL prioritizes efficiency over convenience and safety, so some operations are designed in a slightly awkward or unsafe way in the name of performance.
Now bringing you back...
Please enter your email address below to reset your password. We will send you an email with instructions on how to continue.
Does that email address look wrong? Try again with a different email.