Answers.Body: <p>There are many reasons for wanting to learn a new language, whether C++ or something else. I'll try to make this general enough to address Timothy Truckle's concerns in a comment, but also the questioner's direct question. </p>
  
  <p>If you are a scholar you learn a new language because it gives you some additional perspective on programming that the languages you already know don't possess. For example, some major programming paradigms are imperative, object-oriented, functional, and logic. The scholar will want to know at least the essence of a language from each group. </p>
  
  <p>If you are a prospective employee (such as a student) you probably want to know several languages, even if they are quite similar (ruby, python) because it might give you an edge in getting a job. </p>
  
  <p>If you are already employed you use the language of the group you are with, even COBOL. Occasionally a group will want to change languages and when this happens, knowing the new language will, hopefully, prevent your getting replaced. </p>
  
  <p>If you simply have a program to write, use the language you know best unless (a) it is inappropriate to the task or (b) you want to wear the scholar hat for a while. </p>
  
  <p>Sometimes you choose a language for no other reason than the set of libraries that it is offered with. Those libraries may do most of the work for you in the current task, so don't underestimate the importance of this. </p>
  
  <p>As to the question of C++ vs Java, they are similar on the surface, but have deep differences. Java is a fairly pure OO language, though there have been improvements since it was introduced. C++, on the other hand is a true multi-paradigm language. It is not, however, an OO language, strictly speaking, though it can be programmed that way. It is much stronger as a <em>Data Abstraction</em> language than an OO language, but you need to become familiar with the Standard Template Library to be able to clearly see the difference between that and OO. </p>
  
  <p>From the scholar's standpoint, the great thing about Java, and the reason it is a good <em>first</em> language, is that it takes many decisions and concerns out of the hands of the application programmer and handles them automatically. The biggest of these is memory management, especially the heap. Java's garbage collector means that you needn't concern yourself with the lifetime of objects except in extreme cases. These cases don't arise for beginners, and when they do, Java is not likely to be the best language (billions of objects existing simultaneously, for example). </p>
  
  <p>Still from the standpoint of the scholar, programming in C++ will force you to think at a more detailed level. This isn't ideal for the beginner, but every master programmer needs to do the <em>nitty-gritty</em> at some point, and C++ forces you there. </p>
  
  <p>Java is a much more <em>abstract</em> language. For example, it has only single inheritance, so using formal interfaces is much more important there. The "meaning" of code can be expressed in Java Docs for interfaces rather than in the code itself (though there is danger there, of course). If you program wisely in Java those Java Docs on the Interfaces are likely all of the "comments" that your code needs. </p>
  
  <p>Multiple inheritance in C++ holds many pitfalls, that are overcome with attention to detail and with a level of planning that you don't need in Java, since that alternative isn't available. Combining two superclasses as parents of a subclass can lead to poor design if not done wisely. That isn't a <em>stroke against</em> C++ but a warning that the programmer needs to develop additional skills to avoid pitfalls. </p>
  
  <p>Other things in C++ are more complex than in Java. Visibility, for example, and especially <em>friend</em> classes. Also <em>includes</em>, namespaces, and some lower-level things such as structs. Again, it isn't that this is bad in C++ but the programmer needs to learn how to handle a greater range of detail than in a more abstract language or in a single paradigm language. Learning how to deal with that, and learning the tools that help you do it is valuable. </p>
  
  <p>However, to say that C++ is more efficient than Java is wrong in general, though it may be correct in some situations. Compilers have come a long way in recent years and the efficiency of code is dependent on the quality of the compiler. In particular, if a language must be used at a detail level then the compiler has less opportunity for optimization than if it is used at a more abstract level. If my program describes <em>how to do something</em> it is quite different than if it describes <em>what must be done</em>. In the latter case, the compiler may be better able to come up with the <em>how to</em> than you are. If you describe the <em>how to</em> itself in your program, the compiler may have no option than to plod along with the steps you chose even when there might be a better path to the goal. Functional and Logic languages were once thought inefficient, but they need not be. It was a result of the level of sophistication of translation available at the time. </p>
  
  <p>To conclude, C++ is a good choice to follow Java as it will reveal a level of detail that the Java programmer didn't previously need to contend with. Even if you only use C++ as a <em>better C</em> you will learn a lot. </p>
  
  <p>But learning a language, like Prolog, say, in a completely different family will also teach you  things that are quite different, not just deeper, than what you already know - orthogonal dimensions. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-14T19:32:43.367Z
