Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>C++ is a more low-level language that executes without the context of an interpreter. As such, it has many different design choices than does Python, because C++ has no environment which it can rely on to manage information like types and memory. C++ can be used to write an operating system kernel where there is no code running on the machine except for the program itself, which means that the language (some library facilities are not available for so-called freestanding implementations) must be self-contained. This is why C++ has no equivalent to Python's <code>eval</code>, nor a means of determining members, etc. of a class, nor other features that require an execution environment (or a massive overhead in the program itself instead of such an environment)</p> <p>For your individual questions:</p> <ul> <li>do you have to include the <code>&lt;iostream&gt;</code> everytime? Do you ever not need it?</li> </ul> <p><code>#include &lt;iostream&gt;</code> is the directive that imports the <code>&lt;iostream&gt;</code> header into your program. <code>&lt;iostream&gt;</code> contains the standard input/output objects - in particular, <code>cout</code>. If you aren't using standard I/O objects (for instance, you use only file I/O, or your program uses a GUI library, or are writing an operating system kernel), you do not need <code>&lt;iostream&gt;</code></p> <ul> <li>same question for the standard library, when do you not need std::*?</li> </ul> <p><code>std</code> is the namespace containing all of the standard library. <code>using namespace std;</code> is sort of like <code>from std import *</code>, whereas a <code>#include</code> directive is (in this regard) more like a barebones <code>import std</code> statement. (in actual fact, the mechanism is rather different, because C++ does not use <code>using namespace std;</code> to automatically lookup objects in <code>std</code>; the using-directive only imports the names into the global namespace.)</p> <p>I'll note here that using-directives (<code>using namespace</code>) are frequently frowned upon in C++ code, as they import a lot of names and can cause name clashes. using-declarations (<code>using std::cout;</code>) are preferred when possible, as is limiting the scope of a using-directive (for instance, to one function or to one source file). Don't ever put <code>using namespace</code> in a header without good reason.</p> <ul> <li>is the "main" part a function? Do you ever call the main function? Why is it an integer? Why does C++ need to have a main function but Python doesn't?</li> </ul> <p><code>main</code> is the entry point to the program - where execution starts. In Python, the <code>__main__</code> module serves the same purpose. C++ does not execute code outside a defined function like Python does, so its entry point is a function rather than a module.</p> <ul> <li>do you need "std::cout &lt;&lt; "? Isn't that needlessly long and complicated compared to Python?</li> </ul> <p><code>std::cout</code> is only needed if you don't import the <code>cout</code> name into the global namespace, either by a using-directive (<code>using namespace std;</code>) or by a using-declaration (<code>using std::cout</code>). In this regard, it is once again much like the distinction between Python's <code>import std</code> and <code>from std import *</code> or <code>from std import cout</code>.</p> <p>The <code>&lt;&lt;</code> is an overloaded operator for standard stream objects. <code>cout &lt;&lt; value</code> calls <code>cout</code>'s function to output <code>value</code>. Python needs no such extra code because <code>print</code> is built into the language; this does not make sense for C++, where there may not even be an operating system, much less an I/O library.</p> <ul> <li>do you need to return 0 even when you are never going to use it?</li> </ul> <p>No. <code>main</code> (and no other function) has an implicit <code>return 0;</code> at the end. The return value of <code>main</code> (or, if the <code>exit</code> function is called, the value passed to it) is passed back to the operating system as the exit code. 0 indicates the program successfully executed - that it encountered no errors, etc. If an error is encountered, a non-zero value should be returned (or passed to <code>exit</code>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload