{"id":84572,"date":"2021-09-04T09:00:45","date_gmt":"2021-09-04T03:30:45","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=84572"},"modified":"2021-09-04T09:00:45","modified_gmt":"2021-09-04T03:30:45","slug":"cpp-friend-function","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/","title":{"rendered":"C++ Friend Function"},"content":{"rendered":"<p>The C++ programming language offers various features and functionalities to the programmers. It also supports object-oriented programming which is very useful.<\/p>\n<p>In C++, you have a very cool function which is Friend. The main purpose of the friend function is to help you to access the private and protected members of another class in which it is declared. You can use the keyword \u2018friend\u2019 to declare a friend function.<\/p>\n<h3>Types of Friend Function in C++<\/h3>\n<p>Friend Function is mainly of 2 types:-<\/p>\n<p><strong>1. A global function<\/strong>:- It will allow you to access all the private and protected members of the class.<\/p>\n<p><strong>2. Method of another class<\/strong>:- If you have more than one class and you want to access the non-public data members of a particular class then you can use Friend function.<\/p>\n<h4>Syntax of C++ Friend Function:-<\/h4>\n<p><strong>Declaration of Friend Function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class name_of_the_class\n{\n\nAccess specifiers \/\/ private or protected\n.\n.\nfriend return_type FunctionName(argument(s));\n.\n.\n\n}\n<\/pre>\n<p><strong>Definition of Friend Function in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class name_of_the_class\n{\n\nAccess specifiers \/\/ private or protected\nfriend return_type FunctionName(argument(s));\n}\n\nreturn_type FunctionName(parameter(s))\n{\n\/\/ will help you to access the private and protected data members of the class!\n}\n<\/pre>\n<h3>Implementation of Friend Functions in C++<\/h3>\n<p>In two ways, you can implement a friend function in C++. One is implemented through a method of another class and the second is implemented through global friends.<\/p>\n<p><strong>Implementing through a method of another class<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nclass Tech\n{\nprivate:\nint value;\npublic:\nTech()\n{\nvalue = 15;\n}\nfriend class Exp;\n};\n\nclass Exp\n{\nprivate:\nint value1;\npublic:\nvoid print(Tech&amp; x)\n{\ncout&lt;&lt;\"Base value using the friend class: \" &lt;&lt; x.value&lt;&lt;endl;\n}\n};\n\nint main()\n{\n\ncout&lt;&lt;\"TechVidvan Tutorial: C++ Friend Function!\"&lt;&lt;endl&lt;&lt;endl;\n\nTech value;\nExp value1;\nvalue1.print(value);\nreturn 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: C++ Friend Function!<\/p>\n<p>Base value using the friend class: 15<\/p>\n<\/div>\n<p><strong>Implementing Friend Functions through Global Friends<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nclass Tech\n{\nstring data;\npublic:\nfriend void print( Tech value );\nvoid input( string s1 );\n};\n\nvoid Tech::input( string s1 )\n{\ndata = s1;\n}\n\nvoid print( Tech value )\n{\ncout&lt;&lt;\"A message to you: \"&lt;&lt;value.data&lt;&lt;endl;\n}\n\nint main()\n{\n\ncout&lt;&lt;\"TechVidvan Tutorial: C++ Friend Function!\"&lt;&lt;endl&lt;&lt;endl;\n\nTech value;\n\nvalue.input(\"Good Day\");\nprint( value );\nreturn 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>TechVidvan Tutorial: C++ Friend Function!<\/p>\n<p>A message to you: Good Day<\/p>\n<\/div>\n<h4>Characteristics of Friend Functions in C++<\/h4>\n<ul>\n<li>You cannot invoke a friend function like a normal function without using the object.<\/li>\n<li>You can declare a friend function either in public or in private.<\/li>\n<li>With the friend function, you cannot access the member names directly. To access it, you need to use an object name and dot membership operator.<\/li>\n<li>It cannot be called using the object. Because it is not in the scope of that class.<\/li>\n<\/ul>\n<h4>Why to use friend function in C++<\/h4>\n<p>It is used in special cases like when you want to access the class\u2019s private data directly without using objects of that class. It is also used in operator overloading.<\/p>\n<h3>Summary<\/h3>\n<p>This was all about the Friend function in C++. We talked over the implementation of the friend function in C++. Then we discussed the characteristics of the friend function. We also told you why you should use the friend function in C++.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ programming language offers various features and functionalities to the programmers. It also supports object-oriented programming which is very useful. In C++, you have a very cool function which is Friend. The main&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84595,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4183,4184],"class_list":["post-84572","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-friend-function-in-c","tag-friend-functions-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Friend Function - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about Friend function in C++ &amp; its characteristics. See implementation of friend function in C++ and Why you should use it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Friend Function - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about Friend function in C++ &amp; its characteristics. See implementation of friend function in C++ and Why you should use it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-04T03:30:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Friend-Functions-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Friend Function - TechVidvan","description":"Learn about Friend function in C++ & its characteristics. See implementation of friend function in C++ and Why you should use it.","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:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/","og_locale":"en_US","og_type":"article","og_title":"C++ Friend Function - TechVidvan","og_description":"Learn about Friend function in C++ & its characteristics. See implementation of friend function in C++ and Why you should use it.","og_url":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-09-04T03:30:45+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Friend-Functions-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"C++ Friend Function","datePublished":"2021-09-04T03:30:45+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/"},"wordCount":379,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Friend-Functions-in-C.jpg","keywords":["Friend Function in C++","Friend Functions in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/","url":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/","name":"C++ Friend Function - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Friend-Functions-in-C.jpg","datePublished":"2021-09-04T03:30:45+00:00","description":"Learn about Friend function in C++ & its characteristics. See implementation of friend function in C++ and Why you should use it.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Friend-Functions-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Friend-Functions-in-C.jpg","width":1200,"height":628,"caption":"Friend Function in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-friend-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"C++ Friend Function"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=84572"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84572\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84595"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=84572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=84572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=84572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}