appendix 1 using unix gnu c+ + and borland turbo c+978-1-349-23304-5/1.pdf · using unix gnu c++...

21
Appendix 1 Using Unix GNU C+ + and Borland Turbo C+ + Note that Unix and DOS require different return-types for the main function of a program. These are operating system requirements and both are consistent with c++. If the return-type is not of the type that the operating system is expecting, compilers on both operating systems will give warnings, but the program may still be linked and executed if there are no other errors. The example programs in this book all have the return-type void required by DOS. Unix GNU c++ C++ program files have the extension .cc. The return-type of the main( ) function is integer. For all the example programs of this book, void main( ) may be replaced by just main(), the return-type is then implicitly into After logging in, the prompt given by the operating system is $ or %. The user command lines given below follow the % prompt, but they can be used equally well after a $ prompt. Entering text Programs are entered using an editor, this may be vi (found on all Unix systems) or emacs (found on many Unix systems). If emacs is used to enter the program filename.cc, the command line to be typed in is: % emacs rdename.cc // see local documentation on how to use emacs Compiling, linking and running programs GNU C++ preprocesses, compiles and links both ANSI C and C++ program files. After invoking it with a gcc command line we are ready to execute immediately a program if it does not produce any error messages (warning messages should be carefully considered, and the program only subsequently run if the warning is not a serious one). If error messages do occur, then the syntax of the program should be amended using the editor, and the gcc command line entered again. Unless told otherwise, the gcc command will put any executable file that it creates into the file a.out. To compile and linkfilename.cc and put the executable file into a.out, give the command line: % gcc rdename.cc -lg+ + /I compiles & links C+ + libraries, and puts executable file into a.out The executable file is then run using: % a.out 137

Upload: trinhdat

Post on 24-Aug-2019

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Appendix 1 Using Unix GNU C+ + and Borland Turbo C+ +

Note that Unix and DOS require different return-types for the main function of a program. These are operating system requirements and both are consistent with c++. If the return-type is not of the type that the operating system is expecting, compilers on both operating systems will give warnings, but the program may still be linked and executed if there are no other errors. The example programs in this book all have the return-type void required by DOS.

Unix GNU c++

C++ program files have the extension .cc. The return-type of the main( ) function is integer. For all the example programs of this book, void main( ) may be replaced by just main(), the return-type is then implicitly into

After logging in, the prompt given by the operating system is $ or %. The user command lines given below follow the % prompt, but they can be used equally well after a $ prompt.

Entering text

Programs are entered using an editor, this may be vi (found on all Unix systems) or emacs (found on many Unix systems). If emacs is used to enter the program filename.cc, the command line to be typed in is: % emacs rdename.cc / / see local documentation on how to use emacs

Compiling, linking and running programs

GNU C++ preprocesses, compiles and links both ANSI C and C++ program files. After invoking it with a gcc command line we are ready to execute immediately a program if it does not produce any error messages (warning messages should be carefully considered, and the program only subsequently run if the warning is not a serious one). If error messages do occur, then the syntax of the program should be amended using the editor, and the gcc command line entered again.

Unless told otherwise, the gcc command will put any executable file that it creates into the file a.out. To compile and linkfilename.cc and put the executable file into a.out, give the command line: % gcc rdename.cc -lg+ +

/I compiles & links C+ + libraries, and puts executable file into a.out

The executable file is then run using: % a.out

137

Page 2: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

138 Programming in C+ +

Alternatively, by using the -0 option, the executable file can be put into any named mefidentifier, as in: % gcc filename.cc -Ig+ + -0 fidentifier

II if successful, the executable me is now fidentifier

The executable me is then run using: % fidentifier

Program arguments

Arguments may be given to a program through the command line; each argument is usually separated by 'white space'. The first argument (argv[ 0 ]) is the executable file name as in the command line: % game2 cities II argv[ 0 ] is game2 and argv[ 1 ] is cities

The Unix makefile and the make of a program with user-defined libraries

A makeme has to be created (using emacs or vi) before using the make command which will compile and link the meso The makeme may contain comments, interdependency information, macro definitions, and executable commands.

The interdependency lines specify how a target me is to be created from a number of files. If these mes have been updated more recently than their target m.e, then the target m.e will be regenerated according to the commands which follow the dependency line. In its simplest form, a makefile has the following format: target : dependency-list TAB-character executable command

