<p>2 -3 hours?? That's a really tight timeline to cover:</p>

<ul>
<li>header files, compile, link and build tools</li>
<li>stack semantics and RAII (including stack unwinding during exceptions)</li>
<li>references and how they are not like Java references</li>
<li>pointers and how they are not like Java references</li>
<li>smart pointers for when you have to use the heap</li>
<li>templates including template specialization and variadic templates</li>
<li>operator overloading and why it's important</li>
<li>the Standard Library including collections, iterators, and all of <code>&lt;algorithm&gt;</code> - I need 8-10 hours just for this</li>
<li>multiple inheritance, private inheritance, virtual inheritance, friend</li>
<li>const, noexcept, constexpr, constexpr if</li>
<li>idioms like Pimpl, and why you use them</li>
<li>not to mention the nontrivial problem of how to get stuff to show on the screen. You might want to choose a graphics library to teach. Or a windowing framework (good luck with that.) Or do like most of us and use console apps all term long. Sigh.</li>
</ul>

<p>Trust me, you have more than a term's worth of stuff to cover!</p>

CC BY-SA 3.0
2018-01-08T01:08:46.753Z
2018-01-08T01:08:46.753Z
Kate Gregory
<p>I worked on a professional project. We chose C++, as we could see that there were a lot of C++ programmers out there. By a short way in, we realised that we were wrong. There are a lot of programmers that say they are C++ programmers, but most are incompetent. Not because of them selves, but because of the language. It took a lot of training to get them and ourselves up to speed.</p>
<h1>Many to many relationship of features.</h1>
<ul>
<li>Each C++ feature can be used to implement many different OO concepts.</li>
<li>Each OO concept can be implemented in more than one C++ way.</li>
</ul>
<h1>What C++ can do that other languages can not.</h1>
<h2>Myth</h2>
<p>C++ is a good, fast OO language. This is not true. Eiffel is as fast, safer, and more OO than Java.</p>
<h2>Fact</h2>
<p>C++ is the only language, I know of, where you can create a statically checked dimensions checker.</p>
<p>That is a system, that checks at compile time, that your types are dimensionality correct. E.g.</p>
<pre><code>Distance distance = 120*mile;
Time time = 3*hour;
Speed speed = distance/time; //This is ok
Distance speed2 = distance/time; //This is not
</code></pre>
<p>It does this using templates</p>
<pre><code>typedef Distance Dimension&lt;float,1,0,0,0,0&gt;;
typedef Time     Dimension&lt;float,0,1,0,0,0&gt;;
typedef Speed    Dimension&lt;float,1,-1,0,0,0&gt;;
typedef Area     Dimension&lt;float,2,0,0,0,0&gt;;
typedef Volume   Dimension&lt;float,3,0,0,0,0&gt;;
</code></pre>
<p>Where Dimension is defined else where. It is a bit complex, so look it up.
It is called the Barton–Nackman trick.</p>
<p>It may include something like this (My C++ is rusty, so may be errors), I think it involves friends, and other complex feature. It took us a while to get it right, but then we just left this bit alone. It became a sealed package of functionality:</p>
<pre><code>template&lt;T,d1+d2,t1+t2,x1+x2,y1+y2,z1+z2&gt; operator*(
    template&lt;T,d1,t1,x1,y1,z1&gt; a, 
    template&lt;T,d2,t2,x2,y2,z2&gt; b );

template&lt;T,d,t,x,y,z&gt; operator+(
    template&lt;T,d,t,x,y,z&gt; a, 
    template&lt;T,d,t,x,y,z&gt; b );
</code></pre>
<p>Warning: Wikipedia says that this was removed from the language in C++11, but may be back in C++20.</p>
<p>It is interesting as this is the only place I have seen this type of checking, most other languages insist, incorrectly that the operators and return type of a multiply are all the same type. I would like to see it in other languages, but with much less complexity (though all of the complexity can be abstracted into a library, using a feature that has no other use).</p>

CC BY-SA 3.0
2017-12-31T13:47:29.590Z
"that checks at compile time, that your types are dimensionality correct" F# has this. See: [Units of Measure](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure) and [Are units of measurement unique to F#?](https://stackoverflow.com/q/107243/1243762)
Guy Coder
CC BY-SA 3.0
2018-01-01T10:02:28.550Z
@GuyCoder and that question links to an article that mentions several others, as do some of the answers (though not all statically checked).
ctrl-alt-delor
CC BY-SA 3.0
2017-12-31T10:04:02.960Z
2017-12-31T10:04:02.960Z
2020-06-18T08:36:57.023Z
ctrl-alt-delor
<p>Actually, the similarities between C++ and Java are fairly shallow and the differences are very deep. The syntax of both is derived from C, but the underlying ideas are very different.</p>

