{"id":6538,"date":"2021-09-28T12:48:51","date_gmt":"2021-09-28T07:18:51","guid":{"rendered":"https:\/\/www.goseeko.com\/blog\/?p=5741"},"modified":"2021-09-28T12:48:51","modified_gmt":"2021-09-28T07:18:51","slug":"what-are-friend-functions","status":"publish","type":"post","link":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/","title":{"rendered":"What are Friend functions?"},"content":{"rendered":"\n<p>In C++, if a function is declared as a buddy function, the function can access a class&#8217;s protected and private data.<\/p>\n\n\n\n<p>The keyword friend informs the compiler that the given function is a friend function.&nbsp;<\/p>\n\n\n\n<p>A class&#8217;s <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Friend_function\" target=\"_blank\">friend function<\/a> is defined outside of the scope of the class, yet it has access to all private and protected elements of the class. Friends are not member functions, despite the fact that prototypes for friend functions appear in the class specification.<\/p>\n\n\n\n<p>A function, function template, or member function can be a friend, as can a class or class template, in which case the entire class and its members are friends.<\/p>\n\n\n\n<p>For accessing data, a friend function should be declared inside the body of a class, beginning with the term friend.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Declaration of Friend Function<\/strong>s<\/h2>\n\n\n\n<p>class class_name&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>{&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;friend data_type function_name(argument);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ syntax of friend function.&nbsp;&nbsp;<\/p>\n\n\n\n<p>};&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>The friend function prefixes the term friend in the above declaration. Like any other C++ function, the function can be defined anywhere in the program. Neither the keyword friend nor the scope resolution operator are used in the function declaration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Characteristics<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The method isn&#8217;t in the scope of the class it&#8217;s a buddy for.<\/li><li>Secondly, It can&#8217;t be called with the object because it&#8217;s not in that class&#8217;s scope.<\/li><li>It can be called just like any other function without requiring an object.<\/li><li>Next, It can&#8217;t directly access member names, thus it must use an object name and the dot membership operator with the member name.<\/li><li>It can write in a private or public area.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Some&nbsp;important points<\/strong><\/h2>\n\n\n\n<p>Here are some key points to remember regarding friend functions and classes:<\/p>\n\n\n\n<p>1) Friends should only be utilized for specific purposes. When too many functions or external classes are defined as friends of a class with protected or private data, the usefulness of object-oriented programming&#8217;s encapsulation of separate classes is diminished.\u00a0<\/p>\n\n\n\n<p>2) Friendship is not a two-way street. If class A is a friend of class B, B does not immediately become a friend of A.<\/p>\n\n\n\n<p>3) Friendship is not something you inherit.<\/p>\n\n\n\n<p>4) In Java, the concept of friends does not exist.<\/p>\n\n\n\n<p><strong>Interested in learning about similar topics? Here are a few hand-picked blogs for you!<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-31e5070e-29b0-4ecb-99c2-fa8e82c79693\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.goseeko.com\/blog\/what-is-recursion\/\" target=\"_blank\">What is recursion?<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.goseeko.com\/blog\/what-is-a-compiler\/\" target=\"_blank\">Describe compiler?<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.goseeko.com\/blog\/what-is-programming\/\" target=\"_blank\">What is programming?<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.goseeko.com\/blog\/what-is-a-constructor\/\" target=\"_blank\">Explain constructor?<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.goseeko.com\/blog\/what-is-object-oriented-programming\/\" target=\"_blank\">What is object-oriented programming?<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++, if a function is declared as a buddy function, the function can access a class&#8217;s protected and private data.<\/p>\n","protected":false},"author":28,"featured_media":6405,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[36],"tags":[],"class_list":["post-6538","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science-software-engineering-blog"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.3.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What are Friend functions? - Goseeko blog<\/title>\n<meta name=\"description\" content=\"A class&#039;s Friend Functions is defined outside of the scope of the class, yet it has access to all private and protected elements of the class.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What are Friend functions? - Goseeko blog\" \/>\n<meta property=\"og:description\" content=\"A class&#039;s Friend Functions is defined outside of the scope of the class, yet it has access to all private and protected elements of the class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Goseeko blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/goseeko\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-28T07:18:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"533\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Gunjan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@goseeko\" \/>\n<meta name=\"twitter:site\" content=\"@goseeko\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gunjan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/\"},\"author\":{\"name\":\"Gunjan\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/#\/schema\/person\/843a7ac71fc4ae8c177dbb7820f2dc9c\"},\"headline\":\"What are Friend functions?\",\"datePublished\":\"2021-09-28T07:18:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/\"},\"wordCount\":409,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1\",\"articleSection\":[\"Computers\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/\",\"url\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/\",\"name\":\"What are Friend functions? - Goseeko blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1\",\"datePublished\":\"2021-09-28T07:18:51+00:00\",\"description\":\"A class's Friend Functions is defined outside of the scope of the class, yet it has access to all private and protected elements of the class.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1\",\"width\":800,\"height\":533},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.goseeko.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What are Friend functions?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/#website\",\"url\":\"https:\/\/www.goseeko.com\/blog\/\",\"name\":\"Goseeko blog\",\"description\":\"Learning beyond college, Students platform for life skills.\",\"publisher\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.goseeko.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/#organization\",\"name\":\"Goseeko.com\",\"url\":\"https:\/\/www.goseeko.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i1.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/GoSeeko_Stacked-logo-01.png?fit=2471%2C2471&ssl=1\",\"contentUrl\":\"https:\/\/i1.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/GoSeeko_Stacked-logo-01.png?fit=2471%2C2471&ssl=1\",\"width\":2471,\"height\":2471,\"caption\":\"Goseeko.com\"},\"image\":{\"@id\":\"https:\/\/www.goseeko.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/goseeko\",\"https:\/\/x.com\/goseeko\",\"https:\/\/www.instagram.com\/goseeko\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/#\/schema\/person\/843a7ac71fc4ae8c177dbb7820f2dc9c\",\"name\":\"Gunjan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.goseeko.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/603118e8b67d49e1d7409d0214533c962209d94d08d298ce69d8e191ed573faa?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/603118e8b67d49e1d7409d0214533c962209d94d08d298ce69d8e191ed573faa?s=96&d=mm&r=g\",\"caption\":\"Gunjan\"},\"url\":\"https:\/\/www.goseeko.com\/blog\/author\/gunjangoseeko\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What are Friend functions? - Goseeko blog","description":"A class's Friend Functions is defined outside of the scope of the class, yet it has access to all private and protected elements of the class.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/","og_locale":"en_US","og_type":"article","og_title":"What are Friend functions? - Goseeko blog","og_description":"A class's Friend Functions is defined outside of the scope of the class, yet it has access to all private and protected elements of the class.","og_url":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/","og_site_name":"Goseeko blog","article_publisher":"https:\/\/www.facebook.com\/goseeko","article_published_time":"2021-09-28T07:18:51+00:00","og_image":[{"width":800,"height":533,"url":"https:\/\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg","type":"image\/jpeg"}],"author":"Gunjan","twitter_card":"summary_large_image","twitter_creator":"@goseeko","twitter_site":"@goseeko","twitter_misc":{"Written by":"Gunjan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#article","isPartOf":{"@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/"},"author":{"name":"Gunjan","@id":"https:\/\/www.goseeko.com\/blog\/#\/schema\/person\/843a7ac71fc4ae8c177dbb7820f2dc9c"},"headline":"What are Friend functions?","datePublished":"2021-09-28T07:18:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/"},"wordCount":409,"commentCount":0,"publisher":{"@id":"https:\/\/www.goseeko.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1","articleSection":["Computers"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/","url":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/","name":"What are Friend functions? - Goseeko blog","isPartOf":{"@id":"https:\/\/www.goseeko.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1","datePublished":"2021-09-28T07:18:51+00:00","description":"A class's Friend Functions is defined outside of the scope of the class, yet it has access to all private and protected elements of the class.","breadcrumb":{"@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#primaryimage","url":"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1","width":800,"height":533},{"@type":"BreadcrumbList","@id":"https:\/\/www.goseeko.com\/blog\/what-are-friend-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.goseeko.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What are Friend functions?"}]},{"@type":"WebSite","@id":"https:\/\/www.goseeko.com\/blog\/#website","url":"https:\/\/www.goseeko.com\/blog\/","name":"Goseeko blog","description":"Learning beyond college, Students platform for life skills.","publisher":{"@id":"https:\/\/www.goseeko.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.goseeko.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.goseeko.com\/blog\/#organization","name":"Goseeko.com","url":"https:\/\/www.goseeko.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.goseeko.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/i1.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/GoSeeko_Stacked-logo-01.png?fit=2471%2C2471&ssl=1","contentUrl":"https:\/\/i1.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/GoSeeko_Stacked-logo-01.png?fit=2471%2C2471&ssl=1","width":2471,"height":2471,"caption":"Goseeko.com"},"image":{"@id":"https:\/\/www.goseeko.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/goseeko","https:\/\/x.com\/goseeko","https:\/\/www.instagram.com\/goseeko\/"]},{"@type":"Person","@id":"https:\/\/www.goseeko.com\/blog\/#\/schema\/person\/843a7ac71fc4ae8c177dbb7820f2dc9c","name":"Gunjan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.goseeko.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/603118e8b67d49e1d7409d0214533c962209d94d08d298ce69d8e191ed573faa?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/603118e8b67d49e1d7409d0214533c962209d94d08d298ce69d8e191ed573faa?s=96&d=mm&r=g","caption":"Gunjan"},"url":"https:\/\/www.goseeko.com\/blog\/author\/gunjangoseeko\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.goseeko.com\/blog\/wp-content\/uploads\/2021\/09\/photo-1488590528505-98d2b5aba04b.jpeg?fit=800%2C533&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/posts\/6538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/comments?post=6538"}],"version-history":[{"count":0,"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/posts\/6538\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/media\/6405"}],"wp:attachment":[{"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/media?parent=6538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/categories?post=6538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.goseeko.com\/blog\/wp-json\/wp\/v2\/tags?post=6538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}