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

26 Doing Data

Validation with

Technique Classes

Save Time By

Understanding data validation with classes

Creating a data-validation class

Testing your class

Data validation is one of the most basic and pervasive functions of a computer program. Before you can operate on a given piece of data, you need to know whether or not it is valid. It doesn’t matter

if it is a date, a time, an age, or a Social Security number; the data you accept into your program will cause problems if it is in an invalid format.

Validating a data type is a perfect form of encapsulation, which makes it a perfect task to assign to a C++ class. Because we encapsulate both the data and the rules for the data type within a class, we can move that class from project to project, anywhere that the data type is needed. This saves time in implementing the class, as well as time and effort in validating and testing the class.

When you’re writing an application, take time to identify the data types you’re using. Write classes to validate, save, and load these data types and you will save yourself endless time debugging and extending your applications.

Implementing Data Validation with Classes

Follow these steps to create your own validation classes:

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

In this example, I call my class ch26.cpp.

2. Type the code from Listing 26-1 into your file.

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

Implementing Data Validation with Classes

143

LISTING 26-1: THE VALIDATION CLASS

#include <string>

//Constants used in this validation #define SSN_LENGTH 9

#define SSN_DELIMITER ‘-’

//The validator class

class SSNValidator

{

//Internal member variables private:

//This is the actual SSN. std::string _ssn;

//This is the flag indicating validity. bool _valid;

1

2

protected:

 

bool

IsValid(const char *strSSN);

public:

//Constructors and destructor SSNValidator();

SSNValidator( const char *ssn ); SSNValidator( const std::string& ssn ); SSNValidator( const SSNValidator& aCopy ); virtual ~SSNValidator();

//Accessors for this class

bool

Valid()

{ return _valid; };

std::string

SSN() { return _ssn; };

void

setSSN(

const char *ssn);

// Operators for this class

SSNValidator operator=( const char *ssn ); SSNValidator operator=( const std::string& ssn ); SSNValidator operator=( const SSNValidator& aCopy ); operator const char *();

};

3. Save your code in the code editor.

4. Type the code from Listing 26-2 into your new

This will be the definition for our Validator

file.

 

object. This class can then be included in other

Better yet, copy the code from the source file on

modules to do validation of the type we are

this book’s companion Web site.

defining. In this example, we are validating a U.S.

 

Social Security Number.

 

144 Technique 26: Doing Data Validation with Classes

LISTING 26-2: SOCIAL SECURITY NUMBER VALIDATOR

#include <ctype.h>

bool SSNValidator::IsValid(const char *strSSN)

{

int i;

//No NULL values allowed. if ( strSSN == NULL )

return false;

//Copy the result into a string, removing all delimiters. std::string sSSN;

for ( i=0; i<(int)strlen(strSSN); ++i ) if ( strSSN[i] != SSN_DELIMITER )

sSSN += strSSN[i];

//Must be 9 characters.

if ( strlen(sSSN.c_str()) != SSN_LENGTH ) return false;

//Check to see whether all characters are numeric. for ( i=0; i<(int)strlen(sSSN.c_str()); ++i )

if ( !isdigit( sSSN[i] ) ) return false;

//Must be okay.

return true;

}

// Constructors and destructor SSNValidator::SSNValidator()

{

_ssn = “”; _valid = false;

}

SSNValidator::SSNValidator( const char *ssn )

{

// Only assign if valid. _valid = IsValid( ssn ); if ( _valid )

_ssn = ssn;

}

SSNValidator::SSNValidator( const std::string& ssn )

{

// Only assign if valid.

_valid = IsValid( ssn.c_str() ); if ( _valid )

_ssn = ssn;

}

SSNValidator::SSNValidator( const SSNValidator& aCopy )

Implementing Data Validation with Classes

145

{

_ssn = aCopy._ssn; _valid = aCopy._valid;

}

SSNValidator::~SSNValidator()

{

}

void SSNValidator::setSSN( const char *ssn)

{

// Only assign if valid. if ( IsValid( ssn ) )

{

_valid = true; _ssn = ssn;

}

}

// Operators for this class

SSNValidator SSNValidator::operator=( const char *ssn )

{

// Only assign if valid. if ( IsValid( ssn ) )

{

_valid = true; _ssn = ssn;

}

return *this;

}

SSNValidator SSNValidator::operator=( const std::string& ssn )

{

// Only assign if valid.

if ( IsValid( ssn.c_str() ) )

{

_valid = true; _ssn = ssn;

}

return *this;

}

SSNValidator SSNValidator::operator=( const SSNValidator& aCopy )

{

_valid = aCopy._valid; _ssn = aCopy._ssn; return *this;

}

SSNValidator::operator const char *()

{

return _ssn.c_str();

}