1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Friends

A function that is a friend of class X has access to all of X's private components. It is particularly useful for the I/O operators >>; and <<; which work on istream&; and ostream&; objects and so cannot be member functions of your class. It is also useful for efficient access. Let's revisit our linked list implementation of Istack, and see how simple it becomes:

/////this is Stacks/mydefs2b.h #include <new.h>

//////

class node {
friendclass Istack;
int data;
node* next;
node(int d, node* n);
// constructor };
class Istack {
node* top;
public:
void init(int Nitems = 100)
{ top = NULL; }
void push(int x)
{ top = new node(x, top); }
int pop();
// Not defined here, later
bool isempty()
{ return( top == NULL ); }
bool isfull()
{ return( NULL ); }
// Never Full };

16-Mar-98

Page 14

C201/TAM/AGS