home *** CD-ROM | disk | FTP | other *** search
- Listing 1
-
- class Frog {
- public:
- Frog( char *p = "the anonymous frog")
- {
- name = p;
- dist = 0;
- printf( "BEGIN Frog %s\n", name);
- }
-
- ~Frog() { printf( "END Frog %s\n", name); }
-
- char *Name() { return name; }
-
- virtual void jump(int x) { dist += x; }
-
- virtual void croak() { printf( "R-R-R-ibit\n"); }
-
- int distance() { return dist; }
- private:
- char *name;
- int dist;
- };
-
- Listing 2
-
- class Newt : public Frog {
- public:
- Newt( char *n) : (n) {}
-
- void jump( int x)
- {
- printf( "I'm a jumping Newt\n" );
- // invoke base class function against
- // this derived class object
- Frog::jump( x);
- }
- };
-
- class Salamander : public Frog {
- public:
- Salamander( char *n) : (n) {}
-
- void jump( int x)
- {
- printf( "I'm a jumping Salamander\n" );
- Frog::jump( x);
- }
- };
-
- Listing 3
-
- typedef int Truth;
- const int dim = 100;
-
- class Stack[T] { // Stack of T values
- public:
- Stack() { sp = 0; }
-
- void push( T x) { elt[sp++] = x; }
-
- T pop() { return elt[--sp]; }
-
- Truth isempty() { return sp <= 0; }
-
- Truth isfull() { return sp >= dim; }
- private:
- T elt[dim];
- int sp; // sp is index of next slot available to push
- };
-
-