<p>The biggest difference is that an "object" in C++ is built on the stack, like a struct, not on the heap. It takes additional work and a lot of understanding to become comfortable with the differences. If you do create objects on the heap (with <code>new</code>) you need to understand the differences between pointers and references and these can be the source of hard to debug errors. And you need to be absolutely pure with deallocation. Every object needs a known lifetime to the programmer, whereas Java handles all of that automatically. </p>

<p>C++ has no garbage collector for heap objects, so the memory management problem needs to be solved for every application, unlike Java where it is solved once by the run-time system automatically. Smart pointers in C++ can help a bit with this but are not a complete solution. Memory leakage for non-trivial applications is a serious issue. </p>

<p>The visibility of things is also fundamentally different, with "friend" classes complicating the mental models of complex systems. </p>

<p>Don't underestimate the difficulties. You will likely have a lot of work to do to get the students comfortable with quite different mental models and with practices and idioms that keep them safe. </p>

<p>James Gosling once described Java as C++ without the guns, knives and clubs. You will be trying to program with all of those pitfalls, when the students, coming from Java, have little experience with them. </p>

<p>Their experience with C, if deep enough, will help with the memory management issue, but not with the complex (i.e. dual) object model of C++. You should become familiar with good memory management tools and practices if you want to do non-trivial things in C++. </p>

<p>I was once stopped dead in a C++ project by a deallocation error for 24 hours. The solution required a 1 character change in a file. One character of progress per day is very bad, though I was an experienced programmer and actually knew why it was failing. </p>

<p>Your own experience with C++ seems shallow from the statement of your question. I'd suggest that you work <em>hard</em> on that before you attempt to teach such a course. Especially, build something that requires sophisticated memory management and test everything as you go. You can't think of C++ as just mis-spelled Java, nor Java as just mis-spelled C++. The conceptual differences are many and deep. </p>

