home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_07_05
/
v7n5016a.txt
< prev
next >
Wrap
Text File
|
1989-04-09
|
1KB
|
73 lines
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
};