Example showing how the game3 program me can be compiled and linked together with the mymask library (discussed in Chapter 6): Use emacs or vi to create a makef"lIe where the contents of makeme are: game3exe: mymask.o game3.0

gcc mymask.o game3.0 -lg++ -0 game3exe game3.0:

gcc -c game3.cc -lg++ mymask.o:

gcc -c mymask.cc -lg++

This makeme can then be used, at any time after game3.cc, mymask.h, mymask.cc have been entered using emacs or vi, to produce the executable me game3exe. as in: %make game3exe

If all is successful, game3exe can now be executed using: %game3exe

Page 3: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Using Unix gnu C++ and Borland Turbo C++ 139

Using Borland's Turbo C++ for DOS or DOS + Windows

Turbo C++ program files have the extension . cpp. This is a self-contained program development environment. It contains editing

facilities as well as a compiler and a linker that handle both C and C+ + files.

Entering text

A window is provided for entering and editing a file. The editor enables simple insertion and deletion at the cursor position, and use of the cursor keys to move around the window. A file may be given a name (or a new name) by using the File Save As command. More than one editing window may be open during editing sessions - this is particularly useful for developing multi-file programs. The initial editing window will either be a noname. cpp file, or your own filename.cpp file if you have already retrieved it using the File Manager.

Compiling, linking and running programs

When a program file has been entered, it is compiled using the Compile Compile command producing an object file.

If a program file is successfully compiled, it is then linked and an executable file run using the Run Run command.

Program arguments

These are entered for a program using Run Arguments. Note that argv[ 0 ] is assumed to be the executable file name and so only argv[ 1] argv[ 2 ] etc need be entered, each separated by white space.

Using a Project to make and link programs with useNlefined libraries

Note that header files are included within program files (and maybe in one or more implementation files), so they need not be separately compiled or involved in a make. However, implementation files do need to be compiled before they are linked to a program module.

Enter the Project section of the menu given at the top of the screen. Give a project name, extension .prj. Add the filenames of the program file and the implementation file(s) that you wish to make (you can also delete as required). Exit project.

After the editor has been used to enter the text of the program file and the library files, ensure that the program is the active file. make may be then be used from the Compile section of the menu.

If all is successful, you can then run the project whenever you wish.

Page 4: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Appendix 2 Escape sequences and ASCII chars

c+ + escape sequences, for use within strings (character arrays)

\r CR, carriage return \n newline (LF line feed & CR ) \t horizontal tab \ v vertical tab \f form feed \' single quote char \" double quote char \b backspace \a bell, alert \ \ backslash char itself \0 mill, string terminator

