5
$\begingroup$

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].

$\endgroup$
2
  • 2
    $\begingroup$ 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. $\endgroup$
    – ivanivan
    Dec 31, 2017 at 5:28
  • $\begingroup$ 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. $\endgroup$ Jan 1, 2018 at 16:18

3 Answers 3

10
$\begingroup$

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.

$\endgroup$
6
  • 2
    $\begingroup$ "the similarities between C++ and Java are fairly shallow and the differences are very deep." Thanks for saying that so eloquently. $\endgroup$
    – Guy Coder
    Dec 31, 2017 at 0:32
  • $\begingroup$ "Your own experience with C++ seems shallow from the statement of your question." Thumbs up. $\endgroup$
    – Guy Coder
    Dec 31, 2017 at 0:34
  • $\begingroup$ 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. $\endgroup$
    – Ben I.
    Dec 31, 2017 at 5:19
  • $\begingroup$ I'd stay clear from saying things like "your experience seems shallow". It can be quite offensive. $\endgroup$
    – ItamarG3
    Dec 31, 2017 at 7:26
  • 1
    $\begingroup$ 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. $\endgroup$
    – Buffy
    Jan 1, 2018 at 0:21
4
$\begingroup$

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 <algorithm> - 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!

$\endgroup$
2
$\begingroup$

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<float,1,0,0,0,0>;
typedef Time     Dimension<float,0,1,0,0,0>;
typedef Speed    Dimension<float,1,-1,0,0,0>;
typedef Area     Dimension<float,2,0,0,0,0>;
typedef Volume   Dimension<float,3,0,0,0,0>;

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<T,d1+d2,t1+t2,x1+x2,y1+y2,z1+z2> operator*(
    template<T,d1,t1,x1,y1,z1> a, 
    template<T,d2,t2,x2,y2,z2> b );

template<T,d,t,x,y,z> operator+(
    template<T,d,t,x,y,z> a, 
    template<T,d,t,x,y,z> 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).

$\endgroup$
2
  • 1
    $\begingroup$ "that checks at compile time, that your types are dimensionality correct" F# has this. See: Units of Measure and Are units of measurement unique to F#? $\endgroup$
    – Guy Coder
    Dec 31, 2017 at 13:47
  • $\begingroup$ @GuyCoder and that question links to an article that mentions several others, as do some of the answers (though not all statically checked). $\endgroup$ Jan 1, 2018 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.