Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ Timesaving Techniques (2005) [eng].pdf
Скачиваний:
65
Добавлен:
16.08.2013
Размер:
8.35 Mб
Скачать

Testing the Mailing-List Application

17

{

printf(“Enter the next name for the mailing list:\n”); return GetAnEntry();

}

};

Testing the Mailing-List

Application

1. In the code editor of your choice, reopen the source file to hold the code for your test program.

In this example, I named the test program

After you create a class, it is important to create a test driver that not only ensures that your code is correct, but also shows people how to use your code. The following steps show you how:

ch02.cpp.

2. Type the code from Listing 2-4 into your file, substituting your own names for the italicized constants, variables, and filenames.

A more efficient approach is to copy the code from the source file on this book’s companion Web site.

LISTING 2-4: THE MAILING-LIST TEST PROGRAM

void ProcessEntries( BaseMailingListEntry *pEntry )

{

bool not_done = pEntry->First(); while ( not_done )

{

//Do something with the entry here.

//Get the next one

not_done = pEntry->Next();

}

}

int main(int argc, char **argv)

{

int choice = 0;

printf(“Enter 1 to use a file-based mailing list\n”); printf(“Enter 2 to enter data from the command line\n”); scanf(“%d”, &choice );

if ( choice == 1 )

{

char szBuffer[ 256 ]; printf(“Enter the file name: “); gets(szBuffer);

FileMailingListEntry fmle(szBuffer); ProcessEntries( &fmle );

}

(continued)

18 Technique 2: Using Abstraction to Extend Functionality

LISTING 2-4 (continued)

else

if ( choice == 2 )

{

CommandLineMailingListEntry cmle; ProcessEntries( &cmle );

}

else

printf(“Invalid option\n”);

return 0;

}

The main function for the driver really isn’t very busy — all it’s doing is creating whichever type of object you want to use. The ProcessEntries function is the fascinating one because it is a function that is working on a class type that doesn’t do anything — it has no idea which type of mailing-list entry object it is processing. Rather, it works from a pointer to the base class. If you run this program, you will find that it works as advertised, as you can see in Listing 2-5.

You could likewise create a file containing all entries that we just typed into the various fields above to

enter those fields into the system. You can do all of this without changing a single line of the ProcessEntries function! This is the power of pure virtual functions, and thus the power of abstraction.

When you create a set of classes that are all doing the same general thing, look for the common elements of the class and abstract them into a common base class. Then you can build on that common base in the future, more easily creating new versions of the classes as the need arises.

LISTING 2-5: THE MAILING-LIST PROGRAM IN OPERATION

Enter 1 to use a file-based mailing list Enter 2 to enter data from the command line 2

Enter the first name for the mailing list: Enter the last name of the person: Telles Enter the first name of the person: Matt Enter the first address line: 10 Main St Enter the second address line:

Enter the city: Anytown Enter the state: NY

Enter the zip code: 11518

Enter the next name for the mailing list: Enter the last name of the person:

3 Customizing a Class

with Virtual

Technique Functions

Save Time By

Understanding polymorphism

Overriding selected pieces of a class

Customizing classes at run-time

Using destructors with virtual functions

P olymorphism (from the Greek for “having many forms”) is what happens when you assign different meanings to a symbol or operator in different contexts. All well and good — but what does it

mean to us as C++ programmers?

Granted, the pure virtual function in C++ (discussed in Technique 2) is very useful, but C++ gives us an additional edge: The programmer can override only selected pieces of a class without forcing us to override the entire class. Although a pure virtual function requires the programmer to implement functionality, a virtual function allows you to override that functionality only if you wish to, which is an important distinction.

Allowing the programmer to customize a class by changing small parts of the functionality makes C++ the fastest development language. You should seriously consider making the individual functions in your classes virtual whenever possible. That way the next developer can modify the functionality with a minimum of fuss.

Small changes to the derived class are called virtual functions — in effect, they allow a derived class to override the functionality in a base class without making you tinker with the base class. You can use this capability to define a given class’s default functionality, while still letting end users of the class fine-tune that functionality for their own purposes. This approach might be used for error handling, or to change the way a given class handles printing, or just about anything else. In the next section, I show you how you can customize a class, using virtual functions to change the behavior of a base-class method at run-time.

20 Technique 3: Customizing a Class with Virtual Functions

Customizing a Class with Polymorphism

In order to understand how base classes can be customized using the polymorphic ability offered by virtual functions, let’s look at a simple example of customizing a base class in C++.

1. In the code editor of your choice, create a new file to hold the code for the implementation of the source file.

In this example, the file is named ch03.cpp, although you can use whatever you choose.

2. Type the code from Listing 3-1 into your file, substituting your own names for the italicized constants, variables, and filenames.

Better yet, copy the code from the source file on this book’s companion Web site.

LISTING 3-1: THE VIRTUAL FUNCTION BASE-CLASS SOURCE CODE

#include <string> #include <stdio.h>

class Fruit

{

public:

Fruit()

{

}

virtual ~Fruit()

{

printf(“Deleting a fruit\n”);

}

virtual std::string Color()

{

return “Unknown”;

}

void Print()

{

printf(“My color is: %s\n”, Color().c_str() );

}

};

class Orange : public Fruit

{

public:

Orange()

{

}

virtual std::string Color()

{

return “Orange”;

}

};

class Apple : public Fruit

{

public:

Apple()

{

}

virtual std::string Color()

{

return “Reddish”;

}

};

class Grape : public Fruit

{

public:

Grape()

{

}

virtual std::string Color()

{

return “Red”;

}

};

class GreenGrape : public Grape

{

public:

GreenGrape()

{

}

virtual std::string Color()

{

return “Green”;

}

};