Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Advanced C 1992

.pdf
Скачиваний:
93
Добавлен:
17.08.2013
Размер:
4.28 Mб
Скачать

Part V •Appendixes

Listing C.8. continued

Customer1.PrintCustomer();

cout << “\n”;

Customer2.PrintCustomer();

cout << “\n”;

Customer2.GetCustomer();

cout << “\n”;

Customer2.PrintCustomer();

cout << “\n”;

}

In this program, you first create a class. Then you tell the compiler the class name and describe the class:

class Customer

{

Following specification of the class name, describe those members in the class that are to be public. If a member is public, you can access it from the actual program; if a member is private, you can access it only from a function of the class.

public:

In the public section, declare two class constructors by overloading the class constructor function. This way, you create a default constructor with no parameters and a constructor that initializes the class to specified values. Then declare a function to get, from the keyboard, the class’s data. You declare a function to print that data to the screen. You also create a class destructor.

Both the constructor and the destructor are required in creating a class. If you don’t need these functions, you still must create a function that does nothing. A class constructor always has the same name as the class. The destructor also has the same name as the class, but is preceded by a ~ character.

718

 

Introduction to C++

C C C

 

 

C

C

 

 

 

C

 

 

CCC

Customer();

// The default constructor

 

 

Customer(int nRecType,

// The class constructor

 

 

char * szCustName,

 

 

 

char * szAddr,

 

 

 

double dSales);

 

 

 

void GetCustomer();

 

 

 

void PrintCustomer();

// Print a customer’s information

 

 

~Customer();

// Destructor

 

 

Well hidden from your actual program, in the private section, are the actual data objects for this class. You keep them hidden (by making them private) so the program cannot modify them directly, but instead can modify them only through a function of the class.

You define each variable just as you define a structure in C. Any data type is permissible, including other classes.

private:

int

nRecordType;

char

szCustomerName[120];

char

szAddress[120];

double

dCurrentSales;

};

Once the class is defined, you must provide the functions that are part of the class. You must define these functions after the definition of the class itself.

The first function you create is the class constructor that initializes the class’s members to default values. These default values can be any that are appropriate for both the data’s type and the application. Like a class destructor, a class constructor has neither a return value type nor a return statement.

Customer::Customer() // The class constructor, default values.

{

nRecordType = CUSTOMER_RECORD; strcpy(szCustomerName, “-NONE-”); strcpy(szAddress, “-NONE”); dCurrentSales = 0.0;

}

719

Part V •Appendixes

The next function is also a constructor (done by overloading the constructor function) that allows your program to specify the values to assign to the class’s members.

Customer::Customer(int nRecType,

// The class constructor, explicit

 

// values

char * szCustName,

 

char * szAddr,

 

double dSales)

 

{

 

nRecordType = nRecType; strcpy(szCustomerName, szCustName); strcpy(szAddress, szAddr); dCurrentSales = dSales;

}

You next define the class function that gets, from the keyboard, new values for the class’s members. This function uses C++’s cin and cin.getline classes. Using cin for numeric values is fine; however for character string values, cin.getline is better because it limits the number of characters assigned and ignores any whitespace characters in the input string. Class functions, other than constructors and destructors, can have return values. You can use these return values to indicate either success or failure of the function, or to return a class member’s value.

void Customer::GetCustomer()

{

char szLine[2]; // Used to store a NEWLINE for cin.getline

//You get, from the console, the object’s data values, using a simple

//multiline format:

cout << “Enter ‘“ << CUSTOMER_RECORD << “‘ for a Customer ‘“ << SUPPLIER_RECORD << “‘ for a supplier: “;

cin >> nRecordType;

//Below you don’t use cin, but cin.getline, which gets all

//characters until the delimiting character (the optional third

//character). If the delimiting character is omitted, assume a

//newline. When getting input, cin.getline() does not

//retrieve more characters than the second parameter specifies,

//taking into account the ending NULL for the string.

720

Introduction to C++

C C C

 

CCC

 

C

C

C

cin.getline(szLine, sizeof(szLine)); // discard NEWLINE from last // input.

cout << “Enter the name: “; cin.getline(szCustomerName, sizeof(szCustomerName));

cout << “Enter the address: “; cin.getline(szAddress, sizeof(szAddress));

cout << “Enter the sales: “; cin >> dCurrentSales;

}

The next function prints the class’s contents to the screen. You can also send the contents to a file, a communications port, and so on. This function is simple, using only cout to print.

void Customer::PrintCustomer()

{

//You print the object’s data values, using a simple

//multiline format:

cout << “Type\t” << nRecordType << “\n” << “Name\t” << szCustomerName << “\n” << “Address\t” << szAddress << “\n” << “Sales:\t” << dCurrentSales << “\n”;

}

The final function is the class destructor (which, like the constructor, is required). Because nothing must be done when the class object is destroyed, you simply return. Like a class constructor, a class destructor has neither a return value type nor a return statement.

Customer::~Customer()

{

//Nothing done here. You don’t have anything to do when the

//object is destroyed.

}