Answers.Text: Overall very good,  I'd add that c++ implementations of low level code like the guts of xalan/xerces xml libraries tend to be twice as fast as their java counterparts. On the other hand there are certainly places where java is comparable in throughput to c++. You might point out that the jvm is written in c/c++
Answers.UserDisplayName: pojo-guy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-13T18:21:03.700Z
Answers.LastActivityDate: 2018-01-13T18:21:03.700Z
Answers.UserDisplayName: Buffy
Answers.Body: <p>I think you're having trouble justifying the study of C++ because of the way that you're thinking about it.  If you are aiming towards the practical benefits, then you immediately get to the following trap: neither you nor your students has any idea what languages they will work with in the future, so neither you nor your students have any idea what languages would be the most practical to learn.  There are no good answers when you begin this way, and the motivations will always seem to fall a bit flat.</p>
  
  <p>I might steer the students (and you!) towards a slightly different model of thinking about benefits here.  What foundational <em>ideas about programming and Computer Science</em> are easier to grasp in C++ than in Java?  These foundational ideas will important later, regardless of the development environment, because they are the building blocks of computational thinking.</p>
  
  <p><em>When we teach CS, we are concept-focused and language agnostic.</em></p>
  
  <p>This lens also provides you with the beginnings of a map through the topic itself.  You can quickly sketch out a curriculum at this point, which is best done backwards:</p>
  
  <ol>
  <li><strong>What are the most important concepts that you want the students to learn?</strong> </li>
  </ol>
  
  <p>Fair warning: I am not a C++ programmer!  There may well be more important topics than these to get into, but I know that C++ gives students an opportunity to learn about pointers, pointer arithmetic, and memory management.  You could also get into casting and bit-operations, which forces the students to learn about data representation.  Each of these concepts are fundamental to computers, and much harder to learn about in Java, which abstracts them away.</p>
  
  <p><em>Selling</em> the language to the students is now a comparatively easier task.  You're using C++ because you get to show them some very cool things about how computers really work.</p>
  
  <p>The best time to do such <em>selling</em> is on the first day.  By explaining #1 to them at the beginning, you alter their motivation going forward.  They want to do well on the tasks you've outlined not <em>only</em> because it gets them a better grade, but because they are also mastering deep, important concepts that will help them understand computational thinking.  Computational thinking will help them regardless of what they do in the future.  Heck, they could leave the field entirely; they would still have mastered a rich and powerful tool-set for thinking about problems, wherever they go.</p>
  
  <p>Now we get to the planning portion:</p>
  
  <ol start="2">
  <li><strong>What is the most logical ordering of those concepts?</strong></li>
  <li><strong>How will you (and the students!) know that the concepts have been learned?  This could be some combination of projects and assessments.</strong></li>
  </ol>
  
  <p>I've grouped these together because they are a little interdependent. Sometimes step 3 will cause you to go back and modify the order in step 2, but these two steps are how you arrive at a well-organized semester.  The best tasks for #3 are also rich and interesting in their own right.  (One resource I could recommend for help finding great, motivating tasks for a given concept is this very site.)</p>
  
  <p>It is extremely important that you really get a good answer for #3.  If you've done these three steps well, then the projects and assessments will now be sufficient to prove that the students understand the material.  If you've truly answered #3, and you have total trust in that, then it frees you up to spend the entire course doing one thing:</p>
  
  <ol start="4">
  <li><strong>Prepare your students to do really well on those tasks.</strong></li>
  </ol>
  
  <p>The reason that #4 is so important is because of the social dynamic that results. You really want your students to feel like you're on their side, pulling for the same team. And if your goal, every day, is to help them master the material, then they will be able to see that you are putting tremendous effort into trying to help them.  And there are also benefits to you here; as a relatively beginner teacher (who will certainly make some mistakes along the way!), having students who are willing to cut you a little slack can make your life a lot easier as well.</p>
  
  <p>Now we have a strong course structure!  Tell the students about the cool <em>concepts</em> they will be learning, and tell them that C++ is a great language to focus in on these concepts.  Map out the order of the concepts that you'll be covering and tell them about the projects, tests, etc.  Then come in every day, and focus in a rather laser-like way on making sure that the students are well prepared for the important tasks that you've designed.</p>
  
  <p><sup>(Apologies for the length of this response.  I wasn't trying to write a book chapter, but I am leaving the information in in the hopes that you will find it very helpful!)</sup></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-14T12:11:16.267Z
Answers.Text: *"C++ gives students an opportunity to learn about pointers, pointer arithmetic, and memory management. You could also get into casting and bit-operations, which forces the students to learn about data representation. "* These are concepts of *plain `C`*. You don't need `C++` to demonstrate them. But Yes, it's hard to get a pure `C` compiler (for free) these days...
Answers.UserDisplayName: Timothy Truckle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-14T14:49:50.197Z
Answers.Text: @TimothyTruckle Fair enough :)  I mentioned I was not a C++ programmer.  Most of what I teach is in Java, so those concepts were guesses on my part.  Buffy's answer is undoubtedly far better in terms of what concepts should probably be focused on.  My point to OP was more about how to justify (and set up) a class for students.
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-14T19:36:36.320Z
Answers.Text: In my experience,  people start to really understand programming after they learn their third language. C++ is sufficiently similar to java that there is carryover,  but different enough to increase the breadth and depth of their skills.
Answers.UserDisplayName: pojo-guy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-15T02:40:11.943Z
Answers.Text: @TimothyTruckle, GCC, with the right command-line options, can be made to obey and enforce any of several different versions of the C standard. (https://gcc.gnu.org/onlinedocs/gcc/Standards.html#Standards.)
Answers.UserDisplayName: Solomon Slow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-15T03:16:23.247Z
Answers.Text: @TimothyTruckle What's wrong with GCC or LLVM, the quintessential free C compilers?
Answers.UserDisplayName: Criticize SE actions means ban
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-15T06:37:33.923Z
Answers.Text: @immibis I don't know about LLVM though, but being pedantic I'd say that GCC is a C++ compiler you can configure to behave like a C compiler. And there is the problem: you have to actively do it...
Answers.UserDisplayName: Timothy Truckle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-15T21:37:21.467Z
Answers.Text: @TimothyTruckle G++ is a C++ compiler and GCC is a C compiler...
Answers.UserDisplayName: Criticize SE actions means ban
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-15T22:10:45.827Z
Answers.Text: @immibis and I thought it stands for [gnu compiler collection](https://gcc.gnu.org/)
Answers.UserDisplayName: Timothy Truckle
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-15T22:22:14.607Z
Answers.Text: @TimothyTruckle Well by that logic GCC is a Fortran compiler, not C or C++. GCC is both the GNU Compiler Collection, *and* the GNU C Compiler included in that collection.
Answers.UserDisplayName: Criticize SE actions means ban
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-13T18:38:28.990Z
Answers.LastActivityDate: 2018-01-17T10:59:24.240Z
Answers.LastEditDate: 2018-01-17T10:59:24.240Z
Answers.UserDisplayName: Ben I.
Answers.Body: <p>I'm a multi-platform programmer who has spent many years using C++.</p>
  
  <p>In the last 5 years that has included:</p>
  
  <ul>
  <li>The Realm mobile database engine used by a couple of <strong>billion</strong>
  people worldwide, embedded in their mobile apps. I mainly worked on their Xamarin SDK in C# but had to use C++ in the glue layer to the engine.</li>
  <li>Using the Cocos2D-X
  game engine, for mobile games (actually for my mobile messaging app
  which has a game-like interface). </li>
  <li>Working on a 3D CAD program used to
  design minesites, including their roads, open-pits, tunnels and
  explosive blast patterns.</li>
  </ul>
  
  <p>C++ is still a great language when you need to mix both high-level abstractions and compile down for really low-level performance.</p>
  
  <p>Once of the cooler things happening is compiling C++ to JavaScript and WebAssembly, to deliver high performance graphical web apps like the <a href="https://www.figma.com/" rel="nofollow noreferrer">Figma vector editor.</a></p>
  
  <p>Even if they won't use C++ as a primary language, it's still important to know if you want to interface to some libraries.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-24T12:07:04.563Z
Answers.LastActivityDate: 2018-01-24T12:07:04.563Z
Answers.UserDisplayName: Andy Dent
Answers.Body: <p>There are a few places C++ (or C, or rust or ...) is more useful than Java[1] because C++ does (what Rust likes to call) "zero cost abstractions". The idea is that C++ gives you the power to be as low level as you want to get, and at the same time as high level as you want to go.</p>
  
  <ol>
  <li><strong>Real Time</strong>. Note, I didn't say fast. real-time doesn't always mean fast. Technically, a real-time program could actually be slower than one which is not "real-time".</li>
  </ol>
  
  <p>The main focus of real time is that there should be no pauses longer than X between event firings. So if you have a self-driving car doing something like like:</p>
  
  <pre><code>while (true){
      MyType currentPos = getPos();
      MyType newPos = doCalculationsOnPos(currentPos);
      adjustWheel(newPos);
  }
  </code></pre>
  
  <p>You should know for a <em>fact</em> that you will adjust your steering wheel every X ms or your car can crash. In C++ (and a few other languages, like C, asm, or rust) you can tell during compile time how long will it take to run this loop.
  Now what will happen in Java (a garbage-collected language)?</p>
  
  <pre><code>while (true){
     MyType currentPos = getPos();
     System.gc();
     MyType newPos = doCalculationsOnPos(currentPos);
     System.gc();
     adjustWheel(newPos);
  }
  </code></pre>
  
  <p>The problem here is that you have no idea how long <code>System.gc()</code> will decide to take. And this isn't just useful in robotics (cars, airplanes, or rockets), it's useful in prosaic cases such as writing a <a href="https://blog.ntpsec.org/2017/01/18/rust-vs-go.html" rel="nofollow noreferrer">time server</a> [2]. </p>
  
  <ol start="2">
  <li><strong>Interfaceable</strong> - If you need to write libraries, you'll have to do it in C++ (or C, Rust, Pascal ...). The reason is that, as above, C++ does zero-cost abstractions. </li>
  </ol>
  
  <p>So for example, code like the below can be called like a C function (which is the standard function interface in pretty much all OSs these days) of the same name. So you can use this function in Python, in Rust, C, Java, C#, Go, you name it.</p>
  
  <pre><code>extern "C"
  int hi(int p){
      typeO* a = new typeO(p)
      int returnVal = a.doWork();
      delete a;
      return returnVal;
  }
  </code></pre>
  
  <ol start="3">
  <li><strong>Low level code</strong> - In C++ you can control how and where memory is stored - is it stored in the stack or in the heap. How would you write a Java memory allocater when you don't have a heap to put things in?</li>
  </ol>
  
  <p>[1]. Yes, there are AOT compilers written for Java, and there actually was an <a href="https://stackoverflow.com/questions/1988196/is-it-possible-to-make-an-operating-system-using-java#1988199">0OS written in Java</a>. But you'll need a lot more work bootstrapping the Java environment than a C++ environment. But more importantly, since those solutions are fairly niche (and, at times, not FOSS or even gratis), the community around them is tiny. So, yes, you can compile java to exe (and have been able to do so for a while), but one of the reason that some of the Java community's moved to Go was precisely because Go compiles to a static binary, and the rest were eagerly awaiting AOT compilation.</p>
  
  <p>[2]. Yes, he picked Go (which is a GC language), but it can be turned off, turning Go into a real-time language. The thing is that an ntp server needs to be real time for only a short while. If, though, you need it off all the time, Go won't work for you as memory will grow until you clean up garbage, turning off all real-time guarantees. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-20T17:25:04.257Z
Answers.Text: This is very interesting. Indeed I can focus my course on real-time applications and libraries that should link with other languages.
Answers.UserDisplayName: Erel Segal-Halevi
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-19T07:23:20.670Z
Answers.LastActivityDate: 2018-01-19T11:48:52.467Z
Answers.LastEditDate: 2018-01-19T11:48:52.467Z
Answers.UserDisplayName: cse
Body: <p>I am going to teach a C++ course to students who already know Java. From  answers to <a href="https://cseducators.stackexchange.com/q/4113/1873">my previous question</a>, I learned that C++ is substantially harder to master than Java, particularly because of the need to manually manage memory allocation. Thus, I expect my students to ask:</p>
  
  <blockquote>
    <p>Why do we need to study C++ at all? Why not use only Java?</p>
  </blockquote>
  
  <p>Two general answers are:</p>
  
  <ul>
  <li>Because C++ is faster (but I am not sure the difference is substantial enough)</li>
  <li>Because C++ is already used in many companies where you might work in the future.</li>
  </ul>
  
  <p>But I am looking for a more convincing answer. In particular:</p>
  
  <ul>
  <li>In what kind of applications is C++ considered a better choice than Java?</li>
  <li>In what parts of the software industry is C++ used more than Java?</li>
  </ul>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-13T17:07:30.930Z
Comments.Text: Are **you** familiar with `C++`? In case your're not the better question would be: *what other programming Language should we learn beside Java?* Eg: the automobile industry is using *plain `C`* because `C++` didn't hold its promises regarding speed (either developing nor execution time)  and safety.
Comments.UserDisplayName: Timothy Truckle
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-13T17:45:51.220Z
Comments.Text: After some thinking I believe you cannot get a good answer from here because  we all live in our *filter bubble*. I personally know nobody doing `C++` for a living. Some other might know only people coding it and nothing else. So what you're asking for is a *research* about the current importance of `C++` in the current industry which most likely non of us has done (yet)...
Comments.UserDisplayName: Timothy Truckle
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-13T19:17:03.183Z
Comments.Text: I disagree. The question seems to be about why to teach C++. That is certainly relevant here. I've authored C++ books, by the way, and many members, though not particularly active, are experts in C++ and its instruction.
Comments.UserDisplayName: Buffy
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-14T15:28:00.720Z
Comments.Text: It is not that C++ is fast (to make if fast all sorts of manual optimisations need to be done), but that Java is slow. Java is slow because of virtual machine, and because of memory layout. e.g. for a linked list, it is impossible in java to ensure that the payload is proximate to the linked list, therefore a java implementation will suffer many more cache misses. Another thing that java does not have is proper tail call optimisation. However while it is easy to list short comings in Java, I would find it impossible to recommend C++. The only time to use C++, is to make a contract last forever
Comments.UserDisplayName: ctrl-alt-delor
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-15T02:29:38.047Z
Comments.Text: Memory allocation?  That's just scratching the surface.  The C++ language is _enormously_ more complicated than the Java language.  Just for example, take a look at the documentation for [std::enable_if](http://en.cppreference.com/w/cpp/types/enable_if) and see if you can design a non-trivial exercise for your students that requires them to use it.  Or, just try (I dare you!) to learn anything about the [Boost libraries](http://www.boost.org/) by reading their `.h` files.
Comments.UserDisplayName: Solomon Slow
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-15T02:54:21.260Z
Comments.Text: One thing you can do with typical tool-chains for C, for C++, for Rust, and for many other programming languages, but _not_ with typical Java tool-chains:  You can write projects that create _native executable code_. That covers a lot of territory including; code that links with code written in other languages, code that directly calls the operating system, loadable modules and device drivers that run _in_ the operating system.  You can write your own operating system.  Or, you can write embedded, "bare metal" code that runs with no operating system at all.
Comments.UserDisplayName: Solomon Slow
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-15T03:11:14.907Z
Comments.Text: Because one day you will want to get a job at a company that doesn't use Java, so we're going to start exposing you to new languages now, so you won't be stuck thinking in Java?
Comments.UserDisplayName: Criticize SE actions means ban
Tags: <student-motivation><java><language-choice><c++>
Title: Motivation for learning C++ when you know Java
ThreadText: # Motivation for learning C++ when you know Java
  
  (Asked by: username_0 on 2018-01-13)
  
  I am going to teach a C++ course to students who already know Java. From answers to [my previous question](https://cseducators.stackexchange.com/q/4113/1873), I learned that C++ is substantially harder to master than Java, particularly because of the need to manually manage memory allocation. Thus, I expect my students to ask:
  
  > Why do we need to study C++ at all? Why not use only Java?
  
  Two general answers are:
  
  * Because C++ is faster (but I am not sure the difference is substantial enough)
  * Because C++ is already used in many companies where you might work in the future.
  
  But I am looking for a more convincing answer. In particular:
  
  * In what kind of applications is C++ considered a better choice than Java?
  * In what parts of the software industry is C++ used more than Java?
  
  **Tags:** student-motivation, java, language-choice, c++
  
  > **Comments:**
  > **username_1**: Are **you** familiar with `C++`? In case your're not the better question would be: *what other programming Language should we learn beside Java?* Eg: the automobile industry is using *plain `C`* because `C++` didn't hold its promises regarding speed (either developing nor execution time)  and safety.  
  > **username_1**: After some thinking I believe you cannot get a good answer from here because  we all live in our *filter bubble*. I personally know nobody doing `C++` for a living. Some other might know only people coding it and nothing else. So what you're asking for is a *research* about the current importance of `C++` in the current industry which most likely non of us has done (yet)...  
  > **username_2**: I disagree. The question seems to be about why to teach C++. That is certainly relevant here. I've authored C++ books, by the way, and many members, though not particularly active, are experts in C++ and its instruction.  
  > **username_3**: It is not that C++ is fast (to make if fast all sorts of manual optimisations need to be done), but that Java is username_4. Java is username_4 because of virtual machine, and because of memory layout. e.g. for a linked list, it is impossible in java to ensure that the payload is proximate to the linked list, therefore a java implementation will suffer many more cache misses. Another thing that java does not have is proper tail call optimisation. However while it is easy to list short comings in Java, I would find it impossible to recommend C++. The only time to use C++, is to make a contract last forever  
  > **username_4**: Memory allocation?  That's just scratching the surface.  The C++ language is _enormously_ more complicated than the Java language.  Just for example, take a look at the documentation for [std::enable_if](http://en.cppreference.com/w/cpp/types/enable_if) and see if you can design a non-trivial exercise for your students that requires them to use it.  Or, just try (I dare you!) to learn anything about the [Boost libraries](http://www.boost.org/) by reading their `.h` files.  
  > **username_4**: One thing you can do with typical tool-chains for C, for C++, for Rust, and for many other programming languages, but _not_ with typical Java tool-chains:  You can write projects that create _native executable code_. That covers a lot of territory including; code that links with code written in other languages, code that directly calls the operating system, loadable modules and device drivers that run _in_ the operating system.  You can write your own operating system.  Or, you can write embedded, "bare metal" code that runs with no operating system at all.  
  > **username_5**: Because one day you will want to get a job at a company that doesn't use Java, so we're going to start exposing you to new languages now, so you won't be stuck thinking in Java?  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_9 on 2018-01-19. Score: 2 | Accepted answer
  
  There are a few places C++ (or C, or rust or ...) is more useful than Java[1] because C++ does (what Rust likes to call) "zero cost abstractions". The idea is that C++ gives you the power to be as low level as you want to get, and at the same time as high level as you want to go.
  
  1. **Real Time**. Note, I didn't say fast. real-time doesn't always mean fast. Technically, a real-time program could actually be slower than one which is not "real-time".
  
  The main focus of real time is that there should be no pauses longer than X between event firings. So if you have a self-driving car doing something like like:
  
  ```
  while (true){
      MyType currentPos = getPos();
      MyType newPos = doCalculationsOnPos(currentPos);
      adjustWheel(newPos);
  }
  
  ```
  
  You should know for a *fact* that you will adjust your steering wheel every X ms or your car can crash. In C++ (and a few other languages, like C, asm, or rust) you can tell during compile time how long will it take to run this loop.
  Now what will happen in Java (a garbage-collected language)?
  
  ```
  while (true){
     MyType currentPos = getPos();
     System.gc();
     MyType newPos = doCalculationsOnPos(currentPos);
     System.gc();
     adjustWheel(newPos);
  }
  
  ```
  
  The problem here is that you have no idea how long `System.gc()` will decide to take. And this isn't just useful in robotics (cars, airplanes, or rockets), it's useful in prosaic cases such as writing a [time server](https://blog.ntpsec.org/2017/01/18/rust-vs-go.html) [2].
  
  2. **Interfaceable** - If you need to write libraries, you'll have to do it in C++ (or C, Rust, Pascal ...). The reason is that, as above, C++ does zero-cost abstractions.
  
  So for example, code like the below can be called like a C function (which is the standard function interface in pretty much all OSs these days) of the same name. So you can use this function in Python, in Rust, C, Java, C#, Go, you name it.
  
  ```
  extern "C"
  int hi(int p){
      typeO* a = new typeO(p)
      int returnVal = a.doWork();
      delete a;
      return returnVal;
  }
  
  ```
  
  3. **Low level code** - In C++ you can control how and where memory is stored - is it stored in the stack or in the heap. How would you write a Java memory allocater when you don't have a heap to put things in?
  
  [1]. Yes, there are AOT compilers written for Java, and there actually was an [0OS written in Java](https://stackoverflow.com/questions/1988196/is-it-possible-to-make-an-operating-system-using-java#1988199). But you'll need a lot more work bootstrapping the Java environment than a C++ environment. But more importantly, since those solutions are fairly niche (and, at times, not FOSS or even gratis), the community around them is tiny. So, yes, you can compile java to exe (and have been able to do so for a while), but one of the reason that some of the Java community's moved to Go was precisely because Go compiles to a static binary, and the rest were eagerly awaiting AOT compilation.
  
  [2]. Yes, he picked Go (which is a GC language), but it can be turned off, turning Go into a real-time language. The thing is that an ntp server needs to be real time for only a short while. If, though, you need it off all the time, Go won't work for you as memory will grow until you clean up garbage, turning off all real-time guarantees.
  
  > **Comments:**
  > **username_0**: This is very interesting. Indeed I can focus my course on real-time applications and libraries that should link with other languages.  
  
  ---
  
  ### Answer by username_7 on 2018-01-13. Score: 8
  
  I think you're having trouble justifying the study of C++ because of the way that you're thinking about it. If you are aiming towards the practical benefits, then you immediately get to the following trap: neither you nor your students has any idea what languages they will work with in the future, so neither you nor your students have any idea what languages would be the most practical to learn. There are no good answers when you begin this way, and the motivations will always seem to fall a bit flat.
  
  I might steer the students (and you!) towards a slightly different model of thinking about benefits here. What foundational *ideas about programming and Computer Science* are easier to grasp in C++ than in Java? These foundational ideas will important later, regardless of the development environment, because they are the building blocks of computational thinking.
  
  *When we teach CS, we are concept-focused and language agnostic.*
  
  This lens also provides you with the beginnings of a map through the topic itself. You can quickly sketch out a curriculum at this point, which is best done backwards:
  
  1. **What are the most important concepts that you want the students to learn?**
  
  Fair warning: I am not a C++ programmer! There may well be more important topics than these to get into, but I know that C++ gives students an opportunity to learn about pointers, pointer arithmetic, and memory management. You could also get into casting and bit-operations, which forces the students to learn about data representation. Each of these concepts are fundamental to computers, and much harder to learn about in Java, which abstracts them away.
  
  *Selling* the language to the students is now a comparatively easier task. You're using C++ because you get to show them some very cool things about how computers really work.
  
  The best time to do such *selling* is on the first day. By explaining #1 to them at the beginning, you alter their motivation going forward. They want to do well on the tasks you've outlined not *only* because it gets them a better grade, but because they are also mastering deep, important concepts that will help them understand computational thinking. Computational thinking will help them regardless of what they do in the future. Heck, they could leave the field entirely; they would still have mastered a rich and powerful tool-set for thinking about problems, wherever they go.
  
  Now we get to the planning portion:
  
  2. **What is the most logical ordering of those concepts?**
  3. **How will you (and the students!) know that the concepts have been learned? This could be some combination of projects and assessments.**
  
  I've grouped these together because they are a little interdependent. Sometimes step 3 will cause you to go back and modify the order in step 2, but these two steps are how you arrive at a well-organized semester. The best tasks for #3 are also rich and interesting in their own right. (One resource I could recommend for help finding great, motivating tasks for a given concept is this very site.)
  
  It is extremely important that you really get a good answer for #3. If you've done these three steps well, then the projects and assessments will now be sufficient to prove that the students understand the material. If you've truly answered #3, and you have total trust in that, then it frees you up to spend the entire course doing one thing:
  
  4. **Prepare your students to do really well on those tasks.**
  
  The reason that #4 is so important is because of the social dynamic that results. You really want your students to feel like you're on their side, pulling for the same team. And if your goal, every day, is to help them master the material, then they will be able to see that you are putting tremendous effort into trying to help them. And there are also benefits to you here; as a relatively beginner teacher (who will certainly make some mistakes along the way!), having students who are willing to cut you a little slack can make your life a lot easier as well.
  
  Now we have a strong course structure! Tell the students about the cool *concepts* they will be learning, and tell them that C++ is a great language to focus in on these concepts. Map out the order of the concepts that you'll be covering and tell them about the projects, tests, etc. Then come in every day, and focus in a rather laser-like way on making sure that the students are well prepared for the important tasks that you've designed.
  
  (Apologies for the length of this response. I wasn't trying to write a book chapter, but I am leaving the information in in the hopes that you will find it very helpful!)
  
  > **Comments:**
  > **username_1**: *"C++ gives students an opportunity to learn about pointers, pointer arithmetic, and memory management. You could also get into casting and bit-operations, which forces the students to learn about data representation. "* These are concepts of *plain `C`*. You don't need `C++` to demonstrate them. But Yes, it's hard to get a pure `C` compiler (for free) these days...  
  > **username_7**: @username_1 Fair enough :)  I mentioned I was not a C++ programmer.  Most of what I teach is in Java, so those concepts were guesses on my part.  username_2's answer is undoubtedly far better in terms of what concepts should probably be focused on.  My point to OP was more about how to justify (and set up) a class for students.  
  > **username_6**: In my experience,  people start to really understand programming after they learn their third language. C++ is sufficiently similar to java that there is carryover,  but different enough to increase the breadth and depth of their skills.  
  > **username_4**: @username_1, GCC, with the right command-line options, can be made to obey and enforce any of several different versions of the C standard. (https://gcc.gnu.org/onlinedocs/gcc/Standards.html#Standards.)  
  > **username_5**: @username_1 What's wrong with GCC or LLVM, the quintessential free C compilers?  
  > **username_1**: @immibis I don't know about LLVM though, but being pedantic I'd say that GCC is a C++ compiler you can configure to behave like a C compiler. And there is the problem: you have to actively do it...  
  > **username_5**: @username_1 G++ is a C++ compiler and GCC is a C compiler...  
  > **username_1**: @immibis and I thought it stands for [gnu compiler collection](https://gcc.gnu.org/)  
  > **username_5**: @username_1 Well by that logic GCC is a Fortran compiler, not C or C++. GCC is both the GNU Compiler Collection, *and* the GNU C Compiler included in that collection.  
  
  ---
  
  ### Answer by username_2 on 2018-01-13. Score: 5
  
  There are many reasons for wanting to learn a new language, whether C++ or something else. I'll try to make this general enough to address username_1's concerns in a comment, but also the questioner's direct question.
  
  If you are a scholar you learn a new language because it gives you some additional perspective on programming that the languages you already know don't possess. For example, some major programming paradigms are imperative, object-oriented, functional, and logic. The scholar will want to know at least the essence of a language from each group.
  
  If you are a prospective employee (such as a student) you probably want to know several languages, even if they are quite similar (ruby, python) because it might give you an edge in getting a job.
  
  If you are already employed you use the language of the group you are with, even COBOL. Occasionally a group will want to change languages and when this happens, knowing the new language will, hopefully, prevent your getting replaced.
  
  If you simply have a program to write, use the language you know best unless (a) it is inappropriate to the task or (b) you want to wear the scholar hat for a while.
  
  Sometimes you choose a language for no other reason than the set of libraries that it is offered with. Those libraries may do most of the work for you in the current task, so don't underestimate the importance of this.
  
  As to the question of C++ vs Java, they are similar on the surface, but have deep differences. Java is a fairly pure OO language, though there have been improvements since it was introduced. C++, on the other hand is a true multi-paradigm language. It is not, however, an OO language, strictly speaking, though it can be programmed that way. It is much stronger as a *Data Abstraction* language than an OO language, but you need to become familiar with the Standard Template Library to be able to clearly see the difference between that and OO.
  
  From the scholar's standpoint, the great thing about Java, and the reason it is a good *first* language, is that it takes many decisions and concerns out of the hands of the application programmer and handles them automatically. The biggest of these is memory management, especially the heap. Java's garbage collector means that you needn't concern yourself with the lifetime of objects except in extreme cases. These cases don't arise for beginners, and when they do, Java is not likely to be the best language (billions of objects existing simultaneously, for example).
  
  Still from the standpoint of the scholar, programming in C++ will force you to think at a more detailed level. This isn't ideal for the beginner, but every master programmer needs to do the *nitty-gritty* at some point, and C++ forces you there.
  
  Java is a much more *abstract* language. For example, it has only single inheritance, so using formal interfaces is much more important there. The "meaning" of code can be expressed in Java Docs for interfaces rather than in the code itself (though there is danger there, of course). If you program wisely in Java those Java Docs on the Interfaces are likely all of the "comments" that your code needs.
  
  Multiple inheritance in C++ holds many pitfalls, that are overcome with attention to detail and with a level of planning that you don't need in Java, since that alternative isn't available. Combining two superclasses as parents of a subclass can lead to poor design if not done wisely. That isn't a *stroke against* C++ but a warning that the programmer needs to develop additional skills to avoid pitfalls.
  
  Other things in C++ are more complex than in Java. Visibility, for example, and especially *friend* classes. Also *includes*, namespaces, and some lower-level things such as structs. Again, it isn't that this is bad in C++ but the programmer needs to learn how to handle a greater range of detail than in a more abstract language or in a single paradigm language. Learning how to deal with that, and learning the tools that help you do it is valuable.
  
  However, to say that C++ is more efficient than Java is wrong in general, though it may be correct in some situations. Compilers have come a long way in recent years and the efficiency of code is dependent on the quality of the compiler. In particular, if a language must be used at a detail level then the compiler has less opportunity for optimization than if it is used at a more abstract level. If my program describes *how to do something* it is quite different than if it describes *what must be done*. In the latter case, the compiler may be better able to come up with the *how to* than you are. If you describe the *how to* itself in your program, the compiler may have no option than to plod along with the steps you chose even when there might be a better path to the goal. Functional and Logic languages were once thought inefficient, but they need not be. It was a result of the level of sophistication of translation available at the time.
  
  To conclude, C++ is a good choice to follow Java as it will reveal a level of detail that the Java programmer didn't previously need to contend with. Even if you only use C++ as a *better C* you will learn a lot.
  
  But learning a language, like Prolog, say, in a completely different family will also teach you things that are quite different, not just deeper, than what you already know - orthogonal dimensions.
  
  > **Comments:**
  > **username_6**: Overall very good,  I'd add that c++ implementations of low level code like the guts of xalan/xerces xml libraries tend to be twice as fast as their java counterparts. On the other hand there are certainly places where java is comparable in throughput to c++. You might point out that the jvm is written in c/c++  
  
  ---
  
  ### Answer by username_8 on 2018-01-24. Score: 1
  
  I'm a multi-platform programmer who has spent many years using C++.
  
  In the last 5 years that has included:
  
  * The Realm mobile database engine used by a couple of **billion**
    people worldwide, embedded in their mobile apps. I mainly worked on their Xamarin SDK in C# but had to use C++ in the glue layer to the engine.
  * Using the Cocos2D-X
    game engine, for mobile games (actually for my mobile messaging app
    which has a game-like interface).
  * Working on a 3D CAD program used to
    design minesites, including their roads, open-pits, tunnels and
    explosive blast patterns.
  
  C++ is still a great language when you need to mix both high-level abstractions and compile down for really low-level performance.
  
  Once of the cooler things happening is compiling C++ to JavaScript and WebAssembly, to deliver high performance graphical web apps like the [Figma vector editor.](https://www.figma.com/)
  
  Even if they won't use C++ as a primary language, it's still important to know if you want to interface to some libraries.
  
  
  ---
  
  