{"id":231471,"date":"2025-06-11T06:51:19","date_gmt":"2025-06-11T06:51:19","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=231471"},"modified":"2025-06-11T06:51:21","modified_gmt":"2025-06-11T06:51:21","slug":"overload-ostream-insertion-operator-friend-ostream-operator-ostream-stm-frac-const-rhs-stm-rhs-num-rhs-den","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/06\/11\/overload-ostream-insertion-operator-friend-ostream-operator-ostream-stm-frac-const-rhs-stm-rhs-num-rhs-den\/","title":{"rendered":"\/\/ overload ostream insertion operator&lt;&lt; friend ostream &amp;operator&lt;&lt; (ostream &amp;stm, Frac const &amp;rhs) { stm &lt;&lt; rhs.num &lt;&lt; &#8220;\/&#8221; &lt;&lt; rhs.den; }"},"content":{"rendered":"\n<p>\/\/ overload ostream insertion operator&lt;&lt; friend ostream &amp;operator&lt;&lt; (ostream &amp;stm, Frac const &amp;rhs) { stm &lt;&lt; rhs.num &lt;&lt; &#8220;\/&#8221; &lt;&lt; rhs.den; }<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Overloading the Stream Insertion Operator (&lt;&lt;)<\/h3>\n\n\n\n<p>To enable seamless integration of custom data types with C++&#8217;s standard output streams, such as&nbsp;std::cout, the stream insertion operator (&lt;&lt;) must be overloaded. For a class&nbsp;Frac&nbsp;representing a fraction, this allows an object of the class to be printed in an intuitive and readable format, just like a built-in type.<\/p>\n\n\n\n<p>The standard approach is to implement this overload as a non-member function. Since the function needs to access the private members of the&nbsp;Frac&nbsp;class (specifically,&nbsp;num&nbsp;and&nbsp;den), it is declared as a&nbsp;friend&nbsp;inside the class definition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Frac {\n    int num, den;\npublic:\n    \/\/ ... other members and constructors ...\n\n    \/\/ Friend declaration for the stream insertion operator\n    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stm, Frac const&amp; rhs);\n};\n\n\/\/ Function implementation\nstd::ostream&amp; operator&lt;&lt;(std::ostream&amp; stm, Frac const&amp; rhs) {\n    stm &lt;&lt; rhs.num &lt;&lt; \"\/\" &lt;&lt; rhs.den;\n    return stm;\n}<\/code><\/pre>\n\n\n\n<p>content_copydownloadUse code&nbsp;<a href=\"https:\/\/support.google.com\/legal\/answer\/13505487\" target=\"_blank\" rel=\"noreferrer noopener\">with caution<\/a>.C++<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p>This implementation follows a standard, robust pattern for operator overloading. Let&#8217;s break down the function signature:&nbsp;std::ostream&amp; operator&lt;&lt;(std::ostream&amp; stm, Frac const&amp; rhs).<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Return Type (std::ostream&amp;)<\/strong>: The function returns a reference to an\u00a0ostream\u00a0object. This is crucial for enabling chaining of output operations. For instance, in the statement\u00a0std::cout &lt;&lt; f1 &lt;&lt; &#8221; and &#8221; &lt;&lt; f2;, the expression\u00a0std::cout &lt;&lt; f1\u00a0is evaluated first. It calls the overloaded operator, which prints the fraction and then returns the\u00a0std::cout\u00a0stream itself. This returned stream then becomes the left-hand operand for the next\u00a0&lt;&lt;\u00a0operator, allowing the chain to continue.<\/li>\n\n\n\n<li><strong>Function Name (operator&lt;&lt;)<\/strong>: This is the special name C++ uses to signify an overload of the\u00a0&lt;&lt;\u00a0operator.<\/li>\n\n\n\n<li><strong>First Parameter (std::ostream&amp; stm)<\/strong>: This is a reference to the output stream that the object is being sent to (e.g.,\u00a0std::cout). It is passed as a non-const reference because the act of writing to the stream modifies its internal state.<\/li>\n\n\n\n<li><strong>Second Parameter (Frac const&amp; rhs)<\/strong>: This is the object to be printed\u2014the right-hand side of the\u00a0&lt;&lt;\u00a0operator. It is passed as a constant reference (const&amp;) for two key reasons: efficiency (avoiding a costly copy of the object) and safety (guaranteeing that the print operation does not modify the object).<\/li>\n<\/ol>\n\n\n\n<p>The function body simply sends the numerator, a slash character, and the denominator to the stream&nbsp;stm. Finally, returning&nbsp;stm&nbsp;ensures the operator can be used in chained expressions.thumb_upthumb_down<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner5-240.jpeg\" alt=\"\" class=\"wp-image-231472\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ overload ostream insertion operator&lt;&lt; friend ostream &amp;operator&lt;&lt; (ostream &amp;stm, Frac const &amp;rhs) { stm &lt;&lt; rhs.num &lt;&lt; &#8220;\/&#8221; &lt;&lt; rhs.den; } Overloading the Stream Insertion Operator (&lt;&lt;) To enable seamless integration of custom data types with C++&#8217;s standard output streams, such as&nbsp;std::cout, the stream insertion operator (&lt;&lt;) must be overloaded. For a class&nbsp;Frac&nbsp;representing a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-231471","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/231471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=231471"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/231471\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=231471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=231471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=231471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}