of structure initializer initializer-list - inline inline function - member function - inline input and output - of built-in type of user-defined type operator >> >> int int type int type specifier int integer constant conversion integral promotion type interface class inheritance fat specifications internal linkage structure I/O buffering - iteration statement J jump statement jump-statement - K keyword list L label case case default default scope of labeled statement language design and high-level low-level layout bit-field class objects left shift operator less than operator than or equal to operator levels of abstraction lexical conventions library design headers initialization lifetime of object linkage consistency error external internal linker-specification - linker Lisp list of operator functions literal constants loader local class declaration class member function - class, scope of scope locking () logical AND operator OR operator negation operator operators, bitwise long long constant long double long double double constant long double double type long double type long loop statement lvalue assignment and cast conversion modifiable M macro definition, preprocessing expansion, preprocessing function-like name, scope of names, predefined preprocessing syntax summary () maintenance, software management free store memory manipulator member member-declaration - member-declarator - member-list - modifiable lvalue modular programming modularity multicharacter constant multidimensional array multiple inheritance multiplication operator multiplicative-expression - N name global hiding length of linkage of local overloaded function overloaded member qualified scope of nested class declaration class, scope of new operator new newline \n \n node class null character null '\0' pointer (null) O object object-oriented programming - octal constant number operand const const reference volatile volatile operator , , ! ! # # ## ## %= %= && && &= &= *= *= *=, user-defined *= +, user-defined + ++ ++ ++, user-defined ++ += += -, user-defined - -- -- --, user-defined -- -= -= ->, user-defined -> /= /= :: :: <<, output << <<= <<= =, user-defined = >>, input >> >>= >>= ^= ^= address-of assignment associativity binding strength built-in function call precedence sizeof sizeof subscripting summary user-defined operator function function, list of order of argument evaluation of evaluation output formatted input and of built-in type of user-defined type operator << << overflow overloaded assignment operator binary operator decrement operator function call operator function name increment operator member access operator member name operator subscripting operator unary operator overloading and access and scope resolution resolution rules overriding virtual function P paradigm, programming placement pointer arithmetic assignment to comparison const const conversion declaration null null size of substraction type postfix ++ and -- ++ -- expression precedence of operator predefined address-of operator assignment operator macronames prefix ++ and -- ++ -- preprocessing directive directive, error error directive, null null directive, pragma pragma macro definition macro expansion () syntax summary primary expression private private base class class member procedural programming program environment partitioning start termination protected protected member member access prototypes public public class member pure specifier pure virtual function pure-specifier -pure Q qualified name qualified-class-name -- qualified-name - qualified-type-name -- queue empty quote, single double R range checking recursion recursive decent parser function call reference assignment assignment to call by cast conversion const const declaration initialization operand overloading and volatile volatile register declaration register initialization relational operator relational-expression - reserved identifier resolution ambiguity scoping ambiguity template function overloading resource acquisition exhaustion release re-throw ( ) return return return statement return return type run-time error initialization type information S scope class file function global local of label of local class of macro name of name of nested class resolution operator rules summary separate compilation shift-expression - short type short type specifier short side effects sign extension signed char type signed char type simple-type-name -- Simula size of pointer of string of structure sizeof operator sizeof Smalltalk Smalltalk source file file, inclusion special character specifier auto auto declaration friend friend function inline inline static static storage class template typedef typedef virtual virtual stack unwinding standard component conversion headers include directory libraries statement break break compound continue continue declaration do do empty expression for for goto goto if if summary switch switch () syntax summary while while static type checking static static class member linkage of local object member member declaration member definition member function - specifier static storage class stream closing of file and state string string class concatenation constant type of wide-character struct struct type specifier struct structure initialization of subclass subscripting user-defined summary class declaration syntax compatibility with ANSI C ANSI C compatibility with C declaration syntax declarator syntax exception handling syntax expression syntax macro syntax scope rules statement syntax template syntax support for data abstraction for object-oriented programming - T template class class declaration class definition declaration function function declaration function definition linkage of member function - specifier template syntax summary template-arg -- template-arg-list --- template-argument -- template-argument-list --- template-class-name -- template-declaration -- temporary this this throw throw throw-expression - throwing, exception throw-point token tools design translation phases unit trigraph try try try-block - type user-defined type-specifier - U unary expression minus operator operator operator, user-defined plus, operator unary-expression - unary-operator - uncaught exception undeclared argument underscore character _ unexpected exceptions union anonymous constructor destructor discriminating initialization member function - type specifier union UNIX UNIX unsigned arithmetic char type unsigned char constant type type specifier unsigned V vertical tab \v \v virtual virtual base class destructor function function access function call function, type of specifier virtual user-defined conversion void void argument pointer to void* type void type specifier void volatile volatile member function - volatile operand volatile reference volatile type volatile type specifier volatile W waterfall model white space wide-character string  * *  b1_1_1.cxx #include <stream.hxx> main() { cout << "Hello, world\n"; } b1_1_3.cxx #include <stream.hxx> main () { int inch = 0; cout << "inches="; cin >> inch; cout << inch; cout << "in = "; cout << inch*2.54; cout << " cm\n"; } b1_4_5v.cxx #include <stream.hxx> main() { const float fac = 2.54; float x, in, cm; char ch = 0; for ( int i= 0; i< 8; i++) { cerr << "enter length: "; cin >> x >> ch; if (ch == 'i' ) { // inch in = x; cm = x*fac; } else if (ch == 'c') { // cm in = x/fac; cm = x; } else in = cm = 0; cerr << in << "in = " << cm << " cm\n"; } } b1_5.cxx #include <stream.hxx> extern float pow(float, int); main() { for (int i=0; i<10; i++) cout << pow(2,i) << "\n"; } extern void error(char *); float pow(float x, int n) { if (n < 0) { error ("sorry, negative exponent to pow()"); return 0; } switch (n) { case 0: return 1; case 1: return x; default: return x*pow(x,n-1); } } void error(char *s) { cout << s; } b1__13.cxx #include <stream.hxx> // 1.11 class vector { int *v; int sz; public: vector(int); // constructor ~vector(); // destructor int size() { return sz; } void set_size(int); int& operator[](int); int& elem(int i) { return v[i]; } }; // 1.13 class vec : public vector { int low, high; public: vec(int, int); int& elem(int); int& operator[](int); }; main() { vector a(10); for (int i=0; i<a.size(); i++) { a[i] = i; cout << a[i] << " "; } cout << "\n"; vec b(10,19); for (i=0; i<b.size(); i++) b[i+10] = a[i]; for (i=0; i<b.size(); i++) cout << b[i+10] << " "; cout << "\n"; } extern void exit(int); // 1.13 void error(char* p) { cerr << p << "\n"; exit (1); } // 1.11 vector::vector(int s) { if (s<=0) error("bad vector size"); sz = s; v = new int[s]; } int& vector::operator[](int i) { if (i<0 || sz<=i) error("vector index out of range"); return v[i]; } vector::~vector() { delete v; } // 1.13 int& vec::elem(int i) { return vector::elem(i-low); } vec::vec(int lb, int hb) : (hb-lb+1) { if (hb-lb<0) hb = lb; low = lb; high = hb; } void vector::set_size(int) { /* dummy */ } int& vec::operator[](int i) { if (i<low || high<i) error("vec index out of range"); return elem(i); } b1__14.cxx #include<stream.hxx> extern void exit( int ); extern void error( char* ); // 1.11 class vector { int *v; int sz; public: vector(int); // constructor ~vector(); // destructor int size() { return sz; } void set_size(int); int& operator[](int); int& elem(int i) { return v[i]; } }; vector::vector(int s) { if (s<=0) error("bad vector size"); sz = s; v = new int[s]; } int& vector::operator[](int i) { if (i<0 || sz<=i) error("vector index out of range"); return v[i]; } vector::~vector() { delete v; } // 1.14 class Vec : public vector { public: Vec(int s) : (s) {} Vec(Vec&); ~Vec() {} void operator=(Vec&); void operator*=(Vec&); void operator*=(int); }; Vec::Vec(Vec& a) : (a.size()) { int sz = a.size(); for (int i = 0; i<sz; i++) elem(i) =a.elem(i); } void Vec::operator=(Vec& a) { int s = size(); if (s!=a.size()) error("bad vector size for ="); for (int i =0; i<s; i++) elem(i)=a.elem(i); } Vec operator+(Vec& a, Vec& b) { int s = a.size(); if (s != b.size()) error("bad vector size for +"); Vec sum(s); for (int i=0; i<s; i++) sum.elem(i) = a.elem(i) + b.elem(i); return sum; } void error(char* p) { cerr << p << "\n"; exit (1); } void vector::set_size(int) { } main() { Vec a(10); Vec b(10); for (int i=0; i<a.size(); i++) a[i] = i; b = a; Vec c = a+b; for (i=0; i<c.size(); i++) cout << c[i] << "\n"; } b1__16.cxx #include <vector.hxx> declare(vector,int); implement(vector,int); main() { vector(int) vv(10); vv[2] = 3; vv[10] = 4; // range error } b2_1_3.cxx #include <stream.hxx> int a = 1; void f() { int b = 1; static int c = 1; cout << " a = " << a++ << " b = " << b++ << " c = " << c++ << "\n"; } main () { while (a < 4) f(); } b2_3.cxx #include <stream.hxx> main() { int* p = new int; cout << "sizeof(int) = " << sizeof(int) "\n"; } b2_3_6a.cxx #include <stream.hxx> extern int strlen(char*); char alpha[] = "abcdefghijklmnopqrstuvwxyz"; main () { int sz = strlen(alpha); for (int i=0; i<sz; i++) { char ch = alpha[i]; cout << "'" << chr(ch) << "'" << " = " << ch << " = 0" << oct(ch) << " = 0x" << hex(ch) << "\n"; } } b2_3_6b.cxx #include <stream.hxx> char v[2][5] = { 'a', 'b', 'c', 'd', 'e', '0', '1', '2', '3', '4' }; main() { for ( int i = 0; i<2; i++) { for (int j = 0; j <5; j++) cout << "v[" << i << "][" << j << "]=" << chr(v[i][j]) << " "; cout << "\n"; } } b2_3_7.cxx #include <stream.hxx> main() { char cv[10]; int iv[10]; char* pc = cv; int* pi = iv; cout << "char* " << long(pc+1)-long(pc) << "\n"; cout << "int* " << long(pi+1)-long(pi) << "\n"; } b2_3__10.cxx #include <stream.hxx> struct pair { char* name; int val; }; extern int strlen(char*); extern int strcpy(char*, char*); extern int strcmp(char*, char*); const large = 1024; static pair vec[large]; pair* find(char* p) { for (int i=0; vec[i].name; i++) if (strcmp(p,vec[i].name)==0) return &vec[i]; if (i== large) return &vec[large-1]; return &vec[i]; } int& value(char* p) { pair* res = find(p); if (res->name == 0) { res->name = new char[strlen(p)+1]; strcpy(res->name,p); res->val = 0; } return res->val; } const MAX = 256; main () { char buf [MAX]; while ( cin>>buf) value(buf)++; for (int i=0; vec[i].name; i++) cout << vec[i].name << ":" << vec[i].val << "\n"; } b3_1all.cxx #include <xstream.hxx> #include <ctype.h> enum token_value { NAME, NUMBER, END, PLUS = '+', MINUS = '-', MUL='*', DIV='/', PRINT=';', ASSIGN='=', LP='(', RP=')' }; token_value curr_tok; struct name { char* string; name* next; double value; }; const TBLSZ = 23; name* table[TBLSZ]; int no_of_errors; double error(char* s) { cerr << "error: " << s << "\n"; no_of_errors++; return 1; } extern int strlen(const char*); extern int strcmp(const char*, const char*); extern char* strcpy(char*, const char*); name* look(char* p, int ins = 0) { int ii= 0; char *pp = p; while (*pp) ii = ii<<1 ^ *pp++; if (ii < 0) ii = -ii; ii %= TBLSZ; for (name* n=table [ii]; n; n=n->next) if (strcmp(p,n->string) == 0) return n; if (ins == 0) error("name not found"); name* nn = new name; nn->string = new char[strlen(p) + 1]; strcpy(nn->string,p); nn->value = 1; nn->next = table[ii]; table[ii] = nn; return nn; } inline name* insert(char* s) { return look (s,1); } token_value get_token(); double term(); double expr() { double left = term(); for (;;) switch (curr_tok) { case PLUS: get_token(); left += term(); break; case MINUS: get_token(); left -= term(); break; default : return left; } } double prim(); double term() { double left = prim(); for (;;) switch (curr_tok) { case MUL: get_token(); left *= prim(); break; case DIV: get_token(); double d = prim(); if (d == 0) return error("divide by o"); left /= d; break; default: return left; } } int number_value; char name_string[80]; double prim() { switch (curr_tok) { case NUMBER: get_token(); return number_value; case NAME: if (get_token() == ASSIGN) { name* n = insert(name_string); get_token(); n->value = expr(); return n->value; } return look(name_string)->value; case MINUS: get_token(); return -prim(); case LP: get_token(); double e = expr(); if (curr_tok != RP) return error(") expected"); get_token(); return e; case END: return 1; default: return error ("primary expected"); } } token_value get_token() { char ch = 0; do { if(!cin.get(ch)) return curr_tok = END; } while (ch!='\n' && isspace(ch)); switch (ch) { case ';': case '\n': cin >> WS; return curr_tok=PRINT; case '*': case '/': case '+': case '-': case '(': case ')': case '=': return curr_tok=ch; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': cin.putback(ch); cin >> number_value; return curr_tok=NUMBER; default: if (isalpha(ch)) { char* p = name_string; *p++ = ch; while (cin.get(ch) && isalnum(ch)) *p++ = ch; cin.putback(ch); *p = 0; return curr_tok=NAME; } error ("bad token"); return curr_tok=PRINT; } } int main(int argc, char* argv[]) { switch (argc) { case 1: break; case 2: cin = *new istream(strlen(argv[1]),argv[1]); break; default: error("too many arguments"); return 1; } // insert predefined names: insert("pi")->value = 3.1415926535897932385; insert("e")->value = 2.7182818284590452354; while (1) { get_token(); if( curr_tok == END) break; if (curr_tok == PRINT) continue; cout << expr() << "\n"; } return no_of_errors; } b3_2_6a.cxx extern void strcpy(char *,char *); extern void exit(int); extern int strlen(char *); char *save_string(char* p) { char* s = new char[strlen(p)+1]; strcpy(s,p); return s; } int main (int argc, char* argv[]) { if (argc < 2) exit(1); int size = strlen(argv[1])+1; char* p = save_string (argv[1]); delete[size] p; } b3_2_6b.cxx #include <stream.hxx> extern void exit( int ); void out_of_store() { cout << "operator new failed: out of store\n"; exit(1); } typedef void (*PF)(); extern PF set_new_handler(PF); main() { set_new_handler(&out_of_store); char *p = new char[100000000]; cout << "done, p = " << long(p) << "\n"; } b4_6_8.cxx // This version of the program does not assume sizeof(int)==sizeof(char*) ! #include <stream.hxx> #include <stdarg.hxx> extern void exit(int); void error (int ...); main(int argc, char* argv[]) { switch (argc) { case 1: error(0,argv[0],(char*)0); break; case 2: error(0,argv[0],argv[1],(char*)0); break; default : error(1,"with",dec(argc-1),"arguments",(char*)0); } } void error(int n ...) { va_list ap; va_start(ap,n); for (;;) { char *p = va_arg(ap,char*); if (p == 0) break; cerr << p << " "; } va_end(ap); cerr << "\n"; if (n) exit(n); } b4_6_9.cxx #include <stream.hxx> struct user { char *name; char* id; int dept; }; typedef user* Puser; user heads[] = { "Mcilroy M.D", "doug", 11271, "Aho A.v.", "ava", 11272, "Weinberger P.J.", "pjw", 11273, "Schryer N.L.", "nls", 11274, "Schryer N.L.", "nls", 11275, "Kernighan B.W.", "bwk", 11276 }; typedef int (*CFT)(char*,char*); void sort(char* base, unsigned n, int sz, CFT cmp) { for (int i=0; i<n-1; i++) for (int j=n-1; i<j; j--) { char* pj = base+j*sz; char *pj1 = pj-sz; if ((*cmp)(pj,pj1) <