Lecture 3: Classes



Lecture 3: Classes


Now we begin looking at object-oriented aspects of C++.
3.1 date example
In the C++ Programming Examples, there are two parallel versions of an application which uses date computation. There is an object-oriented C version and a C++ version. Both compile to about the same object. At this point, all C++ supplies is a superior organizational notation and tools. At the machine level, the code generated is about the same in either case.
  • C version written using best practice to avoid confusion and name clashes with other libraries.
  • Consider date routines as a separate library. Show how declarations and definitions work in C++ compared with C. What goes in header files? What goes in implementation files?
  • C++ member functions get unqualified access to object members (data, types and functions) Don't need to qualify with object name as C routines do. this keyword to get at object itself. Can also be used (needlessly) to qualify access to object data members. Unqualified member names can hide global functions/objects. Use :: prefix if you need to get at these.
  • using private: keyword to protect data members. client code versus implementation code. implementation code for one object might be client code for another object it uses. Protecting data members allows implementation to change without code in client objects having to change. (They will most likely need to be recompiled.)
  • using class instead of struct
  • declaring methods const. const key word appears directly after parens. Must appear in decl and def. Part of "signature".
  • static keyword
Associating functions with objects. They get declared as members of the objects they are to manipulate. These members (unless they are declared virtual) don't take up space in the object. Declaration and definition. Defining function in class declaration makes it inline. Don't do this just because it's convenient - remember about code duplication.
Constructors - a special member function for initialization. A behind-the-scenes function call. No return value. No address.
  • date constructors. No-arg constructor: In defining vars, parens must be left off, otherwise compiler thinks its a function declaration; in creating temporaries, parens must be included, e.g. cout << date() prints today's date. When using new, either notation can be used, e.g. new date or new date().
  • () vs = variation for single arg constructor
  • effect of making constructor private
  • specifying construction of contained or inherited objects.
In speaking of pieces of C++ code and their relationship to the various classes, it is useful to differentiate between "implementation" code and "client" code. Implementation code for a particular class is the code used to define its methods. Client code is other code which manipulates instances of the class. This terminology is always used with respect to a particular class. A piece of implementation code for one class may be client code for another class.
3.2 Default, no-arg and copy constructors
Only defined constructors and copy constructor can be used. If no constructors are defined, then only default and copy constructor are available. Default copy constructor can create problems in objects that use new for initialization. Declare an explicit private one if the default behavior is unacceptable, or define an explicit public one. Must take reference for argument - pass by value results in circularity.
3.3 ChrStack example
  • Destructors - a special function member for cleaning up object when it goes out of scope. Another behind-the-scenes function call.
  • syntaxes of creating stack objects (= vs () for single-argument constructors)
  • C++ programmers have to be conscious of constructors, destructors, temporary variable creation because these can consume a lot of execution time and their usage is always implicit. Except for floating-point/integer conversions, C is fairly transparent. functions calls hide code in C. Now implicit/explicit object creation/destruction can be expensive.
  • Problems introduced by default copy construction. Fixes (see above).
3.4 Order of construction and destruction
Before this is discussed, the following concept needs to be defined:
thread of control 
the sequence of program statements executed when the program is run
So, what determines the order of construction and destruction of objects in various circumstances? The following is an exhaustive list of the various circumstances possible in a C++ program:
globals - Within a particular file, the order of definition determines construction sequence. No spec yet for order vis-a-vis globals in other files. Destruction in reverse order.
static globals - Probably thread of control rule. Destruction in reverse order.
static locals - Thread of control rule for construction. Destruction in reverse order.
locals - Construction when thread of control passes through object's definition. Destruction sometime after last use and sometime before object goes out of scope in reverse order of construction.
free store - new and delete operations.
base components - Construction is in the order the base components are declared, before member objects (if any) are constructed and before the class constructor is executed. Destruction is in reverse order, after the class destructor is executed and after member objects (if any) are destroyed.
member objects - Construction is in the order the member objects are declared, after base components (if any) are constructed and before the class constructor is executed. Destruction is in reverse order, after the class destructor is executed and before base components (if any) are destroyed.
temporaries - Construction when thread of control reaches it. Destruction after expression exited and before end of scope. Exactly where is a little indefinite.


0 comments: