#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 6_9a.cmp 6_9a.h 6_9a1.h 6_9a2.h 6_9a3.h 6_9a4.h 6_9test.c makefile # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Mon Jul 30 23:08:33 EDT 1990 # echo x - 6_9a.cmp sed 's/^X//' > 6_9a.cmp << '!EOF!' R1 = -5 r1 = -5 R2 = 5 r2 = 5 R3 = 5 r3 = 5 !EOF! ls -l 6_9a.cmp echo x - 6_9a.h sed 's/^X//' > 6_9a.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Exercise 6.9 // Define a restricted integer class #ifndef RINT_H #define RINT_H #include "6_9a1.h" /* EXPAND */ #include "6_9a2.h" /* EXPAND4 */ #include "6_9a3.h" /* EXPAND4 */ #include "6_9a4.h" /* EXPAND4 */ }; #endif /* RINT_H */ !EOF! ls -l 6_9a.h echo x - 6_9a1.h sed 's/^X//' > 6_9a1.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ class RINT { int i; public: // ... !EOF! ls -l 6_9a1.h echo x - 6_9a2.h sed 's/^X//' > 6_9a2.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ RINT() {} // RINT x; RINT(int j) { i = j; } // RINT x = 3; RINT(RINT& j) { i = j.i; } // RINT x = RINT X~RINT() {} !EOF! ls -l 6_9a2.h echo x - 6_9a3.h sed 's/^X//' > 6_9a3.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ RINT& operator= (RINT &j) // x = y; { i = j.i; return *this; } RINT& operator= (int j) // x = 5; { i = j; return *this; } !EOF! ls -l 6_9a3.h echo x - 6_9a4.h sed 's/^X//' > 6_9a4.h << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ friend RINT operator+(RINT& i, RINT& j) { RINT ret = i.i + j.i; return ret; } friend RINT operator+(RINT& j) { RINT ret = j.i; return ret; } friend RINT operator-(RINT& i, RINT& j) { RINT ret = i.i - j.i; return ret; } friend RINT operator-(RINT& j) { RINT ret = -j.i; return ret; } friend RINT operator*(RINT& i, RINT& j) { RINT ret = i.i * j.i; return ret; } friend RINT operator/(RINT& i, RINT& j) { RINT ret = i.i / j.i; return ret; } friend RINT operator%(RINT& i, RINT& j) { RINT ret = i.i % j.i; return ret; } operator int() { return i; } /* DELETE */ !EOF! ls -l 6_9a4.h echo x - 6_9test.c sed 's/^X//' > 6_9test.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ #include #include "6_9a.h" main() { RINT R1 = 5; int r1 = 5; RINT R2 = 0; int r2 = 0; RINT R3 = R1; int r3 = r1; R1 = 4; r1 = 4; R2 = R3; r2 = r3; R1 = R3 + R2; r1 = r3 + r2; R1 = R3 * 5; r1 = r3 * 5; R1 = 5 - R3; r1 = 5 - r3; R1 = -R3; r1 = -r3; cout << "R1 = " << int(R1) << "\n"; cout << "r1 = " << r1 << "\n"; cout << "R2 = " << int(R2) << "\n"; cout << "r2 = " << r2 << "\n"; cout << "R3 = " << int(R3) << "\n"; cout << "r3 = " << r3 << "\n"; return 0; } !EOF! ls -l 6_9test.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC all: 6_9a 6_9a: 6_9test.c 6_9a.h 6_9a1.h 6_9a2.h 6_9a3.h 6_9a4.h $(CC) 6_9test.c -o 6_9a CMP= 6_9a.cmp OUT= 6_9a.out 6_9a.out: 6_9a ; 6_9a > 6_9a.out test: all $(OUT) $(CMP) cmp 6_9a.out 6_9a.cmp echo tests done !EOF! ls -l makefile # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0