<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C++ &#8211; NoloWiz</title>
	<atom:link href="https://nolowiz.com/category/programming/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>https://nolowiz.com</link>
	<description>Technology news, tips and tutorials</description>
	<lastBuildDate>Thu, 31 Aug 2023 14:36:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.10</generator>

<image>
	<url>https://nolowiz.com/wp-content/uploads/2021/01/cropped-android-chrome-512x512-2-32x32.png</url>
	<title>C++ &#8211; NoloWiz</title>
	<link>https://nolowiz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>CPP Comments</title>
		<link>https://nolowiz.com/cpp-comments/</link>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Thu, 31 Aug 2023 06:11:43 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=5356</guid>

					<description><![CDATA[<p>Comments are an essential part of a good source code. In this tutorial, we will learn about writing comments in C++ code. What is a Comment? Comments in programming are text notes that programmers include in their code to explain its purpose or make it easier to understand. They are not executed by the computer ... <a title="CPP Comments" class="read-more" href="https://nolowiz.com/cpp-comments/" aria-label="More on CPP Comments">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/cpp-comments/">CPP Comments</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Comments are an essential part of a good source code. In this tutorial, we will learn about writing comments in C++ code.</p>



<h2>What is a Comment?</h2>



<p>Comments in programming are text notes that programmers include in their code to explain its purpose or make it easier to understand.</p>



<ul><li>They are not executed by the computer and are intended for other developers.</li><li> Comments improve code readability and help with debugging and collaboration.</li></ul>



<p>There are two types of comments in C++ programming</p>



<ol><li>Single line comment</li><li>Multi-line comment</li></ol>



<p>Let&#8217;s discuss each of these comment types with examples.</p>



<p></p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354"
     crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2735334721002354"
     data-ad-slot="8835878737"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<h2>1. Single-line C++ comments</h2>



<p>Single-line comments in <a href="https://isocpp.org/">C++</a> are denoted by double slashes (<span class="has-inline-color has-vivid-red-color">//</span>). Any text that follows the double slashes on the same line is considered a comment and is ignored by the compiler.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

int main()
{
    // This is a single line comment
    cout&lt;&lt;&quot;Hello!&quot;;  // welcome message
    
    return 0;
}

</pre></div>


<p>In the above code line number 7 is a single-line comment. We can also write code and comment in a single line as shown in line number 8. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
cout&lt;&lt;&quot;Hello!&quot;;  // welcome message
</pre></div>


<p>Let&#8217;s see another example of the factorial of a number.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
// Calculate the factorial of a given number using recursion
int factorial(int n) {
    if (n &lt;= 1) {
        return 1; // Factorial of 0 and 1 is 1
    }
    else {
        return n * factorial(n - 1); // Recursive calculation
    }
}
</pre></div>


<p>Here line number 1,3 and 6 contains single line comment.</p>



<h2>2. Multi-line C++ comments</h2>



<p>In C++, multiline comments are used to add explanatory or descriptive text within the code. They start with <span class="has-inline-color has-vivid-red-color">/*</span> <span style="color:#06784e" class="has-inline-color">and end with</span> <span class="has-inline-color has-vivid-red-color">*/</span>, allowing multiple lines of comments. These comments are ignored by the compiler and are used for documentation purposes.</p>



<p>Here is an example of multi-line comment in C++.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

int main()
{
    /*
    This is a 
    multi line comment 
    used in C++ programminga
    */
    cout&lt;&lt;&quot;Hello!&quot;;
    
    return 0;
}

</pre></div>


<p>Here line number 7 to 11 are multi-line comment.</p>



<p></p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354"
     crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2735334721002354"
     data-ad-slot="8835878737"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<h2>Doxygen Style Comments</h2>



<p><a href="https://www.doxygen.nl/" target="_blank" rel="noreferrer noopener">Doxygen </a>is a documentation generator tool used to generate documentation for software projects. It extracts information from source code and generates various types of documentation, including HTML, PDF, and XML. It helps developers communicate and share information about their code.</p>



<p><strong>Single-line comments</strong></p>



<p>To add a doxygen-style single-line comment in C++, you can use the <span class="has-inline-color has-vivid-red-color">/// </span>syntax before the comment. For example:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/// This is a doxygen-style single line comment.
</pre></div>


<p><strong>Multi-line comments</strong></p>



<p>Multi-line comments comment starts with &#8220;<span class="has-inline-color has-vivid-red-color">/*</span><em>&#8220;, and ends with &#8220;</em><span class="has-inline-color has-vivid-red-color">*/</span>&#8220;. We can write a doxygen-style multi-line comment as shown below :</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
/** * This is a Doxygen-style multi-line comment in C++. 
* It is commonly used to provide documentation for functions, classes, and variables. 
* The comment starts with &quot;/**&quot;, ends with &quot;*/&quot;, and can be used to generate documentation using Doxygen tool. 
*/
</pre></div>


<h2>Commenting Guidelines</h2>



<p>It is best to consider the following points when commenting.</p>



<ul><li><strong>Be concise:</strong> Keep comments brief and to the point.</li><li><strong>Explain why, not what:</strong> Explain why we selected a particular approach.</li><li><strong>Update comments:</strong> As code evolves, remember to update or remove outdated comments that no longer reflect the code&#8217;s functionality.</li><li><strong>Avoid redundant comments:</strong> Don&#8217;t comment on every line. If it is necessary then write the comment.</li><li><strong>Use consistent style:</strong> Follow a consistent commenting style throughout your codebase for a polished look.</li></ul>



<h2>Conclusion</h2>



<p>In conclusion, we have learned to write C++ comments in code. You can read about <a href="https://nolowiz.com/how-to-get-the-type-of-a-variable-in-cpp/" target="_blank" rel="noreferrer noopener">How To Get the Type of a Variable in Cpp</a>.</p>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fcpp-comments%2F&amp;linkname=CPP%20Comments" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fcpp-comments%2F&amp;linkname=CPP%20Comments" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fcpp-comments%2F&amp;linkname=CPP%20Comments" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fcpp-comments%2F&amp;linkname=CPP%20Comments" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fcpp-comments%2F&#038;title=CPP%20Comments" data-a2a-url="https://nolowiz.com/cpp-comments/" data-a2a-title="CPP Comments"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/cpp-comments/">CPP Comments</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How To Get the Type of a Variable in Cpp</title>
		<link>https://nolowiz.com/how-to-get-the-type-of-a-variable-in-cpp/</link>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Thu, 13 Oct 2022 16:05:29 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=3669</guid>

					<description><![CDATA[<p>Sometimes we need to determine the variable type at runtime in C++ code. This tutorial will discuss how to get the type of a variable in C++. The typeid operator allows us to determine the type of variable/object at runtime. The syntax of typeid operator Parameters: Typeid operator accepts one parameter : Type &#8211; Variable ... <a title="How To Get the Type of a Variable in Cpp" class="read-more" href="https://nolowiz.com/how-to-get-the-type-of-a-variable-in-cpp/" aria-label="More on How To Get the Type of a Variable in Cpp">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/how-to-get-the-type-of-a-variable-in-cpp/">How To Get the Type of a Variable in Cpp</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Sometimes we need to determine the variable type at runtime in C++ code. This tutorial will discuss how to get the type of a variable in <a href="https://isocpp.org/" target="_blank" rel="noreferrer noopener">C++</a>.</p>



<p>The <span class="has-inline-color has-vivid-red-color">typeid </span>operator allows us to determine the type of variable/object at runtime.</p>



<h3>The syntax of  typeid operator</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
typeid ( type )
</pre></div>

<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
typeid ( expression )	
</pre></div>


<p><strong>Parameters:</strong></p>



<p>Typeid operator accepts one parameter :</p>



<ul><li>Type &#8211; Variable or object.</li><li>Expression &#8211; Any valid expression.</li></ul>



<p><strong>Returns:</strong></p>



<p>The typeid operator returns an lvalue of type const <span class="has-inline-color has-vivid-red-color">std::type_info</span> that represents the type of expression. </p>



<p>To use the typeid operator we must include the STL header&lt;typeinfo>.</p>



<h3>Examples</h3>



<p> Let&#8217;s check a number variable. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

int main() {

    int a = 20;
    auto b = a * 2 ;
    std::cout &lt;&lt; &quot;b has type -&gt; &quot;&lt;&lt;typeid(b).name()&lt;&lt;&quot;\n&quot;;
    
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>b has type -&gt; i
</code></pre>



<p>Let&#8217;s check a string variable.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;typeinfo&gt;

int main() {

    std::string message = &quot;Hello from NoloWiz&quot;;
    std::cout &lt;&lt; message &lt;&lt;&quot;\n&quot;;
    std::cout &lt;&lt; &quot;message has type -&gt; &quot;&lt;&lt;typeid(message).name()&lt;&lt;&quot;\n&quot;;
    
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Hello from NoloWiz
message has type -&gt; NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE</code></pre>



<p>Let&#8217;s check a float pointer.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

int main() {

    float* percentage = nullptr;
    std::cout &lt;&lt; &quot;percentage has type -&gt; &quot;&lt;&lt;typeid(percentage).name()&lt;&lt;&quot;\n&quot;;
    
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>percentage has type -&gt; Pf</code></pre>



<p><strong>Non-polymorphic object</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

struct Base {}; 
struct Derived : Base {};

int main() {

    Derived drv;
    Base&amp; base = drv;
    std::cout &lt;&lt; &quot;Reference to non-polymorphic base: &quot; &lt;&lt; typeid(base).name() &lt;&lt; '\n';
    
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Reference to non-polymorphic base: 4Base</code></pre>



<p><strong>Polymorphic object</strong></p>



<p>So what about the polymorphic object? let&#8217;s check that.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

struct Base { virtual void foo() {} }; //polymorphic
struct Derived : Base {};

int main() {

    Derived drv;
    Base&amp; base = drv;
    std::cout &lt;&lt; &quot;Reference to polymorphic base: &quot; &lt;&lt; typeid(base).name() &lt;&lt; '\n';
    
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Reference to polymorphic base: 7Derived</code></pre>



<h3>Expression as parameter</h3>



<p>Let&#8217;s pass an expression as a parameter to typeid.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

int main() {

    int abc = 42;
    
    const std::type_info&amp; typeInfo = typeid((10 + abc) / 2.0);
    std::cout &lt;&lt;&quot;Type of expression (10 + abc) / 2.0 : &quot;&lt;&lt;typeInfo.name();
    
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
Type of expression (10 + abc) / 2.0 : d
</pre></div><p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fhow-to-get-the-type-of-a-variable-in-cpp%2F&amp;linkname=How%20To%20Get%20the%20Type%20of%20a%20Variable%20in%20Cpp" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fhow-to-get-the-type-of-a-variable-in-cpp%2F&amp;linkname=How%20To%20Get%20the%20Type%20of%20a%20Variable%20in%20Cpp" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fhow-to-get-the-type-of-a-variable-in-cpp%2F&amp;linkname=How%20To%20Get%20the%20Type%20of%20a%20Variable%20in%20Cpp" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fhow-to-get-the-type-of-a-variable-in-cpp%2F&amp;linkname=How%20To%20Get%20the%20Type%20of%20a%20Variable%20in%20Cpp" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fhow-to-get-the-type-of-a-variable-in-cpp%2F&#038;title=How%20To%20Get%20the%20Type%20of%20a%20Variable%20in%20Cpp" data-a2a-url="https://nolowiz.com/how-to-get-the-type-of-a-variable-in-cpp/" data-a2a-title="How To Get the Type of a Variable in Cpp"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/how-to-get-the-type-of-a-variable-in-cpp/">How To Get the Type of a Variable in Cpp</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Cppfront &#8211; A new Syntax for CPP</title>
		<link>https://nolowiz.com/cppfront-a-new-syntax-for-cpp/</link>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Sat, 08 Oct 2022 04:43:54 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=3594</guid>

					<description><![CDATA[<p>C++ is a good programming language, it is used in performance-critical areas like gaming, multimedia processing, etc but some parts of CPP language syntax are still old-style (cpp1). Cppfront is an experimental compiler for C++ syntax2 (cpp2) by &#160;Herb Sutter. My goal is to explore whether there&#8217;s a way we can evolve C++ itself to ... <a title="Cppfront &#8211; A new Syntax for CPP" class="read-more" href="https://nolowiz.com/cppfront-a-new-syntax-for-cpp/" aria-label="More on Cppfront &#8211; A new Syntax for CPP">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/cppfront-a-new-syntax-for-cpp/">Cppfront &#8211; A new Syntax for CPP</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>C++ is a good programming language, it is used in performance-critical areas like gaming, multimedia processing, etc but some parts of CPP language syntax are still old-style (cpp1). Cppfront is an experimental compiler for C++ syntax2 (cpp2) by &nbsp;<a href="https://github.com/hsutter" target="_blank" rel="noreferrer noopener">Herb Sutter</a>. </p>



<blockquote class="wp-block-quote"><p>My goal is to explore whether there&#8217;s a way we can evolve C++ itself to become 10x simpler, safer, and more toolable. If we had an alternate C++ syntax, it would give us a &#8220;bubble of new code that doesn&#8217;t exist today&#8221; where we could make arbitrary improvements (e.g., change defaults, remove unsafe parts, make the language context-free and order-independent, and generally apply 30 years&#8217; worth of learnings), free of backward source compatibility constraints.</p><cite>-Herb Sutter</cite></blockquote>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354" crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-2735334721002354" data-ad-slot="8835878737" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<p>In Cppfront we have two options :</p>



<ul><li><em>Write mixed Cpp1/Cpp2 in the same source file</em>&nbsp;with perfect backward source compatibility via&nbsp;<code>#include</code>&nbsp;or&nbsp;<code>import</code></li><li><em>Write only Cpp2 in a particular source file</em>&nbsp;and program in a 10x simpler C++, where code is type-safe and memory-safe by construction, keeps perfect backward link compatibility via&nbsp;<code>import</code>, and in the future (if Cppfront succeeds) with faster compilers and better tools tuned for the simpler language.</li></ul>



<p>Herb Sutter suggests it would be nice to have the following features in C++ </p>



<ul id="block-798bef78-a68c-4675-aac2-820cf134aca5"><li>less complexity to remember;</li><li>with fewer safety gotchas; and</li><li>the same level of tool support that other languages enjoy.</li></ul>



<p>CPP2 syntax focus on the following areas:</p>



<ul><li>fix defaults (e.g., make [[nodiscard]] the default);</li><li>double down on modern C++ (e.g., make C++20 modules and C++23 import std; the default);</li><li>remove unsafe parts (e.g., remove union and pointer arithmetic);</li><li>have type and memory safety by default (e.g., make the C++ Core Guidelines safety profiles the default and required);</li><li>eliminate 90% of the guidance we have to teach about today&#8217;s complex language;</li><li>make it easy to write a parser (e.g., have context-free grammar); and</li><li>make it easy to write refactoring and other tools (e.g., have order-independent semantics)</li></ul>



<p>Let&#8217;s try Cppfront.</p>



<h2>Build Cppfront compiler</h2>



<p>I used Ubuntu 22.04 first we need to install g++-10 </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
sudo apt-get update
sudo apt install g++-10
</pre></div>


<p>Next clone the Cppfront repository.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
git clone https://github.com/hsutter/cppfront.git
</pre></div>


<p>Enter into the &#8220;sources&#8221; directory and run the following command to build Cppfront:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
g++-10 cppfront.cpp -std=c++20 -o cppfront
</pre></div>


<p>It will build the Cppfront executable.</p>



<h2>Write some CPP2 code, build and run</h2>



<p>Let&#8217;s write a hello world program using CPP2 syntax.</p>



<p>Open your favorite text editor and write the following code :</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
main: () -&gt; int = {
 std::cout &lt;&lt; &quot;Hello world CPP2\n&quot;; 
}
</pre></div>


<p>Save it as helloworld.cpp2, then run:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
cppfront helloworld.cpp2
</pre></div>


<figure class="wp-block-image size-full"><img loading="lazy" width="585" height="68" src="https://nolowiz.com/wp-content/uploads/2022/10/cppfront-compile.png" alt="" class="wp-image-3608" srcset="https://nolowiz.com/wp-content/uploads/2022/10/cppfront-compile.png 585w, https://nolowiz.com/wp-content/uploads/2022/10/cppfront-compile-300x35.png 300w" sizes="(max-width: 585px) 100vw, 585px" /></figure>



<p>It will produce a helloworld.cpp file now we can compile it using the following command(Please note that we need to include cpp2util.h&#8217;s path ):</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
g++-10 helloworld.cpp -std=c++20 -I/home/rupesh/cppfront/include -o helloworldapp
</pre></div>


<p>Now run ./helloworld it will print &#8220;Hello world CPP2&#8221;.</p>



<figure class="wp-block-image size-full"><img loading="lazy" width="473" height="43" src="https://nolowiz.com/wp-content/uploads/2022/10/cppfront_output.png" alt="Hello world cpp2 output" class="wp-image-3613" srcset="https://nolowiz.com/wp-content/uploads/2022/10/cppfront_output.png 473w, https://nolowiz.com/wp-content/uploads/2022/10/cppfront_output-300x27.png 300w" sizes="(max-width: 473px) 100vw, 473px" /></figure>



<p></p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354" crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-2735334721002354" data-ad-slot="8835878737" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<p>One more example in pure cpp2</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
main: () -&gt; int = {
    std::cout &lt;&lt; &quot;Hello &quot; &lt;&lt; name() &lt;&lt; &quot;\n&quot;;
}

name: () -&gt; std::string = {
    s: std::string = &quot;NoloWiz&quot;;
    decorate(s);
    return s;
}

decorate: (inout s: std::string) = {
    s = &quot;&#91;&quot; + s + &quot;]&quot;;
}
</pre></div>


<p>Compile and run :</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
./cppfront greetings.cpp2 -p
g++-10 greetings.cpp -std=c++20 -I/home/rupesh/cppfront/include -o greetings
./greetings
</pre></div>


<figure class="wp-block-image size-full"><img loading="lazy" width="431" height="61" src="https://nolowiz.com/wp-content/uploads/2022/10/cpp2_output.png" alt="CPP 2 output" class="wp-image-3618" srcset="https://nolowiz.com/wp-content/uploads/2022/10/cpp2_output.png 431w, https://nolowiz.com/wp-content/uploads/2022/10/cpp2_output-300x42.png 300w" sizes="(max-width: 431px) 100vw, 431px" /></figure>



<p>More examples are available <a href="https://github.com/hsutter/cppfront/tree/main/regression-tests" target="_blank" rel="noreferrer noopener">here</a>.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Can C++ be 10x Simpler &amp; Safer?  - Herb Sutter - CppCon 2022" width="900" height="506" src="https://www.youtube.com/embed/ELeZAKCN4tY?start=412&#038;feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fcppfront-a-new-syntax-for-cpp%2F&amp;linkname=Cppfront%20%E2%80%93%20A%20new%20Syntax%20for%20CPP" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fcppfront-a-new-syntax-for-cpp%2F&amp;linkname=Cppfront%20%E2%80%93%20A%20new%20Syntax%20for%20CPP" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fcppfront-a-new-syntax-for-cpp%2F&amp;linkname=Cppfront%20%E2%80%93%20A%20new%20Syntax%20for%20CPP" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fcppfront-a-new-syntax-for-cpp%2F&amp;linkname=Cppfront%20%E2%80%93%20A%20new%20Syntax%20for%20CPP" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fcppfront-a-new-syntax-for-cpp%2F&#038;title=Cppfront%20%E2%80%93%20A%20new%20Syntax%20for%20CPP" data-a2a-url="https://nolowiz.com/cppfront-a-new-syntax-for-cpp/" data-a2a-title="Cppfront – A new Syntax for CPP"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/cppfront-a-new-syntax-for-cpp/">Cppfront &#8211; A new Syntax for CPP</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Split Strings in C++</title>
		<link>https://nolowiz.com/split-strings-in-cpp/</link>
					<comments>https://nolowiz.com/split-strings-in-cpp/#respond</comments>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Sun, 19 Dec 2021 15:53:36 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=2150</guid>

					<description><![CDATA[<p>Splitting string into sub-strings is a common task in many applications. In this article, we will discuss various ways to split strings in C++. 1.Using the getline function The getline function can be used to split strings in C++. is : Extact string from istream object str : extracted line string delim : delimiter character ... <a title="Split Strings in C++" class="read-more" href="https://nolowiz.com/split-strings-in-cpp/" aria-label="More on Split Strings in C++">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/split-strings-in-cpp/">Split Strings in C++</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Splitting string into sub-strings is a common task in many applications. In this article, we will discuss various ways to split strings in C++.</p>



<h2>1.Using the getline function</h2>



<p>The <span class="has-inline-color has-vivid-red-color">getline </span>function can be used to split strings in C++. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
istream&amp; getline (istream&amp;  is, string&amp; str, char delim);
</pre></div>


<ul><li>is : Extact string from istream object </li><li>str : extracted line string</li><li>delim : delimiter character</li></ul>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;

using namespace std;

void splitstring(const string &amp;str, 
                 char delimiter,
                 vector&lt;string&gt;&amp; tokens)
{
    stringstream ss(str); //Create stringstream object
    string word;// to store the token (splitted string)

    while(getline(ss,word,delimiter).good())
    {
        tokens.push_back(word);
    }
    if (tokens.size()&gt;0)
       tokens.push_back(word); // insert last token
}

int main()
{

    string str=&quot;car,bike,bus,bicycle&quot;;
    vector&lt;string&gt; vec;
    splitstring(str,',',vec);

    for(auto s:vec)
    {
        cout&lt;&lt;s&lt;&lt;&quot;\n&quot;;
    }
    return 0;
}

</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>car
bike
bus
bicycle</code></pre>



<p>To split the string, we will extract individual words/tokens in the string using getline function and inserts extracted tokens into a vector.</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354"
     crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2735334721002354"
     data-ad-slot="8835878737"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<h2>2.Using the std::regex_token_iterator function</h2>



<p>We can use <span class="has-inline-color has-vivid-red-color">std::regex_token_iterator</span> function to split string in C++11.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;regex&gt;

using namespace std;

void splitstring(string &amp;str,
                 regex re,
                 vector&lt;string&gt;&amp; tokens)
{
    string word;// to store the token (splitted string)
    regex_token_iterator&lt;string::iterator&gt;  it( str.begin(), str.end(), re, -1 );
    regex_token_iterator&lt;string::iterator&gt; rend;

    while( it!=rend )
    {
        word = *it++;
        if (word !=  str)
            tokens.push_back(word);
    }
}

int main()
{

    string str=&quot;car,bike,bus,bicycle&quot;;
    std::regex regex(&quot;\\,&quot;);
    
    vector&lt;string&gt; tokens;
    splitstring(str,regex,tokens);

    for(const auto v :tokens)
    {
        cout&lt;&lt;v&lt;&lt;&quot;\n&quot;;
    }

    return 0;
}

</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>car
bike
bus
bicycle</code></pre>



<h2>3.Using the std::strtok function</h2>



<p>A sequence of calls to <span class="has-inline-color has-vivid-red-color">std::strtok</span> function will give us tokens. Please note that this function uses C strings. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;stdio.h&gt;
using namespace std;

void splitstring(const string &amp;str,
                 const char *delimiter,
                 vector&lt;string&gt;&amp; tokens)
{
    string word;// to store the token (splitted string)

    char* ptoken = strtok(const_cast&lt;char*&gt;(str.c_str()),delimiter);
    while(ptoken!=nullptr)
    {
       word = string(ptoken);
       if (word != str)
           tokens.push_back(word);

       ptoken = strtok(nullptr,delimiter);
    }
}

int main()
{

    string str=&quot;car,bike,bus,bicycle&quot;;
    
    vector&lt;string&gt; tokens;
    splitstring(str,&quot;,&quot;,tokens);

    for(const auto v :tokens)
    {
        cout&lt;&lt;v&lt;&lt;&quot;\n&quot;;
    }

    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>car
bike
bus
bicycle</code></pre>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354"
     crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2735334721002354"
     data-ad-slot="8835878737"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<h2>4.Using the std::string::find function</h2>



<p>In this method, we will use the <span class="has-inline-color has-vivid-red-color">string::find</span> function to search the delimiter. Find function will return the position of the separator and we can create tokens from it. We will use <span class="has-inline-color has-vivid-red-color">string::find</span> and <span class="has-inline-color has-vivid-red-color">string::substr</span> for tokenization. Here <a href="https://nolowiz.com/what-is-stringnpos-in-cpp/" target="_blank" rel="noreferrer noopener">string::npos</a> is used as an error indicator. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;

void splitstring(const string &amp;str,
                 char delimiter,
                 vector&lt;string&gt; &amp;tokens)
{
    string word; // to store the token (splitted string)

    size_t offset = 0, count;

    size_t pos = str.find(delimiter);
    while (pos != string::npos)
    {
        count = pos - offset;
        word = str.substr(offset, count);
        tokens.push_back(word);

        offset = pos + 1;
        pos = str.find(delimiter, pos + 1);
    }

    if (tokens.size() &gt; 0)
    {
        word = str.substr(offset);
        tokens.push_back(word);
    }
}
int main()
{

    string str = &quot;car,bike,bus,bicycle&quot;;
    vector&lt;string&gt; tokens;
    splitstring(str, ',', tokens);

    for (auto v : tokens)
    {
        cout &lt;&lt; v &lt;&lt; &quot;\n&quot;;
    }

    return 0;
}
</pre></div>


<h2>5.Split string in C++ using boost::split</h2>



<p>In this method, we will use <a href="https://www.boost.org/" target="_blank" rel="noreferrer noopener">boost </a>library for string splitting. we will use the <span class="has-inline-color has-vivid-red-color">boost::split</span> function to split the string.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;boost/algorithm/string.hpp&gt;

using namespace std;
 
int main() {
	string str(&quot;car,bike,bus,bicycle&quot;);

    vector&lt;string&gt; tokens;
    boost::split(tokens, str, boost::is_any_of(&quot;,&quot;));
 
    for(auto v: tokens )
       cout&lt;&lt; v &lt;&lt;&quot;\n&quot;;

	return 0;
}
</pre></div><p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fsplit-strings-in-cpp%2F&amp;linkname=Split%20Strings%20in%20C%2B%2B" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fsplit-strings-in-cpp%2F&amp;linkname=Split%20Strings%20in%20C%2B%2B" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fsplit-strings-in-cpp%2F&amp;linkname=Split%20Strings%20in%20C%2B%2B" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fsplit-strings-in-cpp%2F&amp;linkname=Split%20Strings%20in%20C%2B%2B" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fsplit-strings-in-cpp%2F&#038;title=Split%20Strings%20in%20C%2B%2B" data-a2a-url="https://nolowiz.com/split-strings-in-cpp/" data-a2a-title="Split Strings in C++"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/split-strings-in-cpp/">Split Strings in C++</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nolowiz.com/split-strings-in-cpp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>What is string::npos in C++?</title>
		<link>https://nolowiz.com/what-is-stringnpos-in-cpp/</link>
					<comments>https://nolowiz.com/what-is-stringnpos-in-cpp/#respond</comments>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Sun, 19 Dec 2021 07:43:54 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=2175</guid>

					<description><![CDATA[<p>In this article, we will discuss string::npos in C++. The std::string::npos is a static member constant. It is defined in the string (basic_string.h) header file. It is a static member constant with the maximum value representable by the type size_type. The type of size_type is unsigned integer however it is initialized as -1 but due ... <a title="What is string::npos in C++?" class="read-more" href="https://nolowiz.com/what-is-stringnpos-in-cpp/" aria-label="More on What is string::npos in C++?">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/what-is-stringnpos-in-cpp/">What is string::npos in C++?</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will discuss <span class="has-inline-color has-vivid-red-color">string::npos</span> in <a href="https://isocpp.org/" target="_blank" rel="noreferrer noopener">C++</a>. </p>



<p>The <span class="has-inline-color has-vivid-red-color">std::string::npos</span> is a static member constant. It is defined in the string (basic_string.h) header file. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
///  Value returned by various member functions when they fail.
static const size_type	npos = static_cast&lt;size_type&gt;(-1);
</pre></div>


<p>It is a static member constant with the maximum value representable by the type size_type. The type of size_type is unsigned integer however it is initialized as -1 but due to signed to unsigned implicit conversion, it will hold the largest positive value. This is a portable way to specify the maximum value of any unsigned type.</p>



<p>Let&#8217;s print the value of string::npos.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
#include &lt;iostream&gt;
using namespace std;

int main() {
	cout&lt;&lt;string::npos&lt;&lt;endl;
	return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>18446744073709551615</code></pre>



<p>Here  18446744073709551615  is the maximum value of unsigned 64-bit number<sup> </sup>2<sup>64 </sup> -1 (on a 64-bit platform).</p>



<p>However,the <span class="has-inline-color has-vivid-red-color">string::npos</span> generally used in the following cases</p>



<ul><li>end of string indicator by functions that expects a string index.</li><li>error indicator for functions that returns a string index.</li></ul>



<p>Let&#8217;s look into these two cases with examples.</p>



<h4>Using it as end of string indicator</h4>



<p>Firstly we will look into npos as the end of string indicator.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
using namespace std;
 
int main() {
    string str = &quot;hello&quot;;
    string s2(str,3,string::npos); // npos to indicate end of the string
     
    cout&lt;&lt;s2&lt;&lt;&quot;\n&quot;;
     
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>lo</code></pre>



<h4>Using it as error indicator </h4>



<p>In this case, the string::npos is used as the return value from the string find function. If no match found string <span class="has-inline-color has-vivid-red-color">find</span> function will return string::npos.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
using namespace std;

int main() {
    string str = &quot;hello&quot;;
    
    if(str.find('w') == string::npos)
        cout &lt;&lt; &quot;w not found\n&quot;;
        
	return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>w not found</code></pre>



<h2>Conclusion</h2>



<p>In conclusion, the exact meaning of string::npos depends on the context. Hence, we can summarize as</p>



<ul><li>it is maximum value representable by size_type</li><li>End of string indicator</li><li>Error indicator </li></ul>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fwhat-is-stringnpos-in-cpp%2F&amp;linkname=What%20is%20string%3A%3Anpos%20in%20C%2B%2B%3F" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fwhat-is-stringnpos-in-cpp%2F&amp;linkname=What%20is%20string%3A%3Anpos%20in%20C%2B%2B%3F" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fwhat-is-stringnpos-in-cpp%2F&amp;linkname=What%20is%20string%3A%3Anpos%20in%20C%2B%2B%3F" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fwhat-is-stringnpos-in-cpp%2F&amp;linkname=What%20is%20string%3A%3Anpos%20in%20C%2B%2B%3F" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fwhat-is-stringnpos-in-cpp%2F&#038;title=What%20is%20string%3A%3Anpos%20in%20C%2B%2B%3F" data-a2a-url="https://nolowiz.com/what-is-stringnpos-in-cpp/" data-a2a-title="What is string::npos in C++?"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/what-is-stringnpos-in-cpp/">What is string::npos in C++?</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nolowiz.com/what-is-stringnpos-in-cpp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>All About Pure Virtual Functions in C++</title>
		<link>https://nolowiz.com/all-about-pure-virtual-functions-in-cpp/</link>
					<comments>https://nolowiz.com/all-about-pure-virtual-functions-in-cpp/#respond</comments>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Tue, 14 Dec 2021 15:23:19 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=2113</guid>

					<description><![CDATA[<p>In this article, we will discuss pure virtual functions in C++ and usage with examples. Let&#8217;s dive into the definition of pure virtual function. So what is pure virtual function? A pure virtual function is a function, that must be overriden in the derived class need not to be defined A virtual function can be ... <a title="All About Pure Virtual Functions in C++" class="read-more" href="https://nolowiz.com/all-about-pure-virtual-functions-in-cpp/" aria-label="More on All About Pure Virtual Functions in C++">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/all-about-pure-virtual-functions-in-cpp/">All About Pure Virtual Functions in C++</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will discuss pure virtual functions in <a href="https://isocpp.org/" target="_blank" rel="noreferrer noopener">C++</a> and usage with examples. Let&#8217;s dive into the definition of pure virtual function. </p>



<h2>So what is pure virtual function?</h2>



<p>A pure virtual function is a function,</p>



<ul><li>that must be overriden in the derived class </li><li>need not to be defined</li></ul>



<p>A virtual function can be declared &#8220;pure&#8221; using the =0 (pseudo initializer).</p>



<p>Example of pure virtual function declaration</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
class Base{
   public:
    virtual void f3() =0; //pure virtual function
};

</pre></div>


<h2>Abstract class and pure virtual function</h2>



<p>An abstract class has at least one pure virtual function. We cannot instantiate(create a new object) of an abstract class. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
class Base{
   public:
    void f1(); // function
    virtual void f2(); //virtual function
    virtual void f3() =0; //pure virtual function
};

Base b; //  error : pure virtual function f3 not overriden
</pre></div>


<p>Here &#8220;Base&#8221; class is an abstract class because it has a pure virtual function f3. The derived class must override the pure virtual function of the base class.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
 class Derived : public Base {
        // no f1: fine
        // no f2: fine, we inherit Base::f2
        void f3();// Overriden pure virtual function
    };
 Derived d; //Ok
</pre></div>


<p>Here f3 is overridden hence we can create an object of Derived.</p>



<h2>What happens if we don&#8217;t override pure virtual function?</h2>



<p>If we don&#8217;t override pure virtual function in a derived class it will become an abstract class.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
class D2 : public Base {
        // no f1: fine
        // no f2: fine, we inherit Base::f2
        // no f3: fine, but D2 is therefore still abstract
    };

 D2 d;   // error: pure virtual Base::f3 not overridden
</pre></div>


<p>Here we cannot create an instance of D2 because D2 didn&#8217;t override the pure virtual function so it becomes an abstract class.</p>



<h2>Conclusion</h2>



<p>In conclusion, we can summarize the following points</p>



<ul><li>To declare pure virtual function use the &#8220;pseudo initializer&#8221; <span class="has-inline-color has-vivid-red-color">=0</span>.</li><li>A class with one or more pure virtual functions is an abstract class.</li><li>Pure virtual functions must be overriden by derived class.</li><li>If we dont override pure virtual function in derived class it will become an abstract class.</li></ul>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fall-about-pure-virtual-functions-in-cpp%2F&amp;linkname=All%20About%20Pure%20Virtual%20Functions%20in%20C%2B%2B" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fall-about-pure-virtual-functions-in-cpp%2F&amp;linkname=All%20About%20Pure%20Virtual%20Functions%20in%20C%2B%2B" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fall-about-pure-virtual-functions-in-cpp%2F&amp;linkname=All%20About%20Pure%20Virtual%20Functions%20in%20C%2B%2B" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fall-about-pure-virtual-functions-in-cpp%2F&amp;linkname=All%20About%20Pure%20Virtual%20Functions%20in%20C%2B%2B" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fall-about-pure-virtual-functions-in-cpp%2F&#038;title=All%20About%20Pure%20Virtual%20Functions%20in%20C%2B%2B" data-a2a-url="https://nolowiz.com/all-about-pure-virtual-functions-in-cpp/" data-a2a-title="All About Pure Virtual Functions in C++"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/all-about-pure-virtual-functions-in-cpp/">All About Pure Virtual Functions in C++</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nolowiz.com/all-about-pure-virtual-functions-in-cpp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Reading String From Input in C++</title>
		<link>https://nolowiz.com/reading-string-from-input-in-cpp/</link>
					<comments>https://nolowiz.com/reading-string-from-input-in-cpp/#respond</comments>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Sun, 12 Dec 2021 14:57:20 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=2095</guid>

					<description><![CDATA[<p>In this article, we will discuss different ways to read strings from input in C++. The cin object and getline function can be used to read strings. Using the cin to read string input In this example, we will use cin object to read strings. The cin is an istream object. Output Note that there ... <a title="Reading String From Input in C++" class="read-more" href="https://nolowiz.com/reading-string-from-input-in-cpp/" aria-label="More on Reading String From Input in C++">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/reading-string-from-input-in-cpp/">Reading String From Input in C++</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will discuss different ways to read strings from input in C++. The cin object and getline function can be used to read strings.</p>



<h2>Using the cin to read string input</h2>



<p>In this example, we will use cin object to read strings. The cin is an <a href="https://www.cplusplus.com/reference/istream/istream/" target="_blank" rel="noreferrer noopener">istream</a> object.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

int main()
{
    cout &lt;&lt; &quot;Please enter your name: &quot;;

    string name;
    cin&gt;&gt; name;

    cout &lt;&lt; &quot;Hello &quot; &lt;&lt; name &lt;&lt;&quot;!&quot;&lt;&lt;endl;

    return 0;
}

</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Please enter your name: Tom
Hello Tom!</code></pre>



<p>Note that there is no explicit memory management and no fixed-size buffer to overflow.</p>



<h2>Using the getline to read string</h2>



<p>Let&#8217;s use getline function to read string input.</p>



<pre class="wp-block-code"><code>//Get line from stream into string
istream&amp; getline (istream&amp; is, string&amp; str);</code></pre>



<p><strong> is </strong>: the istream object from which characters are extracted.</p>



<p><strong> str</strong> : string object where the extracted line is stored.</p>



<p>Let&#8217;s look into an example.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

int main()
{
    cout &lt;&lt; &quot;Please enter your name: &quot;;

    string name;
    getline(cin,name);

    cout &lt;&lt; &quot;Hello &quot; &lt;&lt; name &lt;&lt;&quot;!&quot;&lt;&lt;endl;

    return 0;
}

</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Please enter your name: Bob
Hello Bob!</code></pre>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Freading-string-from-input-in-cpp%2F&amp;linkname=Reading%20String%20From%20Input%20in%20C%2B%2B" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Freading-string-from-input-in-cpp%2F&amp;linkname=Reading%20String%20From%20Input%20in%20C%2B%2B" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Freading-string-from-input-in-cpp%2F&amp;linkname=Reading%20String%20From%20Input%20in%20C%2B%2B" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Freading-string-from-input-in-cpp%2F&amp;linkname=Reading%20String%20From%20Input%20in%20C%2B%2B" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Freading-string-from-input-in-cpp%2F&#038;title=Reading%20String%20From%20Input%20in%20C%2B%2B" data-a2a-url="https://nolowiz.com/reading-string-from-input-in-cpp/" data-a2a-title="Reading String From Input in C++"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/reading-string-from-input-in-cpp/">Reading String From Input in C++</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nolowiz.com/reading-string-from-input-in-cpp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Virtual Destructors in CPP</title>
		<link>https://nolowiz.com/virtual-destructors-in-cpp/</link>
					<comments>https://nolowiz.com/virtual-destructors-in-cpp/#respond</comments>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Sat, 11 Dec 2021 07:56:08 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=2058</guid>

					<description><![CDATA[<p>In this article, we will discuss virtual destructors in CPP. Destructor is part of Resource Acquisition Is Initialization(RAII) programming idiom. The basic idea of RAII is constructor will acquire(allocate) resources and the destructor will release resources for a class. The most common example of a destructor is when the constructor uses new, and the destructor ... <a title="Virtual Destructors in CPP" class="read-more" href="https://nolowiz.com/virtual-destructors-in-cpp/" aria-label="More on Virtual Destructors in CPP">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/virtual-destructors-in-cpp/">Virtual Destructors in CPP</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will discuss virtual destructors in CPP.</p>



<p>Destructor is part of <a href="https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization" target="_blank" rel="noreferrer noopener">Resource Acquisition Is Initialization</a>(RAII) programming idiom. The basic idea of RAII is constructor will acquire(allocate) resources and the destructor will release resources for a class. The most common example of a destructor is when the constructor uses new, and the destructor uses delete.</p>



<p>Destructors are also known as “prepare to die” member functions. They have often abbreviated “dtor”. Now let&#8217;s dive into virtual destructors.</p>



<h2>When to use virtual destructors?</h2>



<p>When we want to delete polymorphically we can use a virtual destructor. In other words, virtual destructors are useful when we want to delete an instance of a derived class through a pointer to the base class.</p>



<p>Let&#8217;s understand it with an example. Consider base class without destructor.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

class Base{
public:
     ~Base(){ cout&lt;&lt;&quot;Base destructor&quot;&lt;&lt;endl; }

};

class Derived : public Base{
public:
    ~Derived(){ cout&lt;&lt;&quot;Derived destructor&quot;&lt;&lt;endl; }
};

int main()
{
    Base *pBase=new Derived();
    delete pBase;
    return 0;
}

</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Base destructor</code></pre>



<p>Here only base class destructor called, derived class destructor not called and as a result resource will not be released( Here we are only printing for sake of simplicity). To solve this issue we can use a virtual destructor.</p>



<p><span class="has-inline-color has-vivid-red-color">virtual ~Base()</span>{ cout&lt;&lt;&#8220;Base destructor&#8221;&lt;&lt;endl; }</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
// Virtual destructor example

#include &lt;iostream&gt;

using namespace std;

class Base{
public:
    virtual ~Base(){ cout&lt;&lt;&quot;Base destructor&quot;&lt;&lt;endl; }

};
class Derived : public Base{
public:
    ~Derived(){ cout&lt;&lt;&quot;Derived destructor&quot;&lt;&lt;endl; }
};

int main()
{
    Base *pBase=new Derived();
    delete pBase;
    return 0;
}

</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Derived destructor
Base destructor</code></pre>



<p>From this, we can see first the derived class destructor is called the base class destructor.</p>



<h2>Conclusion</h2>



<p>In conclusion, a base class destructor should be virtual if we are going to delete polymorphically ( delete via a pointer to base).</p>



<p></p>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fvirtual-destructors-in-cpp%2F&amp;linkname=Virtual%20Destructors%20in%20CPP" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fvirtual-destructors-in-cpp%2F&amp;linkname=Virtual%20Destructors%20in%20CPP" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fvirtual-destructors-in-cpp%2F&amp;linkname=Virtual%20Destructors%20in%20CPP" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fvirtual-destructors-in-cpp%2F&amp;linkname=Virtual%20Destructors%20in%20CPP" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fvirtual-destructors-in-cpp%2F&#038;title=Virtual%20Destructors%20in%20CPP" data-a2a-url="https://nolowiz.com/virtual-destructors-in-cpp/" data-a2a-title="Virtual Destructors in CPP"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/virtual-destructors-in-cpp/">Virtual Destructors in CPP</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nolowiz.com/virtual-destructors-in-cpp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Size of C++ classes</title>
		<link>https://nolowiz.com/size-of-cpp-classes/</link>
					<comments>https://nolowiz.com/size-of-cpp-classes/#respond</comments>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Fri, 10 Dec 2021 01:52:42 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=2034</guid>

					<description><![CDATA[<p>In this article, we will discuss the size of C++ classes. We will look into an empty class, a class with member function, a class with virtual functions, and a class with data members. We will be using a 64-bit machine to compile the test code. For a 64-bit machine, the pointer size is 8 ... <a title="Size of C++ classes" class="read-more" href="https://nolowiz.com/size-of-cpp-classes/" aria-label="More on Size of C++ classes">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/size-of-cpp-classes/">Size of C++ classes</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we will discuss the size of <a href="https://isocpp.org/" target="_blank" rel="noreferrer noopener">C++ classes</a>. We will look into an empty class, a class with member function, a class with virtual functions, and a class with data members.</p>



<p>We will be using a 64-bit machine to compile the test code. For a 64-bit machine, the pointer size is 8 bytes.</p>



<h2>Size of empty class</h2>



<p>The size of an empty c++ class is 1 byte. It is to ensure that the addresses of two different objects will be different.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

class empty{
};

int main()
{
    cout&lt;&lt; &quot;Size of empty class : &quot;&lt;&lt;sizeof(empty)&lt;&lt;endl;
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Size of empty class : 1</code></pre>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354" crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-2735334721002354" data-ad-slot="8835878737" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<h2>Class with a member function</h2>



<p>Let&#8217;s consider a class with one member function.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

class Shape{
public:
    void draw(){cout &lt;&lt;&quot;shape&quot;;}
};

int main()
{
    cout&lt;&lt; &quot;Size of shape class : &quot;&lt;&lt;sizeof(Shape)&lt;&lt;endl;
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Size of shape class : 1</code></pre>



<p>So adding member functions doesn&#8217;t change the class size. What about virtual functions?</p>



<h2>Class with virtual function</h2>



<p>Let&#8217;s consider a class with a virtual function. Do you think its size is still 1 byte?</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

class Shape{
public:
    virtual void draw(){cout &lt;&lt;&quot;shape&quot;;}
};

int main()
{
    cout&lt;&lt; &quot;Size of shape class with virtual function : &quot;&lt;&lt;sizeof(Shape)&lt;&lt;endl;
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<pre class="wp-block-code"><code>Size of shape class with virtual function : 8</code></pre>



<p>For classes with virtual functions, the compiler will add a hidden pointer known as a virtual pointer(VPTR). The size of a pointer is in a 32-bit platform is 4 bytes. Here we are using the 64-bit machine so it is 8 bytes.</p>



<h2>Class with data members</h2>



<p>Let&#8217;s consider a class with member variables.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;

using namespace std;

class Shape{
public:
    int a;
};

int main()
{
    cout&lt;&lt; &quot;Size of shape class with data member : &quot;&lt;&lt;sizeof(Shape)&lt;&lt;endl;
    return 0;
}
</pre></div>


<p><strong>Output</strong></p>



<p>Size of shape class with data member: 4</p>



<p>Here the size of the class changed based on the data type of the member variable. The int data type will take 4 bytes of memory.</p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354" crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-2735334721002354" data-ad-slot="8835878737" data-ad-format="auto" data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<h2>Conclusion</h2>



<p>In conclusion, we can summarize the size of C++ classes as</p>



<ul><li>The Size of empty class : 1 byte</li><li>Size of class with member function(s) : 1 byte</li><li>Size of class with virtual functions : 8 bytes on 64-bit platform or 4 bytes on 32-bit platform</li></ul>



<p></p>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fsize-of-cpp-classes%2F&amp;linkname=Size%20of%20C%2B%2B%20classes" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fsize-of-cpp-classes%2F&amp;linkname=Size%20of%20C%2B%2B%20classes" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fsize-of-cpp-classes%2F&amp;linkname=Size%20of%20C%2B%2B%20classes" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fsize-of-cpp-classes%2F&amp;linkname=Size%20of%20C%2B%2B%20classes" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fsize-of-cpp-classes%2F&#038;title=Size%20of%20C%2B%2B%20classes" data-a2a-url="https://nolowiz.com/size-of-cpp-classes/" data-a2a-title="Size of C++ classes"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/size-of-cpp-classes/">Size of C++ classes</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nolowiz.com/size-of-cpp-classes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Shortest C++ Program</title>
		<link>https://nolowiz.com/shortest-c-program/</link>
					<comments>https://nolowiz.com/shortest-c-program/#respond</comments>
		
		<dc:creator><![CDATA[Rupesh Sreeraman]]></dc:creator>
		<pubDate>Fri, 05 Nov 2021 14:06:32 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<guid isPermaLink="false">https://nolowiz.com/?p=1928</guid>

					<description><![CDATA[<p>Have you ever thought about the shortest C++ program? In this article, we&#8217;ll look into the shortest C++ program. C++ is a compiled language. Firstly, the compiler processes the source code files and produces object files. The linker will combine these object files to yield an executable program. Let&#8217;s look into the smallest C++ program ... <a title="Shortest C++ Program" class="read-more" href="https://nolowiz.com/shortest-c-program/" aria-label="More on Shortest C++ Program">Read more</a></p>
<p>The post <a rel="nofollow" href="https://nolowiz.com/shortest-c-program/">Shortest C++ Program</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Have you ever thought about the shortest <a href="https://isocpp.org/" target="_blank" rel="noreferrer noopener">C++</a> program? In this article, we&#8217;ll look into the shortest C++ program.</p>



<p>C++ is a compiled language. Firstly, the compiler processes the source code files and produces object files. The linker will combine these object files to yield an executable program.</p>



<p>Let&#8217;s look into the smallest C++ program</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
int main() {}
</pre></div>


<p>This shortest program has a <span class="has-inline-color has-vivid-red-color">main()</span> function it takes no arguments and does nothing. The curly brackets {} express the grouping in C++. Here it is used to start and end the main function.</p>



<p>As per the C++ standard, every C++ program must have exactly one global main function. The program starts executing this <span class="has-inline-color has-vivid-red-color">main() </span>function.</p>



<p>The main function will return an <span class="has-inline-color has-vivid-red-color">int</span> value. The program will return this value to the &#8220;system&#8221;. A non-zero value indicates the failure. Linux/Unix-based environments often make use of this returned value and Windows environments rarely use it.</p>



<p></p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354"
     crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2735334721002354"
     data-ad-slot="8835878737"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>



<p>Let&#8217;s compile and run this program</p>



<pre class="wp-block-code"><code>g++ -o shortest shortest_program.cpp
./shortest</code></pre>



<p>The program displays nothing, let us check the returned value. </p>



<pre class="wp-block-code"><code>./shortest
echo $?
0</code></pre>



<p>From the above output, we can see that it returns 0.</p>



<p>Let&#8217;s disassemble this program and see the instructions. Yes, it is not that small :).</p>



<pre class="wp-block-code"><code>g++ -S -o shortest.s shortest_program.cpp</code></pre>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: cpp; title: ; notranslate">
	.file	&quot;shortest_program.cpp&quot;
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$0, %eax
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	&quot;GCC: (Ubuntu 11.2.0-7ubuntu2) 11.2.0&quot;
	.section	.note.GNU-stack,&quot;&quot;,@progbits
	.section	.note.gnu.property,&quot;a&quot;
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	&quot;GNU&quot;
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:
</pre></div>


<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Shortest C++ Program | NoloWiz #cpp #nolowiz #shortest #program" width="900" height="506" src="https://www.youtube.com/embed/2JaaYUo95GI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>That&#8217;s all about the shortest C++ program.</p>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2735334721002354"
     crossorigin="anonymous"></script>
<!-- article-horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-2735334721002354"
     data-ad-slot="8835878737"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p></p>
<p><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fnolowiz.com%2Fshortest-c-program%2F&amp;linkname=Shortest%20C%2B%2B%20Program" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_whatsapp" href="https://www.addtoany.com/add_to/whatsapp?linkurl=https%3A%2F%2Fnolowiz.com%2Fshortest-c-program%2F&amp;linkname=Shortest%20C%2B%2B%20Program" title="WhatsApp" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fnolowiz.com%2Fshortest-c-program%2F&amp;linkname=Shortest%20C%2B%2B%20Program" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_copy_link" href="https://www.addtoany.com/add_to/copy_link?linkurl=https%3A%2F%2Fnolowiz.com%2Fshortest-c-program%2F&amp;linkname=Shortest%20C%2B%2B%20Program" title="Copy Link" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fnolowiz.com%2Fshortest-c-program%2F&#038;title=Shortest%20C%2B%2B%20Program" data-a2a-url="https://nolowiz.com/shortest-c-program/" data-a2a-title="Shortest C++ Program"></a></p><p>The post <a rel="nofollow" href="https://nolowiz.com/shortest-c-program/">Shortest C++ Program</a> appeared first on <a rel="nofollow" href="https://nolowiz.com">NoloWiz</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://nolowiz.com/shortest-c-program/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
