Chapter 2 templates and generic programming (10)

Source: Internet
Author: User

16.4.4 friends declaration of Queue and QueueItem

1. Set the class template to youyuan

Template <class Type>
Class QueueItem
{
QueueItem (const Type & t): item (t), next (0 ){}
Type item;
QueueItem * next;
Friend class Queue <Type>;
};
Template <class Type>
Class QueueItem
{
QueueItem (const Type & t): item (t), next (0 ){}
Type item;
QueueItem * next;
Friend class Queue <Type>;
}; 2. Queue output Operator

Template <class Type>
Ostream & operator <(ostream & OS, const Queue <Type> & q)
{
OS <"<";
QueueItem <Type> * p;
For (p = q. head; p! = 0; p = p-> next)
{
OS <p-> item <"";
}
OS <"> ";
Return OS;
}
Template <class Type>
Ostream & operator <(ostream & OS, const Queue <Type> & q)
{
OS <"<";
QueueItem <Type> * p;
For (p = q. head; p! = 0; p = p-> next)
{
OS <p-> item <"";
}
OS <"> ";
Return OS;
} 3. Set the function template to youyuan

Template <class Type>
Class QueueItem
{
Type item;
QueueItem * next;
QueueItem (const Type & t): item (t), next (0 ){}
Friend class Queue <Type>;
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );
};
Template <class Type>
Class QueueItem
{
Type item;
QueueItem * next;
QueueItem (const Type & t): item (t), next (0 ){}
Friend class Queue <Type>;
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );
}; Template <class Type>
Class Queue
{
Public:
Queue (): head (0), tail (0 ){}
Queue (const Queue & Q): head (0), tail (0)
{Copy_elems (Q );}
Queue & operator = (const Queue &);
~ Queue () {destroy ();}
Type & front ()
{Return head-> item ;}
Const Type & front () const {return head-> item ;}
Void push (const Type &);
Void pop ();
Bool empty () const
{Return head = 0 ;}
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );
Private:
QueueItem <Type> * head;
QueueItem <Type> * tail;
Void destroy ();
Void copy_elems (const Queue &);
};
Template <class Type>
Class Queue
{
Public:
Queue (): head (0), tail (0 ){}
Queue (const Queue & Q): head (0), tail (0)
{Copy_elems (Q );}
Queue & operator = (const Queue &);
~ Queue () {destroy ();}
Type & front ()
{Return head-> item ;}
Const Type & front () const {return head-> item ;}
Void push (const Type &);
Void pop ();
Bool empty () const
{Return head = 0 ;}
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );
Private:
QueueItem <Type> * head;
QueueItem <Type> * tail;
Void destroy ();
Void copy_elems (const Queue &);
}; 4. Type dependency and output Operator

Each type bound to a Queue that uses the Queue output operator must have an output operator.

From xufei96's column
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.