См. оригинал здесь.
class X { public : int i; X(int j = 2) { i = j; }; void P() { cout << i; }; }; class Y : public X { int i; public : virtual void P() { X::P(); cout << i; }; Y(int j) { i = j+1; }; }; X * a; Y * b; void Q() { a = new X(1); b = new Y(1); b->P(); a = b; a->P(); }
template <class T, int size> class STACK { public : STACK(); ~STACK(); int Push(const T&); int Pop (T&); int Empty(); int Full(); protected : T body[size]; int top; };
int i = 0; class STRING { char * cp; public : STRING (const char * s){ cp = new char [strlen (s) + 1];strcpy (cp, s);}; STRING (STRING& s){ cp = new char [strlen (s.cp) + 1];strcpy (cp, s.cp);}; STRING (const STRING& s1, const STRING& s2){ cp = new char [strlen (s1.cp) + strlen (s2.cp) + 1]; strcat (strcpy (cp, s1.cp), s2.cp); }; ~STRING () { i++; delete [] cp; }; operator char * () { return cp; }; void out () { cout << cp << '\n'; } }; void f(STRING s) { s.out();} void q() { char * p = "string1"; STRING s("string2", "string3"); f (p); f (s); } void Q(){ q(); cout << i; }
E1, E2 : EXCEPTION; PROCEDURE P1 (i : in INTEGER) IS BEGIN IF i = 1 THEN RAISE E1; END IF; IF i = 2 THEN RAISE E2; END IF; PUT («P1-Finish»); EXCEPTION WHEN E2 => PUT («Catch1»); RAISE; WHEN E1 => PUT («Catch2»); END P1; PROCEDURE P2 IS BEGIN P1 (2); PUT («P2-Finish»); EXCEPTION WHEN E1 => PUT («Catch3»); WHEN E2 => PUT («Catch4»); END P2; PROCEDURE P IS E1, E2 : EXCEPTION; PROCEDURE P2 IS BEGIN P1 (2); PUT («Finish-P2»); EXCEPTION WHEN E1 => PUT («Catch5»); WHEN E2 => PUT («Catch6»); END P2; BEGIN P1 (1); P2; P1 (2); PUT («Finish»); EXCEPTION WHEN E1 => PUT («Catch7»); WHEN E2 => PUT («Catch8»); WHEN OTHERS => PUT («Catch9»); END P;