Title: Classes: Part II
1Chapter 7
21. Introduction
- In this chapter we continue our study of classes
and data abstraction - We discuss many more advanced topics and lay the
groundwork for the discussion of classes and
operator overloading in Chapter 8. - We us the C-style strings to help the reader
master the complex topic of C pointers.
32. const (Constant) Objects and const Member
Functions
- const Objects
- Some objects need to be modifiable and some do
not. The programmer may use the keyword const to
specify that an object is not modifiable, and
that any attempt to modify the object is a syntax
error. const Time noon(12, 0, 0)
42. const (Constant) Objects and const Member
Functions (cont.)
- C compilers disallow any member functions calls
for const objects unless the member functions
themselves are also declared const. - This is true even for get member functions that
do not modify the object. - Member functions declared const cannot modify the
object -- the compiler disallows this.
52. const (Constant) Objects and const Member
Functions (cont.)
- const Member Functions
- A function is specified as const both in its
prototype and in its definition. - Example
- prototype int getValue() const
- definition int AgetValue() const
return privateDataMember
62. const (Constant) Objects and const Member
Functions (cont.)
- An interesting problem arises here for
constructors and destructors, each of which often
need to modify objects. - The const declaration is not required for
constructors and destructors of const objects. - A constructor must be allowed to modify an object
so that the object can be initialized properly. - A destructor must be able to perform its
termination housekeeping chores before an object
is destroyed.
72. const (Constant) Objects and const Member
Functions (cont.)
- Member Initializer Lists
- This example classclass (int c, int i )
privData(i) count c is
equivalent to classclass (int c,
int i ) privData i count c
82. const (Constant) Objects and const Member
Functions (cont.)
- Member Initializer Lists (cont.)
- All data members can be initialized using member
initializer lists. - but consts and references must be initialized in
this manner. - not providing a member initializer list for these
is usually a syntax error.
93. Composition Objects as Members of Classes
- An AlarmClock class object needs to know when it
is supposed to sound its alarm, so why not
include a Time object as a member of the
AlarmClock object? - Such a capability is called composition. A class
can have objects of other classes as members.
103. Composition Objects as Members of Classes
(cont.)
- Member objects are constructed in the order in
which they are declared (not the order in the
initializer list) and before their host object is
constructed. - A member object does not need to be initialized
explicitly through a member initializer. If it is
not provided, the member objects default
constructor will be called implicitly.
114. friend Functions and friend Classes
- A friend function of a class is defined outside
that classs scope, yet has the right to access
the private members of the class. - A function or an entire class may be declared to
be a friend of another class.
124. friend Functions and friend Classes (cont.)
- To declare a function as a friend of a class,
precede the function prototype in the class
definition with the keyword friend. - To declare class ClassTwo as a friend of class
ClassOne place a declaration of the form
friend class ClassTwo in the definition of
class ClassOne.
134. friend Functions and friend Classes (cont.)
- Friendship is granted, not taken.
- for class B to be a friend of class A class A
must explicitly declare class B is it s friend. - Also, friendship is neither symmetric or
transitive - if A is a friend of B and B is a friend of C
- That does not imply that B is a friend of A
- That also does not imply that A is a friend of C.
145. Using the this Pointer
- this is a pointer to the object you are in.
- this is the object itself.
- One use of the this pointer is in enabling
cascading member function calls, and in cascading
overloaded input and output. a b c
cout ltlt a ltlt b ltlt c
156. Dynamic Memory Allocation with Operators new
and delete
- The new and delete operators provide a means for
performing dynamic memory allocation. - new
- The new operator automatically creates an object
of the proper size, calls the constructor for the
object and returns a pointer of the correct type.
166. Dynamic Memory Allocation with Operators new
and delete (cont.)
- new (cont.)
- Example TypeName typeNamePtr
typeNamePtr new TypeName - If new is unable to find space, it returns a NULL
pointer. - In the new ANSI/ISO draft new throws an
exception -- Chapter 13 discusses this, along
with how to catch the exception.
176. Dynamic Memory Allocation with Operators new
and delete (cont.)
- delete
- To free space for this object you must use the
delete operator as follows delete
typeNamePtr - C allows you to provide an initializer for a
newly created object as in float thingPtr
new float (3.14159)
186. Dynamic Memory Allocation with Operators new
and delete (cont.)
- A 10 element integer array can be created as
follows int arrayPtr new int 10 - This array can be deleted with the statement
delete arrayPtr - forgetting the in the previous statement can
lead to runtime logic errors on some compilers.
197. static Class Members
- A static class variable represents class-wide
information. - Every class instance can see the static member as
if it were its data member, but there is only one
copy shared among them. - Although static data members may seem like global
variables, static data members have class scope
ad can be public or private (or protected).
207. static Class Members (cont.)
- A classs static members exist even when no
objects of that class exist. - You typically initialize static data members in
the definition file of the class int
Employeecount 0 - If there is a static member function (getCount)
you can call it with the class name and scope
resolution operator the same way
EmployeegetCount( )
217. static Class Members (cont.)
- Referring to the this pointer within a static
member function is a syntax error. - Declaring a static member function const is a
syntax error. - A classs static data members and static member
functions exist and can be used even if no
objects of that class are instantiated. - static member functions can only access static
data members of the class.
227. static Class Members (cont.)
- assert
- The assert macro -- defined in assert.h --
tests the value of a condition - if the value is false, then assert issues an
error message and calls the function abort (part
of stdlib.h) and an error message containing the
following is printed - the line number
- the file name
- the condition tested
237. static Class Members (cont.)
- assert (cont.)
- Assertions do not have to be removed from the
program when debugging is completed. When
assertions are no longer needed the line
define NDEBUG is inserted at the
beginning of the program file. This causes the
preprocessor to ignore all assertions instead of
the programmer having to delete each assertion
manually.
248. Data Abstraction and Information Hiding
- Classes normally hide their implementation
details from the clients of the classes. This is
called information hiding. - Describing the functionality of a class
independent of its implementation is called data
abstraction.
258. Data Abstraction and Information Hiding (cont.)
- C classes allow us to define Abstract Data
Types (ADTs). An ADT has a few properties - data (and its representation)
- operations on that data
- encapsulation
269. Container Classes and Iterators
- Container classes
- Among the most popular types of classes are
container classes (classes designed to hold
collections of objects) - Container classes typically provide services such
as insertion, deletion, searching, sorting and
testing an item for membership. - Arrays, stacks, queues, trees, and linked lists
are examples of container classes.
279. Container Classes and Iterators (cont.)
- Iterators
- It is common to associate iterator objects -- or
more simply iterators -- with container classes. - An iterator is an object that returns the next
item of a collection (or performs some operation
on the next item) - Iterators are normally written as friends so they
can have direct access to the private data.
2810. Proxy Classes
- It is desirable to hide the implementation
details of a class to prevent access to
proprietary information (private data) and
proprietary program logic. - Providing clients of your class with a proxy
class that know only the public interface to your
class enables the clients to use your classs
services without giving the client access to your
classs implementation details.
2910. Proxy Classes (cont.)
- Implementing a proxy class requires several
steps. - First we have our Implementation class we want to
hide. - Second we create an Interface class we want to
allow or customers to use. - You provide the client with the Interface.h file.
and compiled versions of everything else.
30- Interface.h class Implementation
class Interface public
Interface( int) void setValue(
int) int getValue() const private
Implementation ptr
31- Interface.cpp include interface.h
include implementation.h Interface
Interface (int v) ptr(new
Implementation(v)) void
InterfacesetValue(int v)
ptr-gtsetValue(v) int
InterfacegetValue() const return
ptr-gtgetValue()
32- Client.cpp include ltiostream.hgt include
interface.h int main()
Interface i(5) cout ltlt
Interface contains ltlt i.getValue()
ltltendl i.setValue(10) cout ltlt
Interface contains ltlt i.getValue()
ltltendl return 0