Friday, 11 May 2012

Object-Based Programming


Although we've yet to write a class of our own, we've used classes extensively since Chapter 1: the string and vector classes, the iostream classes supporting input and output, and so on. In this chapter, we design and implement our own classes.

What do we know about classes from our use of them? Before we can use a class, we must make it known to the program because the class is not built into the language. Usually, we do this by including a header file:

#include <string> 
string pooh[ 4 ] = 
    { "winnie", "robin", "eeyore", "piglet" }; 


The class name serves as a type name in the same way as the built-in type names such as int and double. Often, there are multiple ways to initialize an object of a class:

#include <vector> 


string dummy( "dummy" ); 
vector< string > svec1( 4 ); 
vector< string > svec2( 4, dummy ); 
vector< string > svec3( pooh, pooh+4 ); 


Each class provides a set of operations we can apply to objects of the class. These operations typically consist of named functions, such as size() and empty(), and overloaded instances of the predefined operators, such as inequality and assignment:

if ( svec2 != svec3 && ! svec3.empty() ) 
     svec2 = svec3; 


if ( svec2.size() == 4 ) 
     // all is well ... 

What we don't know, generally, is how the class is implemented. Does the string class calculate its size at each request, or does it store the size within each class object? Are the elements of the vector stored within the vector object, or are the elements stored elsewhere and addressed within the vector object by a pointer?

In general, a class consists of two parts: a public set of operations and operators, and a private implementation. These operations and operators are called class member functions and represent the public interface of the class. As users of a class, we can access only the public interface. This, in fact, is how we have used the string class, the vector class, and so on. For example, all we know about the size() member function of string is its prototype: It has a void parameter list and returns an integer value.

The private implementation of a class consists of the member function definitions and any data associated with the class. For example, if the string class object calculates the length of its string with each invocation of size(), no associated data is required and the definition of size() is likely to involve a for loop walking the length of the string. If the string class object stores the length of its string, a private data member must be defined within each class object. This definition of size() returns the value of that member. Each time the length of the string is modified, the data member must be updated.

These kinds of implementation details are usually of no concern to the user of the class. As users, we simply program to the public interface. In this way, as long as the interface does not change, our code using that interface also does not need to change, even if the underlying implementation is reengineered.

In this chapter, we turn from just using classes to providing classes for ourselves and others to use. Designing and implementing classes are the primary activities of C++ programmers.


0 comments:

Post a Comment