Once you are skilled at using classes, you will find these features helpful. Proper use of class objects limits the potential for program errors by requiring accessing and modifying class members by using an interface layer of functions that perform error checks.

721

Part V •Appendixes

722

Function/HeaderCFile CrossCReference CC C C

CDC C

DC C C

C C C

C C C

Function/Header File

Cross Reference

The prototype for each function is in one or more header files. The following table lists the header file(s), whether the function is ANSI, and the function’s prototype.

If the column labeled ANSI has no entry, the function is not part of the ANSI standard. Many compilers may offer this function; however, you must carefully check whether a given compiler supports the function as you expect.

Table D.1. Header/ Function Cross Reference.

Header file(s)

ANSI

Function prototype

process.h & stdlib.h

ANSI

math.h & stdlib.h

ANSI

abort( )

abs( )

continues

723

Part V • Appendixes

Table D.1. continued

Header file(s)

ANSI

Function prototype

io.h

 

math.h

ANSI

math.h

 

malloc.h

 

time.h

ANSI

math.h

ANSI

math.h

 

assert.h

ANSI

math.h

ANSI

math.h

ANSI

math.h

 

math.h

 

stdlib.h

ANSI

math.h & stdlib.h

ANSI

stdlib.h

ANSI

stdlib.h

ANSI

math.h & stdlib.h

 

malloc.h

 

malloc.h

 

malloc.h

 

malloc.h

 

malloc.h

 

malloc.h

 

malloc.h

 

access( )

acos( ) acosl( ) alloca( )

asctime( ) asin( ) asinl( )

assert( ) atan( ) atan2( ) atan2l( ) atanl( )

atexit( ) atof( ) atoi( ) atol( ) atold( ) bcalloc( ) bexpand( ) bmalloc( ) brealloc( ) bfree( ) bfreeseg( ) bheapadd( )

724

 

 

Function/Header File Cross Reference

C C C

 

 

 

 

CDC

 

 

 

 

C C C

 

 

 

 

C

 

 

 

 

 

 

 

Header file(s)

ANSI

Function prototype

 

 

 

 

 

 

 

 

malloc.h

 

malloc.h

 

malloc.h

 

malloc.h

 

malloc.h

 

malloc.h

 

search.h & stdlib.h

ANSI

math.h

 

math.h

 

malloc.h & stdlib.h

ANSI

math.h

ANSI

math.h

 

process.h

 

conio.h

 

conio.h

 

direct.h

 

direct.h

 

io.h

 

io.h

 

float.h

 

stdio.h

ANSI

time.h

ANSI

io.h

 

io.h

 

float.h

 

bheapchk( ) bheapmin( ) bheapseg( ) bheapset( ) bheapwalk( ) bmsize( )

bsearch( ) cabs( ) cabsl( )

calloc( ) ceil( ) ceill( ) cexit( ) cgets( ) cgets( ) chdir( ) chdrive( ) chmod( ) chsize( ) clear87( )

clearerr( ) clock( ) close( ) commit( ) control87( )

continues

725

Part V • Appendixes

Table D.1. continued

Header file(s)

ANSI

Function prototype

math.h

ANSI

math.h

ANSI

math.h

 

math.h

 

conio.h

 

conio.h

 

io.h

 

conio.h

 

time.h

ANSI

math.h

 

time.h

ANSI

stdlib.h

ANSI

math.h

 

io.h

 

io.h

 

stdlib.h

 

io.h

 

process.h

 

process.h

 

process.h

 

process.h

 

process.h

 

process.h

 

process.h

 

cos( ) cosh( ) coshl( ) cosl( ) cprintf( ) cputs( ) creat( ) cscanf( )

ctime( ) dieeetomsbin( )

difftime( ) div( ) dmsbintoieee( ) dup( )

dup2( ) ecvt( ) eof( ) execl( ) execle( ) execlp( ) execlpe( ) execv( ) execve( ) execvp( )

726

 

 

Function/Header File Cross Reference

C C C

 

 

 

 

CDC

 

 

 

 

C C C

 

 

 

 

C

 

 

 

 

 

 

 

Header file(s)

ANSI

Function prototype

 

 

 

 

 

 

 

 

process.h

 

process.h & stdlib.h

ANSI

math.h

ANSI

malloc.h

 

math.h

 

math.h

ANSI

math.h

 

stdlib.h

 

malloc.h

 

stdio.h

ANSI

stdio.h

ANSI

stdlib.h

 

stdio.h

 

stdio.h

ANSI

stdio.h

ANSI

malloc.h

 

stdio.h

ANSI

malloc.h

 

stdio.h

ANSI

stdio.h

 

stdio.h

ANSI

stdio.h

ANSI

malloc.h

 

malloc.h

 

malloc.h

 

execvpe( )

exit( ) exp( ) expand( ) expl( )

fabs( ) fabsl( ) fatexit( ) fcalloc( )

fclose( ) fcloseall( ) fcvt( ) fdopen( )

feof( ) ferror( ) fexpand( )

fflush( ) ffree( )

fgetc( ) fgetchar( )

fgetpos( ) fgets( ) fheapchk( ) fheapmin( ) fheapset( )

continues

727