Definition of the C ++ youyuan template class

Source: Internet
Author: User

The concept of youyuan is no longer arrogant and easy to use. There are two forms:
1. Friend meta function: Friend ret-type classname: funname (....);
2. Friend Meta class: Friend class classname;
The only thing to note is that whether it is a friend function or a friend class, you must declare

Youyuan class is actually something to tell. Once the class inherits, The youyuan relationship is still a little complicated. The following is a simple description:

C declares that A is its friend Meta class, so the most basic thing is that a can use the private method or object in C.
As shown in the figure, a is the base class of B, and C is the base class of D. ABCD has the following relationships:
1. The newly added method bcannot access the private member of C.
2. B's method inherited from a can access the private member of C.
3. A can only access the Private Members inherited from C in D. The new private members in D cannot access it!

To sum up:

(1) the relationship between friends and friends cannot be inherited, but the access permissions of existing methods remain unchanged.

(2) If the method of the base class is modified, the access permission is changed.

(3) The relationship between friends and friends is not passed.

If Class B is a friend of Class A, Class C is B's friend, and class C is not necessarily a friend of Class.
 

 

The following example illustrates the definition of a template class.

# Include <iostream>
Using namespace STD;
// Using STD: cout;

Template <typename T>
Class Node
{
Template <typename T> friend class listnode;

// The statement of the youyuan template class, which is written as friend class listnode <t>; some textbooks mentioned that it was not compiled
PRIVATE:
T data;
Node <t> * Next; // The template function variable definition must not play with the parameter list
};

Template <typename T>
Class listnode
{
PRIVATE:
Node <t> * first;
Public:
Listnode () {First = 0 ;}
~ Listnode ();
Listnode & insert (int K, T & X );
Listnode & Delete (T & X );
Bool isempty () const;
Int length () const;
Void display () const;
};

Template <typename T>
Listnode <t> ::~ Listnode ()
{
Node <t> * curr = first-> next;
Node <t> * Prev = first;
While (curr)
{
Delete Prev;
Prev = curr;
Curr = curr-> next;
}
}

Template <typename T>
Bool listnode <t >:: isempty () const
{
Return first = 0
}

Template <typename T>
Listnode <t> & listnode <t>: insert (int K, T & X)
{
Node <t> * curr = first;
Node <t> * temp;
Int Index = 0;
Temp = first;
Temp = new node <t>;
Temp-> DATA = X;
Temp-> next = 0;

If (k <0 | K> length ())
{
Cout <"the input location is illegal" <Endl;
Return * this;
// Exit (1 );
}

If (k = 0 & First = 0)
{
Temp-> next = first;
First = temp;
}
Else
{
For (Index = 0; index <K; index ++)
Curr = curr-> next;

Temp-> next = curr-> next;
Curr-> next = temp;
}

Return * this;

}

Template <typename T>
Int listnode <t >:: length () const
{
Int Index = 0;
Node <t> * curr = first;
While (curr)
{
Index ++;
Curr = curr-> next;
}

Return Index;
}

Template <typename T>
Listnode <t> & listnode <t>: delete (T & X)
{
Node <t> * curr = first-> next;
Node <t> * Prev = first;
While (curr)
{
If (first-> DATA = X)
{
First = first-> next;
Delete Prev;
}
Else
{
If (curr-> DATA = X)
{
Prev-> next = curr-> next;
Delete curr;
}
}

Prev = curr;
Curr = curr-> next;
}

Return * this;
}

Template <typename T>
Void listnode <t>: Display () const
{
Node <t> * curr = first;
While (curr)
{
Cout <curr-> data <"";
Curr = curr-> next;
}
}

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.