The American Standard Code for Information Interchange (ASCm char set Code Char Code Char Code Char Code C1uJr 0 Null 32 (space) 64 @ 96 1 SOH 33 ! 65 A 97 a 2 Start text 34 " 66 B 98 b 3 End text 35 # 67 C 99 c 4 EOT 36 $ 68 D 100 d 5 ENQ 37 % 69 E 101 e 6 ACK 38 & 70 F 102 f 7 Bell 39 71 0 103 g 8 Backspace 40 ( 72 H 104 h 9 H. tab 41 ) 73 I 105 10 LF 42 * 74 J 106 j 11 V. tab 43 + 75 K 107 k 12 Form feed 44 76 L 108 I 13 CR 45 77 M 109 m 14 SO 46 78 N 110 n 15 SI 47 / 79 0 111 0

16 DLE 48 0 80 P 112 P 17 DC1 49 1 81 Q 113 q 18 DC2 50 2 82 R 114 r 19 DC3 51 3 83 S 115 s 20 DC4 52 4 84 T 116 t 21 NAK 53 5 85 U 117 u 22 SYN 54 6 86 V 118 v 23 ETB 55 7 87 W 119 w 24 CAN 56 8 88 X 120 x 25 EM 57 9 89 Y 121 Y 26 SUB 58 90 Z 122 z 27 ESCape 59 91 [ 123 { 28 FS 60 < 92 \ 124 I 29 OS 61 93 ] 125 } 30 RS 62 > 94 A 126 31 US 63 ? 95 127 DELete

140

Page 5: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Appendix 3 C+ + keywords

All c++ keywords are written in lower case.

asm delete if return union auto do inline short unsigned break double int signed virtual case else long sizeof void catch enum new static volatile char extern operator struct while class float private switch const for protected template continue friend public this default goto register typedef

141

Page 6: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Appendix 4 Operator precedence and associativity

The C+ + operators grouped in order of decreasing precedence.

Le'Vel Operators Associativity

1 .. left to right

2 -> [ ] left to right

3 prefix ++ prefix -- right to left

postfix ++ postfix --

unary + unary - unary * unary &

( type ) sizeof new delete

4 ->* * left to right

5 * / %' left to right

6 + left to right

7 « » left to right

8 < <= > >= left to right

9 != left to right

10 & left to right

11 A left to right

12 left to right

13 && left to right

14 II left to right

15 ? : left to right

16 *= /= %'= += right to left

&= A

1= «= »= =

17 left to right

142

Page 7: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Appendix 5 The iostream libraries

Input and output facilities are not defined within the c++ language, but are provided by the iostream libraries. A stream is an abstraction referring to any flow of data from a source (or producer) to a sink (or consumer). At the lowest level, this is interpreted as the transfer of a sequence of bytes, and there is no concept of the type of the data bytes being handled. At the user level, a stream involves the transfer of a sequence of objects such as characters, integers, etc.

The iostream libraries have two parallel families of classes: those derived from streambuf, and those derived from ios. Both of these base classes are low level classes, and each does a separate set of tasks. All stream classes have at least one of these classes as a base class.

The streambuf classes

The streambuf class provides an interface to physical devices. It provides methods for buffering and handling streams when little or no formatting is required. The classes rdebuf and strstreambuf are derived from streambuf.

The ios family of classes

The inheritance diagram for the ios family, shown on the following page, is not a simple hierarchy because of the generous use of multiple inheritance whereby a single class can inherit from more than one base class. Virtual inheritance, through the use of virtual base classes, avoids multiple declarations. So fstream, for instance, inherits members from ios, istream, ostream, fstreambase, and iostream.

The ios class definition includes a pointer member to a streambuf. It enables formatted input and output with error-checking using a streambuf. All classes in the ios family use a streambuf (or a ruebuf or strstreambuf, which are special cases of a streambuf) as a source and/or sink.

The predefined open streams

The iostream class provides four predefined open streams:

istream _ withassign ein; ostream _ withassign cout; ostream _ withassign eerr; ostream _ withassign clog;

/ / standard input stream, usually keyboard / / standard output stream, usually screen / / standard error with unbuffered output

/ / standard error with buffered output

ios provides formats for objects of the predefined data types and types derived from these. Users may provide formats for class objects through overloading.

143

Page 8: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

144 Programming in C+ +

An inheritance diagram for the ios family of classes

The extraction operator

Stream input is accomplished with the extraction operator > >. Its left operand is an object of class istream. The right operand can be of any type or class for which stream input has been defined.

By default, the extraction operator skips whitespace (as defined by the isspace function in ctype.h), then reads in characters appropriate to the type of the input object until whitespace is again encountered.

The action taken when whitespace is encountered may be altered by using the ws manipulator, or by using several iostream member functions.

Page 9: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

The iostream libraries 145

The insertion operator

Stream output is accomplished with the insertion operator < <. Its left operand is an object of class ostream. Its right operand is an object of any type or class for which stream output has been defined.

The < < operator associates from left to right and returns a reference to the ostream operator for which it was invoked. This allows insertions to be cascaded in a single statement.

ManipulaJors

A way to change some of the default format is to use special function-like operators called manipulaJors. These take a stream reference as an argument and return a reference to the same stream. Manipulators can be embedded in a chain of insertions or extractions to alter stream states without actually performing any insertions or extractions. The set of manipulators include:

dec hex oct WI

endl ends nosh setbase( int n )

setftU( int c ) setpredsion( int n ) setw( int n)

/I set decimal conversion base format /I set hexadecimal conversion base format

/I set octal base format /I extract whitespace characters

" insert newline and flush stream " insert terminal null '\0' in stream

" flush an ostream '* set conversion base format to base n where n is 0, 8, 10, 16, 0 is default giving decimal *'

/I set the fill character to c - default is' , /I set the float precision to n

/I set the fieldwidth to n

The iomanip library enables the manipulators which take arguments. So iomanip.h should be included when such manipulators are used.

The input and output libraries

The stream class libraries consists of several classes declared in three header files: lostream.h, fstream.h, and strstream.h.

Only a selection of detail is given here about classes in the iostream and fstream libraries.

Page 10: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

146

The iostream library

( i ) class ios

Programming in C+ +

1* provides operations common to both input and output, specialising in high-level formatting *1

some public member functions: int bad( ); II nonzero if error occurred

II nonzero on end" of file II nonzero if an operation failed

II returns the current fill character

int eof( ); int fail( ); char r.u( ); char fill( char ); int good(); int width( ); int width( int );

( ii ) class iostream

II resets the fill character, returns the previous one II nonzero if no errors appeared

II returns the current width setting II sets the width as given; returns the previous width

1* inherits from its base classes, allowing both input and output on a stream *1

( iii ) class iostream _ withassign II as iostream plus an overloaded assignment operator

( iv ) class istream 1* provides formattedl unformatted input » operator overloaded for all predefined types *1

some public member functions: int gcount( ); II returns the numbers of characters last extracted int get( ); II extracts the next character or EOF istream& get( signed char* buffer, int len, char = '\n' ); istream& get( unsigned char* buffer, int len, char = '\n' );

1* both of these extract chars into the given buffer until the delimiter (third argument) is encountered, or until ( len - 1 ) bytes have been read;

a terminating null '\0' is placed on the char string - the delimiter never is; fails only if no chars were extracted *1

istream& get( signed char& ); istream& get( unsigned char& );

II both of these extract a single char into the given char reference istream& getline( signed char* buffer, int len, char = '\n'); istream& getlioe( unsigned char* buffer, int len, char = '\n');

1* both of these are the same as get except that the delimiter is also extracted; the delimiter is not copied to buffer *1

istream& ignore( int n = 1, int delim = EOF); 1* causes up to n characters in the input stream to be skipped;

int peek( ); istream& putback( char );

stops if delim is encountered *1 II returns next char without extraction

II pushes back a character into the stream

Page 11: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

The iostream libraries 147

istream& read( unsigned char*, int); II extracts a given number of chars into an array

istream& seekg( long ); II moves to an absolute position ( as returned from tellg )

istream& seekg( long, seek _ dir); 1* moves to position relative to the current position, following the definition: enum seek_dir{ beg, cur, end }; ·1

long teUg( ); II returns the current stream position

( v ) class istream _ withassign II is an istream with an added assignment operator

( vi ) class ostream 1* provides formatted and unformatted output the « operator is overloaded for all the predefined types *1

some public member functions: ostream& Oush( ); II flushes the stream ostream& put( char ); II inserts the char into the output stream ostream& seekp( long );

II moves to an absolute position (as returned from teUp ) ostream& seekp( long, seek _ dir ); 1* moves to position relative to the current

position, following the definition: enum seek _ dir { beg, cur, end } ./ long teUp( ); II returns the current stream position ostream& write( roost unsigned char·, int n);

II inserts n chars (nulls included) into output stream

( vii ) class ostream _ witbassign II is an ostream with an added assignment operator

The /stream library

The class of of stream inherits the insertion operations from ostream. The class of ifstream inherits the extraction operations from istream. The file stream classes also provide constructors and member functions for creating files and handling file input and output.

By default, files are opened in text mode. This means that, on input, carriage­returnllinefeed are converted to the '\n' character. On output, the '\n' character is converted to carriage-returnllinefeed.

The file opening mode is set with an optional second argument to the open function, chosen from ios constants:

ios :: app ios :: ate los :: in ios :: out

// append data by writing at end of file /1 seek to end of file upon original open /I open for input (default for if streams)

/I open for output(default for of streams)

Page 12: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

148 Programming in C+ +

ios :: binary I I open file in binary mode ios :: trunc I I discard contents if file exists ios :: nocreate II if file does not exist, open fails ios :: noreplace'l if file exists, open for output fails unless ate or app is set

( i ) class fstream I I provides for simultaneous input and output on a f"Ilebufuf constructors :

fstream( ); I I makes an fstream that is not attached to a file fstream( coost char·, int, int = filebuf :: openprot );

II makes an fstream, opens a file, and connects to it fstream( int ); /I makes an fstream, connects to an open file descriptor fstream( int, char·, int);

II makes an fstream connected to an open file and uses specified buffer

( ii ) class ifstream I I provides input operations on a f"Ilebuf constructors:

ifstream( ); I I makes an ifstream that is not attached to a file ifstream( coost char·, int = ios :: in, int = f"Ilebuf :: openprot );

I I makes an ifstream, opens an input file in protected mode, ifstream( int ); II makes an ifstream, connects to an open file descriptor ifstream( int fd, char*, int );

II makes an ifstream connected to an open file and uses specified buffer

( iii ) class of stream I I provides input operations on a f"Ilebuf constructors :

ofstream( ); II makes an of stream that is not attached to a file ofstream( coost char ., int = ios :: out, int = filebuf :: openprot );

II makes an of stream , opens a file, and connects to it ofstream( int); I I makes an of stream, connects to an open file descriptor

ofstream( int fd, char*, int ); II makes an of stream connected to an open file and uses specified buffer

public member functions of these fstream classes: void open( coost char·, int, int = f"Ilebuf :: openprot );

f"Ilebuf* rdbuf( ); void attach( int); void close( ); void setbuf( char·, int );

II opens a file for an fstream I I returns the filebuf used

II connects to an open file descriptor II closes the associated rdebuf and file

I I uses a specified buffer

Page 13: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Appendix 6 Other libraries

The following are some library header file identifiers with a selection of macros and prototypes of functions that are likely to be available together with a C++ compiler. There will be more, many specific to the implementation.

An int return type usually indicates that if a function is successful a non-zero (true) value is returned else a zero (false) value is returned. Only when other int values are returned are they noted here.

ctype.h / / macros and functions for character testing and conversion

int tolower(int ch); int toupper(int ch); /* these convert an integer ch

(in the range 0 to 127) to its lowercase/uppercase value, all others are left unchanged;

if success, returns converted value, else ch returned unchanged */ int isalnum(int c); /* if success,

int isalpha(int c); int isascii(int c);

int iscntrl(int c);

int isdigit(int c); int isgrapb(int c);

int islower(int c); int isupper(int c); int isprint(int c);

int ispunct(int c);

int isspace(int c);

c is a letter (A to Z or a to z) or a digit (0 to 9) */ II if success, c is a letter (A to Z or a to z)

/* if success, the low order byte of c is in the range 0 to 127 */

/* if success, c is a delete character or ordinary control char in range 0 to 32 */

II if success, c is a digit (0 to 9) /* if success, c is a printing character,

like isprint, except that a space character is excluded */ / / if success, c is a lowercase letter (a to z)

II if success, c is an uppercase letter (A to Z) /* if success, c is a printing character,

including space * / /* if success, c is a punctuation character (iscntrl or isspace) */ /* if success, c is a space, tab,

carriage return, new line, vertical tab, or formfeed */

149

Page 14: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

150 Programming in C+ +

tlir.1I " functions for working with directories

lnt chdlr(const cJuu4' path); '* causes the directory specified by path to become the current working directory,

path must specify an existing directory *' lnt mkdir(coDSt cJuu4' path); '* creates anew directory

from the given path *' lnt nndlr(coDSt cbar* path); '* deletes the directory

given by path; the directory named by path must be empty, must not be the current working directory, must not be the root directory *'

floot·1I

FLT DIG FLT MIN FLT MAX DBL DIG DBL MIN DBL MAX LDBL DIG LDBL MIN LDBL MAX

limits. II

CHAR BIT CHAR MAX CHAR MIN INT MAX INT MIN LONG MAX LONG MIN SBRT MAX SBRT MIN UCHAR MAX USBRT MAX UINT MAX ULONG MAX

/I macros giving ranges of real number quantities

/I type float, number of digits of accuracy " type float, minimum value " type float, maximum value

/I type double, number of digits of accuracy " type double, minimum value " type double,maximum value

" type long double, number of digits of accuracy " type long double, minimum value 'I type long double, maximum value

/I macros giving ranges of integral quantities

/I type char, number of bits " type char, maximum value " type char, minimum value 'I type int, maximum value II type int, minimum value

/I type long, maximum value /I type long, minimum value

/I type short, maximum value /I type short, minimum value

/I type unsigned char, maximum value /I type unsigned short, maximum value

/I type unsigned integer, maximum value /I type unsigned long, maximum value

Page 15: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

mllth.h

int abs(int x);

double fabs(double x); int labs(long int x); double acos(double x);

double asin(double x);

double atan(double x); double ceil(double x);

double ftoor(double x);

double exp(double x);

Other libraries 151

" maths functions

'* returns the absolute (positive magnitude) value of an int *'

" returns the absolute value of a double " returns the absolute value of a long int '* returns the arc cosine

of a double in range -1 to 1 *' '* returns the arc sine of a double in range -1 to 1 *'

" returns the arc tangent of a double '* rounds up a double; returns, as a double, the smallest integer not < x *' '* rounds down a double;

returns, as a double, the largest integer not> x *' double fmod(double x, double y);

" gives the value e to power x; '* calculates x modulo y, the remainder of x/y *'

double log(double x); " natural logarithm function double loglO(double x); " common logarithm function double modf(double x, double* ipart); '* splits a double

into integer and fraction parts: breaks x into two parts: the integer and the fraction and stores the integer in ipart

double pow(double x, double y); and returns the fractional part of x *'

'* power function, gives x to the power of y (x**y) *'

" calculates the positive square root of a double double sqrt(double x);

stdlib.h " some commonly used functions

" for program termination void abort(void); " abnormally terminates a program void exit(int status); '* provides normal termination,

closing all files and writing buffered output if necessary *' " for random numbers int rand(void); '* random number generator;

uses a multiplicative congruential random number generator to return successive pseudo-random numbers in the range 0 to RAND_MAX,

returns the generated pseudo-random number *' void srand(unsigned seed); '* initialises random number generator;

the generator is set to a new starting point with each seed number *'

Page 16: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

152 Programming in C+ +

" for searching and sorting void· bsearch(coost void· key, coost void· base, size _ t neiem, size _ t width,

int (·femp)(coost void·, coost void·»; ,. performs a binary search on a sorted list of data objects: makes a binary search for the value ·key in a table (array) of nelem elements in memory where base is the base (Oth element) of the search table, femp is a user-defmed comparison routine that compares two items and returns a value based on the comparison, key is the item to be searched for (the search key), nelem is the number of entries in the table, width is the number of bytes in each entry; on success returns address of the first matching item in the table else returns 0 *' void qsort(void· base, size_t nelem, size_t width, int (·femp)(coost void. ,

coost void·»; ,. sorts using the quicksort algorithm (is an implementation of the "median of three" variant of the quicksort algorithm) where base is the base (Oth element) of the search table, femp is a user-defined comparison routine that compares two items and returns a value based on the comparison, nelem is the number of entries in the table, width is the number of bytes in each entry; returns no value *' strlng.h " functions for handling char arrays terminating with '\0'

char· strcat(char· dest, coost char* src); '* appends a copy of src to the end of dest, the length of the resulting string is

strlen(dest) + strlen(src); returns a pointer to the concatenated strings *' char· strchr(coost char* s, int c); '* scans a string s

for the first occurrence of char c; if success, returns a pointer to the fIrSt occurrence of the char else returns null *'

int strcmp(coost char· sl, coost char*52); '* compares two strings from the first character in each string until the corresponding chars differ

or until the end of the strings is reached; returns an int value that is: < 0 if sl < s2, = = 0 if sl = = s2 ,> 0 if sl > s2 *'

char· strcpy(char* dest, coost char· src); '* copies string src to dest; returns pointer to dest *'

int strlen(coost char· s); '* calculates length of a string; returns the number of characters in s, not counting the terminating null *'

char· strstr(coost char· sl, coost char· 52); '* fmds the fIrSt occurrence of string s2 in string s 1; if success

returns a pointer to the char in s 1 where s2 begins else returns null *' char· strpbrk(coost char* sl, coost char· 52); '* scans a string, sl,

for the first occurrence of any char appearing in s2; if success returns a pointer to the first occurrence a char in s2 else returns null *'

Page 17: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Index

Abstraction 52, 70 abstract base class 122 abstract data structure 70 abstract data type 3, 72, 96 functional decomposition 52 top-down design 52

Address 18, 30, 35, 36, 96, 123 return address 46

Argc 60, 132, 133 Arguments 46,48

and operands 95 constant 53, 92 generic 111 passing by reference 48, 91 passing by value 48, 92 passing to main 60, 64, 132

Argv 60, 64, 108, 109 ~y 28,33,36,49,96,103 ASCII 9, 62, 140

Binding early or static late or dynamic

Block scope

Boolean

90, 122 4,90

5, 6, 122 21 58 21

enumerated constants 41,53 21 21

expressions values

Ch~ 12,37 Cin 10, 63, 94 Classes 3, 5, 72, 79, 129, 135

abstract base class 122, 127 base class 78-SO, 102, 129, 135 class interface 78 class templates 116 constructor initialisation 74,

SO, liS, 117 constructors 74, 80, 85, 89,

104, 125, 135

153

data members 72, 86, 99, 104, liS, 117

definition 72 derived class 78, 122, 135 destructor 74, 81, 89 friend class 99 friend functions 85, 89 hierarchy 4, 6, 79, 128, 129 inheritance 78, 81, 102, lOS,

127, 129, 135 inheritance list 79, 106, 124,

129, 130 instances 74, 90, 129 member functions 72, 73, 85,

89,93, 100, 117, 124, 131 multiple inheritance 129, 135 objects 74, 75, SO, 89, 95,

103, 108, liS, 123, 127 private base class 79 private members 72, 78 protected members 72 public base class 79 public members 72, 75, 79 pure virtual function 127 template Ill, 116 template constructor 117 template member function 117 this pointer 93 user-defined type 95, 111 virtual base class 127, 129 virtual functions 89, 122, 130

Close() 63 Comments 8 Compiling 9, 34, 65, 66, 137, 139 Const 12 Constants 12

constant ~gument 53 constant expressions 35, 119 constant pointer 35, 38

Cout 10, 63, 94 Ctype library 130, 149

Page 18: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

154 Programming in C+ +

Declarations 11 Endl 10 of arrays 33,34, 96 Enumerated constants 41,53 of class objects 75,90, 116 Eof{ ) 64 of enumerated constants 41 Escape sequences 9, 140 of fractional numbers 14 Exception 85, 130, 132 of functions 46 handling 87, 89, 131-134 of integers 12 Expressions 13, 15, 51 of pointers 31,96 Extent 59 of pure virtual functions 127 of references 32 Files 62 of sfructs 41 data 62-64 of template classes 116 file modes 63 of template functions 113 header 9, 38, 62, 65, 73, 86 of virtual functions 124 implementation 65,73, 86 using the new operator 103 program 62, 65, 137, 139

Define 34 Float 14,95 Definitions 11 Formal

of arrays 34, 104 arguments 46, 48, 49, 60 of class templates 116 parameters 111 of classes 72 Free store 59,97 of function templates 112 Friends 85,89 of functions 45 convention 92 of linked lists 99 friend class 99 of member functions 73 friend functions 85,89 of pointers 30,99, 122 Fstream library 62, 147 of predefmed types 11 close( ) 63 of pure virtual functions 127 condition flags 64 of structs 41 file modes 63, 147 of virtual classes 129 fstream class 62, 148 of virtual functions 124 ifstream class 62, 148

Derived classes 72,80, 129 of stream class 62, 148 Derived types 30-41, 96 open( ) 63 Directive 8,9,34 Functions 2, 9, 45-60 Double 14 access functions 69 Duration 58,59,75 arguments 48,90

dynamic 59,97, 99, 108 constructor 74, SO, 91 local 59,97, 107 destructor 74, 81 static 59, 97, 107 friend functions 92

Dynamic function call 46, 90, 122 binding 122 function declaration 46 duration 59, 97, 99, 108 function definition 45

function overloading 90 Editor 62, 137, 139 function prototype 46, 73, Encapsulation 70 113, 122

Page 19: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Index 155

functional decomposition 52 of predefined type objects 12 inline functions SO,73,90 of references 32 main function 9,45,60 of string literals 37 member functions 73, 122 Inline 5, 50, 73, 112 operator definition 91 Instance 72, 74, 90 overloading 85 Instantiation 111 pure virtual 127 Int 12,95 return type 32,46,74 Iomanip library 10, 14 signature 48, 85, 113, 122 Iostream library 8,9, 143 template 111, 112 cin 10, 13,57 virtual 122 condition flags 64

cout 10,57 Get( ) 29,57,63 fstream class 62, 148 Getline( ) 44,63,81 getO 29,57,63

getline( ) 44,63, 81 Header file 9,65, 73 ifstream class 62, 148 Heap 59 istream class 57, 146

ignore( ) 57,63 Identifiers 11, 18, 90 ios base class 62, 143

class 72,74 iostream class 143, 146 class object 75 manipulators 10, 145 function 45, 73, 85, 90, of stream class 62, 148

112, 122 ostream class 57, 147 member function 75 put( ) 57 template function 117 !stream class 57, 146 template parameter 111 member functions 57,81 virtual function 135

Ignore( ) 57,63 Keywords 11, 141 Imperative 1 Implementation biding 70 Libraries 65, 66, 73, 135 Include 9,38,62,65 ANSI C libraries 3, 149 Inheritance 6, 72, 78, 92, 122 iostream libraries 143

derived class 78 user-defined 65,75 diagram 79, 123, 129 Linked lists 97 iostream classes 62 base classes 99 list 79,80, 115, 117 heterogeneous 98, 127, 132 multiple 6, 135 homogeneous 97

Initialisation 11, 12, 31 nodes 98, 129 list 80, 115, 117 I ,inking 66, 137, 139 of arrays 35,36 Local 40 of class objects 75,90, 116 duration 59, 97, 107 of constructors 80, 115 scope 40, 58, 97, 107 of enumerated constants 41 Long 12 of pointers 31 Lvalue 15, 31, 32, 34

Page 20: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

156 Programming in C+ +

Macro Main

arguments Make Manipulators Members

34 9,45, 60

60, 138, 139 138, 139

10, 14, 145

of classes 72-75, 92 of derived classes 78-81, 104,

122-124, 128, 129 ofsttucts 41

Name 18

Object-oriented 4 language 4, 78, 135 methodology 5, 6, 135 programming 4, 126, 135

Objects 4, 8, 12, 18, 74, 75, 80 initialisation 75, 116

Open() 63 ~rators 10,12,92,95,135

arithmetic 12 assignment 15, 32, 92, 93,

associativity binary comma

115, 116 16, 22, 142

12 51 16 compound assignment

conditional 60 25,32

97, 102, 103 30,31

42, 57, 75, 89 11,37,63, 85,

94, 144

decrement delete dereferencing direct selection extraction

increment 25, 32 indirect selection 32, 42, 75,

indirection insertion

122, 123 30

10, 37, 63, 85, 94, 145

logical 22 new 97, 102, 103, 117 operator definitions 92

operator symbols overloading precedence reference relational

95, 142 11, 86, 91

13, 142 30, 32

22 scope resolution selection

73 41,89

16 34, 102, 104, 116

17, 105 12, 16, 17

sizeof subscript type cast unary

Ostream class member functions

Overloading overloaded functions

57, 147 57

5,85 85 85 overloaded operators

Parameters 45, 116 Pointers 18,30,37,42,48,96,98

array identifiers 35 constant 35, 38 declaring 30 null pointer 98 table of 122

Polymorphism 5, 6, 122, 126, 135 Predefined types 8, 11, 30

fractional 14 integer 12

Preprocessor 9 directives 8, 9, 34

Private 78, 85, 116 Program arguments 64, 138, 139 Protected 79, 81, 85 Prototype 46, 58, 67, 73, 113 Public 75, 79 Put() 57

Reference Reference type Return Return type Reusability Run

32,57,91,93 32,48, 116

50,93 32,74

135 60, 137, 139

Page 21: Appendix 1 Using Unix GNU C+ + and Borland Turbo C+978-1-349-23304-5/1.pdf · Using Unix gnu C++ and Borland Turbo C++ 139 Using Borland's Turbo C++ for DOS or DOS + Windows Turbo

Index 157

Scope 58,75,90 function 111, 112 block 58,75 generic parameters 116 class 59,73,90 generic types 111 file 59,97, 107 instantiation 111 function 58 overriding 112 function prototype 58 parameter list 111, 119 local 40, 58, 97, 107 This 93

Setprecision 10, 14,47 Top-down design 52 Signature 48, 113, 122 Type 3, 18,96 Stack 40, 46 abstract data 3,96 Statements 1,21,24 array 33

block 21,40 class 96 break 28 derived 30-43,96 continue 28 fractional 14 do 26,27 generic 112, 117 expression 15 integer 12 for 26,27 pointer 30 if 23 predefined 8 if else 23 reference 32,48,93 repetition 26 referenced 30,48 return 50 return 32,45,57 selection 23 string 37 switch 24 struct 41,96 while 26 user~fined 95, 111

Static Type conversions 17, 102, 121 binding 90 explicit 17,47, 105 duration 59, 97, 107 implicit 15, 17, 112

Stdlib library 19, 86, 130, 151 String library 38, 86, 91, 130, 152 Unsigned 12 Strings 9,37

string constant 9 Virtual 6, 122, 127 string literal 9,37,90 Virtual base class 122, 127, 129

Struct 41,96 Virtual function 122, 135 Structured 2 inline 124

flow of control 2,21 redefining 130, 131 languages 2 Visibility 58,59,75 layout 21 Void 9,46 methodology 5 programming 6,21 White space 11, 29, 37, 44 top-down design 52

Templates 4,5, 111, 120 class 111, 116, 119 constant parameters 119