#!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # 6_7.c 6_7.cmp 6_7a.c 6_7tst.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:20 EDT 1990 # echo x - 6_7.c sed 's/^X//' > 6_7.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Exercise 6.7 // print out the conversions and // values for each expression #include struct X { int i; X(int in) { cout << "converting int " << in << " to X\n"; i = in; } operator+(int in) { cout << "adding X " << i << " + int " << in << "\n"; int ret = i + in; cout << "op+(X,int) returns " << ret << "\n"; return ret; } }; struct Y { int i; Y(X Xin) { cout << "converting X " << Xin.i << " to Y\n"; i = Xin.i; } operator+(X Xin) { cout << "adding Y " << i << " to X " << Xin.i << "\n"; int ret = i + Xin.i; cout << "op+(Y,X) returns " << ret << "\n"; return ret; } operator int() { cout << "converting Y " << i << " to int\n"; return i; } }; XX operator* (X Xin, Y Yin) { cout << "multiplying X " << Xin.i << " times Y " << Yin.i << "\n"; X Xout(Xin.i * Yin.i); cout << "op*(X, Y) returns " << Xout.i << "\n"; return Xout; } int f(X Xin) { cout << "calling f(X = " << Xin.i << ")\n"; return Xin.i; } XX x = 1; Y y = x; int i = 2; main() { int ret = i + 10; cout << "i + 10 = " << ret << "\n"; ret = y + 10; cout << "y + 10 = " << ret << "\n"; ret = y + 10 * y; cout << "y + 10 * y = " << ret << "\n"; ret = x + y + i; cout << "x + y + i = " << ret << "\n"; ret = x * x + i; cout << "x * x + i = " << ret << "\n"; ret = f(7); cout << "f(7) = " << ret << "\n"; // illegal conversion // ret = f(y); // cout << "f(y) = " << ret << "\n"; ret = y + y; cout << "y + y = " << ret << "\n"; ret = 106 + y; cout << "106 + y = " << ret << "\n"; return 0; } !EOF! ls -l 6_7.c echo x - 6_7.cmp sed 's/^X//' > 6_7.cmp << '!EOF!' converting int 1 to X converting X 1 to Y i + 10 = 12 converting int 10 to X adding Y 1 to X 10 op+(Y,X) returns 11 y + 10 = 11 converting int 10 to X multiplying X 10 times Y 1 converting int 10 to X op*(X, Y) returns 10 adding Y 1 to X 10 op+(Y,X) returns 11 y + 10 * y = 11 converting Y 1 to int adding X 1 + int 1 op+(X,int) returns 2 x + y + i = 4 converting X 1 to Y multiplying X 1 times Y 1 converting int 1 to X op*(X, Y) returns 1 adding X 1 + int 2 op+(X,int) returns 3 x * x + i = 3 converting int 7 to X calling f(X = 7) f(7) = 7 converting Y 1 to int converting Y 1 to int y + y = 2 converting Y 1 to int 106 + y = 107 !EOF! ls -l 6_7.cmp echo x - 6_7a.c sed 's/^X//' > 6_7a.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ struct X { int i; X(int); operator+(int); }; struct Y { int i; Y(X); operator+(X); operator int(); }; XX operator* (X, Y); int f(X); XX x = 1; Y y = x; int i = 2; main() { i + 10; y + 10; y + 10 * y; x + y + i; x * x + i; f(7); f(y); y + y; 106 + y; } !EOF! ls -l 6_7a.c echo x - 6_7tst.c sed 's/^X//' > 6_7tst.c << '!EOF!' /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ /* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */ /* The C++ Answer Book */ /* Tony Hansen */ /* All rights reserved. */ // Exercise 6.7 // print out the conversions and // values for each expression #include struct X { int i; X(int in) { cout << "converting int " << in << " to X\n"; i = in; } operator+(int in) { cout << "adding X " << i << " + int " << in << "\n"; int ret = i + in; cout << "op+(X,int) returns " << ret << "\n"; return ret; } }; struct Y { int i; Y(X Xin) { cout << "converting X " << Xin.i << " to Y\n"; i = Xin.i; } operator+(X Xin) { cout << "adding Y " << i << " to X " << Xin.i << "\n"; int ret = i + Xin.i; cout << "op+(Y,X) returns " << ret << "\n"; return ret; } operator int() { cout << "converting Y " << i << " to int\n"; return i; } }; XX operator* (X Xin, Y Yin) { cout << "multiplying X " << Xin.i << " times Y " << Yin.i << "\n"; X Xout(Xin.i * Yin.i); cout << "op*(X, Y) returns " << Xout.i << "\n"; return Xout; } int f(X Xin) { cout << "calling f(X = " << Xin.i << ")\n"; return Xin.i; } XX x = 1; Y y = x; int i = 2; main() { int ret = i + 10; cout << "i + 10 = " << ret << "\n"; ret = y + 10; cout << "y + 10 = " << ret << "\n"; ret = y + 10 * y; cout << "y + 10 * y = " << ret << "\n"; ret = x + y + i; cout << "x + y + i = " << ret << "\n"; ret = x * x + i; cout << "x * x + i = " << ret << "\n"; ret = f(7); cout << "f(7) = " << ret << "\n"; // illegal conversion // ret = f(y); // cout << "f(y) = " << ret << "\n"; ret = y + y; cout << "y + y = " << ret << "\n"; ret = 106 + y; cout << "106 + y = " << ret << "\n"; return 0; } !EOF! ls -l 6_7tst.c echo x - makefile sed 's/^X//' > makefile << '!EOF!' CC= CC -I. -I../../CC all: 6_7 6_7: 6_7.c $(CC) 6_7.c -o 6_7 CMP= 6_7.cmp OUT= 6_7.out 6_7.out: 6_7 ; 6_7 > 6_7.out test: all $(OUT) $(CMP) cmp 6_7.out 6_7.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