In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the knowledge points of Youyuan in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn what are the knowledge points of Youyuan in C++.
First, the concept of friends:
1. What is a friend?
Friend is a kind of relationship in C++.
Friend relations occur between functions and classes or between classes.
Friendship is a single item and cannot be passed on.
2. The usage of friends:
Declare friends in a class with the friend keyword
The friend of a class can be another class or a concrete function
Friends are not part of the class.
Friends are not restricted by the access level in the class.
Friends can directly access all members of a specific class
3. The grammar of Youyuan:
Declare a function or class in a class using the friend keyword:
Class Test
{
Double x
Double y
Friend void func (Test& t)
}
Void func (Test& t)
{
}
Note: the func () global function is a friend of the Test class, and func () can access all members of the Test class, but func () is not a member function of Test.
Sample code:
# include
# include
Class Test
{
Double x
Double y
Public:
Test (double xjinghe double y)
{
This- > xerox
This- > yellowy
}
Double getX ()
{
Return x
}
Double getY ()
{
Return y
}
Friend double func (Test& T1 Test& T2)
}
Double func (Test& T1 Test& T2)
{
Double ret = 0
Ret = (t2.y-t1.y) * (t2.y-t1.y) + (t2.x-t1.x) * (t2.x-t1.x)
Ret = sqrt (ret)
Return ret
}
Int main ()
{
Test T1 (1Pol 2)
Test T2 (100.20)
Printf ("T1 (% fjore% f)\ n", t1.getX (), t1.getY ())
Printf ("T2 (% fjore% f)\ n", t2.getX (), t2.getY ())
Printf ("(T1 ~ T2) =% f\ n", func (T1 ~ T2))
Return 0
}
Output result:
Root@txp-virtual-machine:/home/txp/add# gathers + test.cpp
Root@txp-virtual-machine:/home/txp/add#. / a.out
T1 (1.0000000.000000)
T2 (10.000000520.000000)
(T1, T2) = 20.124612
4. The embarrassment of Youyuan:
Friends are born to take into account the efficiency of the c language.
Friends directly destroy the encapsulation of object-oriented.
The efficiency of Youyuan in the actual development of products outweighs the gain.
Youyuan has been gradually abandoned in software engineering.
5. Points for attention:
Friendly relations are not transitive.
The friend of a class can be a member function of another class.
The friend of a class can be a complete class
All member functions are friends
Code testing:
# include
Class ClassC
{
Const char* n
Public:
ClassC (const char* n)
{
This- > n = n
}
Friend class ClassB
}
Class ClassB
{
Const char* n
Public:
ClassB (const char* n)
{
This- > n = n
}
Void getClassCName (ClassC& c)
{
Printf ("c.n =% s\ n", c.n)
}
Friend class ClassA
}
Class ClassA
{
Const char* n
Public:
ClassA (const char* n)
{
This- > n = n
}
Void getClassBName (ClassB& b)
{
Printf ("b.n =% s\ n", b.n)
}
/ *
Void getClassCName (ClassC& c)
{
Printf ("c.n =% s\ n", c.n)
}
, /
}
Int main ()
{
ClassA A ("A")
ClassB B ("B")
ClassC C ("C")
A.getClassBName (B)
B.getClassCName (C)
Return 0
}
Output result:
Root@txp-virtual-machine:/home/txp/add#. / a.out
B. n = B
C. N = C
If you open the masked part of the code above, the compilation will report an error (because the friend is not transitive):
Root@txp-virtual-machine:/home/txp/add# gathers + test.cpp
Test.cpp: In member function 'void ClassA::getClassCName (ClassC&)':
Test.cpp:5:17: error: 'const char* ClassC::n' is private
Const char* n
^
Test.cpp:48:32: error: within this context
Printf ("c.n =% s\ n", c.n)
^
6. Summary:
Friends are born to take into account the efficiency of the c language.
Friends directly destroy the encapsulation of object-oriented.
Friendly relations are not transitive.
The friend of a class can be a member function of another class.
The friend of a class can be a completed class
Thank you for your reading. The above is the content of "what are the knowledge points of Youyuan in C++". After the study of this article, I believe you have a deeper understanding of what are the knowledge points of Youyuan in C++. The specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.