C Friend-Friend

Source: Internet
Author: User

C Friend-Friend

Friend is used to break the limit of protected or private protection, the first thing to do is to be declared in the class of the visitor is a friend function or friend class. The code is as follows

  1. #include <iostream>
  2. using namespace Std;
  3. Class Square{
  4. Private:
  5. int side;
  6. Public:
  7. Square(int a): Side(a){}
  8. Friend class Rectangle ; //declare the Class is friend
  9. };
  10. Class Rectangle {
  11. Private:
  12. int width, height;
  13. Public:
  14.                 rectangle< Span style= "color: #0000cc;" > (int aint B) :width (A) ,height (B{
  15. void Set_values (int a, int b){
  16. Width = a;
  17. Height = b;
  18. }
  19. int Girth();
  20. Friend int area (Rectangle &); //declare the friend function here
  21. int get_width(){return width;}
  22. int get_height(){return height;}
  23. void Conver_from_square(square &s){
  24. Width = Height = s. Side; //access so easily
  25. }
  26. };
  27. //implement The function here , Access easily , too
  28. int area (Rectangle &r){return (r. Width * r. Height); }
  29. int r_area (Rectangle &r{return R.get_width (* R.get_height (;
  30. int Rectangle:: Girth(){return width + width + height + height; }
  31. int main () {
  32. Test_sizeof:
  33. cout << "Sizeof:square" << sizeof(Square)
  34. << ", \trectangle" << sizeof(Rectangle) << "\ n" ;
  35. Test_access:
  36. Rectangle R(2, 3);
  37.         cout < < << area (R<< Span style= "color: #0000cc;" >< <.girth () << endl
  38. cout << "Onather Way:area" << R_area(R) << Endl;
  39. Rectangle R1(2, 3);
  40. Square s(5);
  41. R1. Conver_from_square(S);
  42. cout << "Rectangle convering from Square, girth is" << R1. Girth() < ; < Endl;
  43. return 0;
  44. }
Results
  1. Sizeof:square 4, Rectangle 8
  2. Area:6 girth:10
  3. Onather Way:area 6
  4. Rectangle convering from Square, girth is 20
Look at the function
  • Area
  • R_area
  • Girth
if it is not a friend function or class, the access case, such as the R_area () function, allows the friend function to access the member directly. But there is a difference between the member function and the function girth ()
Look at size, the friend or friend function does not increase the size of the class, just declare it.

C Friend-Friend

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.