C ++ youyuan getting started

Source: Internet
Author: User

Before explaining what a friend yuan is, Let's explain the disadvantages of the need for friends yuan and friends yuan:

Generally, for common functions, it is impossible to protect members of the category class. To do so, you must make all the members of the class public (shared ), however, the problem is that any external function can access it without any constraints, and c ++ uses the friend modifier, it allows some functions you set to operate on the data protection, avoiding setting all Class Members to public to maximize the security of data members.

Youyuan can enable normal functions to directly protect data from class member functions and avoid frequent calls of class member functions, which can save processor overhead and improve program efficiency. However, even the maximum protection, it also destroys the class encapsulation feature, which isDisadvantages of youyuanWe do not recommend it as the cpu speed is getting faster today, but as a necessary knowledge point and a complete component of c ++, we still need to discuss it.

Declare a common vertex in the class and add the friend modifier to it. Then, this function becomes a friend of the class and can access all the members of the class.

Next let's look at a piece of code to see how we use youyuan to access all the members of the category.


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Internet
{
Public:
Internet (char * name, char * address)
{
Strcpy (Internet: name, name );
Strcpy (Internet: address, address );
}
Friend void ShowN (Internet & obj); // statement of the youyuan Function
Public:
Char name [20];
Char address [20];
};


Void ShowN (Internet & obj) // function definition, which cannot be written. void Internet: ShowN (Internet & obj)
{
Cout <obj. name <endl;
}
Void main ()
{
Internet a ("China Software Development Lab", "www.cndev-lab.com ");
ShowN ();
Cin. get ();
}



The above Code defines the membership name of object a through the membership function. The membership function cannot be regarded as a member function of the class, it is only a common function declared as a class friend, so the definition of the class external function cannot be written as void Internet: ShowN (Internet & obj.

A common function can be a friend function of multiple classes., We modify the above Code and observe the changes:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Country;
Class Internet
{
Public:
Internet (char * name, char * address)
{
Strcpy (Internet: name, name );
Strcpy (Internet: address, address );
}
Friend void ShowN (Internet & obj, Country & cn); // pay attention to this
Public:
Char name [20];
Char address [20];
};

Class Country
{
Public:
Country ()
{
Strcpy (cname, "China ");
}
Friend void ShowN (Internet & obj, Country & cn); // pay attention to this
Protected:
Char cname [30];
};

Void ShowN (Internet & obj, Country & cn)
{
Cout <cn. cname <"|" <obj. name <endl;
}
Void main ()
{
Internet a ("China Software Development Lab", "www.cndev-lab.com ");
Country B;
ShowN (a, B );
Cin. get ();
}

 

A member function of a class can also be a friend of another class,This allows a member function of a class to operate on data members of another class. In the following code, add a Country class. observe the following:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Internet;

Class Country
{
Public:
Country ()
{
Strcpy (cname, "China ");
}
Void Editurl (Internet & temp); // declaration of member functions
Protected:
Char cname [30];
};
Class Internet
{
Public:
Internet (char * name, char * address)
{
Strcpy (Internet: name, name );
Strcpy (Internet: address, address );
}
Friend void Country: Editurl (Internet & temp); // you can declare the function.
Protected:
Char name [20];
Char address [20];
};
Void Country: Editurl (Internet & temp) // external definition of the member function
{
Strcpy (temp. address, "edu.cndev-lab.com ");
Cout <temp. name <"|" <temp. address <endl;
}
Void main ()
{
Internet a ("China Software Development Lab", "www.cndev-lab.com ");
Country B;
B. Editurl ();
Cin. get ();
}

 

 

The entire class can also be a friend of another class.The friend can also be calledFriends. Each member function of a friend class can access all the members of another class.

The sample code is as follows:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;
Class Internet;

Class Country
{
Public:
Country ()
{
Strcpy (cname, "China ");
}
Friend class Internet; // statement of the friend class
Protected:
Char cname [30];
};
Class Internet
{
Public:
Internet (char * name, char * address)
{
Strcpy (Internet: name, name );
Strcpy (Internet: address, address );
}
Void Editcname (Country & temp );
Protected:
Char name [20];
Char address [20];
};
Void Internet: Editcname (Country & temp)
{
Strcpy (temp. cname, "People's Republic of China ");
}
Void main ()
{
Internet a ("China Software Development Lab", "www.cndev-lab.com ");
Country B;
A. Editcname (B );

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.