<p>My own experience as a programmer (and author)` is that Java helps me program correctly, but C++ fights me constantly for every inch of ground. I could build some beautiful things, but the fight was constant and brutal. </p>

CC BY-SA 3.0
2017-12-31T00:32:08.157Z
"the similarities between C++ and Java are fairly shallow and the differences are very deep." Thanks for saying that so eloquently.
Guy Coder
CC BY-SA 3.0
2017-12-31T00:34:50.057Z
"Your own experience with C++ seems shallow from the statement of your question." Thumbs up.
Guy Coder
CC BY-SA 3.0
2017-12-31T05:19:00.323Z
I feel like I learned a tremendous amount from this answer.  I would have mentioned garbage collection and pointer referencing/referencing, but my knowledge of C++ is also fairly shallow.  This is a much deeper answer.
Ben I.
CC BY-SA 3.0
2017-12-31T07:26:14.760Z
I'd stay clear from saying things like "your experience seems shallow". It can be quite offensive.
ItamarG3
CC BY-SA 3.0
2017-12-31T09:24:54.397Z
Thanks for the informative answer. I understand that C++ features "guns knives and clubs" that make it much more difficult to master. To teach this I need projects that actually require the use of such features... while there are endless ideas for software projects,  many of them can be built using only the standard c++ libraries that handle all memory issues for you. What are some examples of projects that do require the sophisticated manual memory management of c++?
Erel Segal-Halevi
CC BY-SA 3.0
2018-01-01T00:21:16.710Z
The easy way for you to come up with a project to practice on yourself is to take one of the more sophisticated Java course projects and re-do it in C++. But try to do it with C++ idioms, rather than Java idioms.
Buffy
CC BY-SA 3.0
2017-12-30T23:10:09.947Z
2017-12-31T09:31:23.033Z
2017-12-31T09:31:23.033Z
Buffy
<p>I am now teaching an OOP course to 2nd year undergrads. This is actually a course in advanced programming in Java. In the next semester, I am going to teach the same students, a C++ course. From what I know, C++ and Java are quite similar - there are differences, but they can be covered in 2-3 hours, which are one week of teaching. This leaves me 12 more weeks, and I wonder what else to teach.</p>

<p>I looked at the syllabus from previous year and it looks quite similar to my current Java course: OOP, overloading, generics, unit-testing, software design, etc. I don't want to teach the same course twice in different languages...</p>

<p>One idea that I had is to focus, not on teaching the language itself but on teaching specific practical applications in which C++ has a clear competitive advantage. For example, if game-programming is usually done in C++, then I can focus on game-programming. So my first question is: what kind of applications are written in C++ substantially more than in Java?</p>

<p>And my second question: what other new topics can I include in this C++ course?</p>

<p>[NOTE: in case this is relevant, the same students currently take a course in C].</p>

CC BY-SA 3.0
2017-12-31T05:28:18.980Z
Even if the differences were trivial - and they aren't - you still have a different tool set, tool chain, debugging methods. And the library ecosystem is different as well.  You won't have issues wtih basic programming concepts - loops, condition checks, data types etc but there is so much more to deal with.
ivanivan
CC BY-SA 3.0
2018-01-01T16:18:33.103Z
`C++`, has multiple inheritance. Few modern languages have this, even fewer do it properly (Eiffel being an exception). Take some inheritance structure, that can also be comparable. Implement it, so that you implement each of `<`, `>`, `<=`, `>=`, `=`, `!=`. You can implement them all in terms or `<` in the base class, then the inherited class only has to implement `<`. No `compare(a:T, b:T): int{-1,0,1}`. Note multiple inheritance is safe, but repeated inheritance is not, and must be handled correctly.
ctrl-alt-delor
<curriculum-design><java><undergraduate><c>
Teaching C++ to students that know Java
# Teaching C++ to students that know Java

(Asked by: username_0 on 2017-12-30)

I am now teaching an OOP course to 2nd year undergrads. This is actually a course in advanced programming in Java. In the next semester, I am going to teach the same students, a C++ course. From what I know, C++ and Java are quite similar - there are differences, but they can be covered in 2-3 hours, which are one week of teaching. This leaves me 12 more weeks, and I wonder what else to teach.

I looked at the syllabus from previous year and it looks quite similar to my current Java course: OOP, overloading, generics, unit-testing, software design, etc. I don't want to teach the same course twice in different languages...

One idea that I had is to focus, not on teaching the language itself but on teaching specific practical applications in which C++ has a clear competitive advantage. For example, if game-programming is usually done in C++, then I can focus on game-programming. So my first question is: what kind of applications are written in C++ substantially more than in Java?

And my second question: what other new topics can I include in this C++ course?

[NOTE: in case this is relevant, the same students currently take a course in C].

**Tags:** curriculum-design, java, undergraduate, c

> **Comments:**
> **username_1**: Even if the differences were trivial - and they aren't - you still have a different tool set, tool chain, debugging methods. And the library ecosystem is different as well.  You won't have issues wtih basic programming concepts - loops, condition checks, data types etc but there is so much more to deal with.  
> **username_2**: `C++`, has multiple inheritance. Few modern languages have this, even fewer do it properly (Eiffel being an exception). Take some inheritance structure, that can also be comparable. Implement it, so that you implement each of `<`, `>`, `<=`, `>=`, `=`, `!=`. You can implement them all in terms or `<` in the base class, then the inherited class only has to implement `<`. No `compare(a:T, b:T): int{-1,0,1}`. Note multiple inheritance is safe, but repeated inheritance is not, and must be handled correctly.  

---

## 3 answers

### Answer by username_5 on 2017-12-30. Score: 11 | Accepted answer

Actually, the similarities between C++ and Java are fairly shallow and the differences are very deep. The syntax of both is derived from C, but the underlying ideas are very different.

The biggest difference is that an "object" in C++ is built on the stack, like a struct, not on the heap. It takes additional work and a lot of understanding to become comfortable with the differences. If you do create objects on the heap (with `new`) you need to understand the differences between pointers and references and these can be the source of hard to debug errors. And you need to be absolutely pure with deallocation. Every object needs a known lifetime to the programmer, whereas Java handles all of that automatically.

C++ has no garbage collector for heap objects, so the memory management problem needs to be solved for every application, unlike Java where it is solved once by the run-time system automatically. Smart pointers in C++ can help a bit with this but are not a complete solution. Memory leakage for non-trivial applications is a serious issue.

The visibility of things is also fundamentally different, with "friend" classes complicating the mental models of complex systems.

Don't underestimate the difficulties. You will likely have a lot of work to do to get the students comfortable with quite different mental models and with practices and idioms that keep them safe.

James Gosling once described Java as C++ without the guns, knives and clubs. You will be trying to program with all of those pitfalls, when the students, coming from Java, have little experience with them.

Their experience with C, if deep enough, will help with the memory management issue, but not with the complex (i.e. dual) object model of C++. You should become familiar with good memory management tools and practices if you want to do non-trivial things in C++.

I was once stopped dead in a C++ project by a deallocation error for 24 hours. The solution required a 1 character change in a file. One character of progress per day is very bad, though I was an experienced programmer and actually knew why it was failing.

Your own experience with C++ seems shallow from the statement of your question. I'd suggest that you work *hard* on that before you attempt to teach such a course. Especially, build something that requires sophisticated memory management and test everything as you go. You can't think of C++ as just mis-spelled Java, nor Java as just mis-spelled C++. The conceptual differences are many and deep.

My own experience as a programmer (and author)` is that Java helps me program correctly, but C++ fights me constantly for every inch of ground. I could build some beautiful things, but the fight was constant and brutal.

> **Comments:**
> **username_4**: "the similarities between C++ and Java are fairly shallow and the differences are very deep." Thanks for saying that so eloquently.  
> **username_4**: "Your own experience with C++ seems shallow from the statement of your question." Thumbs up.  
> **username_6**: I feel like I learned a tremendous amount from this answer.  I would have mentioned garbage collection and pointer referencing/referencing, but my knowledge of C++ is also fairly shallow.  This is a much deeper answer.  
> **username_7**: I'd stay clear from saying things like "your experience seems shallow". It can be quite offensive.  
> **username_0**: Thanks for the informative answer. I understand that C++ features "guns knives and clubs" that make it much more difficult to master. To teach this I need projects that actually require the use of such features... while there are endless ideas for software projects,  many of them can be built using only the standard c++ libraries that handle all memory issues for you. What are some examples of projects that do require the sophisticated manual memory management of c++?  
> **username_5**: The easy way for you to come up with a project to practice on yourself is to take one of the more sophisticated Java course projects and re-do it in C++. But try to do it with C++ idioms, rather than Java idioms.  

---

### Answer by username_3 on 2018-01-08. Score: 4

2 -3 hours?? That's a really tight timeline to cover:

* header files, compile, link and build tools
* stack semantics and RAII (including stack unwinding during exceptions)
* references and how they are not like Java references
* pointers and how they are not like Java references
* smart pointers for when you have to use the heap
* templates including template specialization and variadic templates
* operator overloading and why it's important
* the Standard Library including collections, iterators, and all of  - I need 8-10 hours just for this
* multiple inheritance, private inheritance, virtual inheritance, friend
* const, noexcept, constexpr, constexpr if
* idioms like Pimpl, and why you use them
* not to mention the nontrivial problem of how to get stuff to show on the screen. You might want to choose a graphics library to teach. Or a windowing framework (good luck with that.) Or do like most of us and use console apps all term long. Sigh.

Trust me, you have more than a term's worth of stuff to cover!


---

### Answer by username_2 on 2017-12-31. Score: 2

I worked on a professional project. We chose C++, as we could see that there were a lot of C++ programmers out there. By a short way in, we realised that we were wrong. There are a lot of programmers that say they are C++ programmers, but most are incompetent. Not because of them selves, but because of the language. It took a lot of training to get them and ourselves up to speed.
Many to many relationship of features.

Each C++ feature can be used to implement many different OO concepts.
Each OO concept can be implemented in more than one C++ way.

What C++ can do that other languages can not.
Myth
C++ is a good, fast OO language. This is not true. Eiffel is as fast, safer, and more OO than Java.
Fact
C++ is the only language, I know of, where you can create a statically checked dimensions checker.
That is a system, that checks at compile time, that your types are dimensionality correct. E.g.
Distance distance = 120*mile;
Time time = 3*hour;
Speed speed = distance/time; //This is ok
Distance speed2 = distance/time; //This is not

It does this using templates
typedef Distance Dimension;
typedef Time     Dimension;
typedef Speed    Dimension;
typedef Area     Dimension;
typedef Volume   Dimension;

Where Dimension is defined else where. It is a bit complex, so look it up.
It is called the Barton–Nackman trick.
It may include something like this (My C++ is rusty, so may be errors), I think it involves friends, and other complex feature. It took us a while to get it right, but then we just left this bit alone. It became a sealed package of functionality:
template operator*(
    template a, 
    template b );

template operator+(
    template a, 
    template b );

Warning: Wikipedia says that this was removed from the language in C++11, but may be back in C++20.
It is interesting as this is the only place I have seen this type of checking, most other languages insist, incorrectly that the operators and return type of a multiply are all the same type. I would like to see it in other languages, but with much less complexity (though all of the complexity can be abstracted into a library, using a feature that has no other use).

> **Comments:**
> **username_4**: "that checks at compile time, that your types are dimensionality correct" F# has this. See: [Units of Measure](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure) and [Are units of measurement unique to F#?](https://stackoverflow.com/q/107243/1243762)  
> **username_2**: @GuyCoder and that question links to an article that mentions several others, as do some of the answers (though not all statically checked).  

---

