Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ teaching

  1. Apr 1, 2017 #1


    User Avatar
    Gold Member

    Hi people,
    This semester I am going to be a tutor for a master course class on C++ in HEP...
    I would like to listen from people with more expertise on the field:
    1. What do students find (in general) difficult to understand in C++ from your experience with teaching it? In my case I think the most difficult part in C++ was the use of references and pointers -at certain points I am still feeling uncomfortable with using them. So I am trying to refresh, even theoretically, the whole point of refs and pointers (having to do with memory treatments of the computer). What are the most common , you think, questions one might receive on the language?
    This could as well deserve to be in Academic Guidance, but I'm also aiming for CS experts. Nevertheless, mods feel free to move it if you find it necessary..
  2. jcsd
  3. Apr 1, 2017 #2

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I tend to stay away from pointers, still get confused using them after all these years.
  4. Apr 1, 2017 #3


    User Avatar
    Gold Member

    I agree w/ you that pointers are one of the hardest, if not the hardest, things for C programmers to really grasp. As for C++, the whole trio of inheritance, encapsulation and abstraction are fairly easy to grasp as concepts but learning how to USE them in effective ways is a whole 'nother story and can be taught successfully via books and classrooms about as well as it can be taught that way how to ride a bike.
  5. Apr 1, 2017 #4


    Staff: Mentor

    The notion of method signatures, name mangling of method names, and method overloading and when it's good to use.

    The bad idea of overloading operators in such a way as to make them confusing to use. A common use was to overload the plus operator for strings concatenation but some folks said was a mistake since it string concatenation isn't commutative. Most of us though thought the argument was nonsense with respect to strings but made good sense for other operator usage,

    The bad idea of multiple inheritance and how it gets implemented in C++ where a class inherits from two parent classes who in turn inherit from the same grandparent class aka the diamond problem giving you two paths to the grandparent member variables which can give you erroneous results and a debugging nightmare. I'm not sure if this is a feature of C++ or vendor specific.


    The gotchas book might be good to find things that students will get wrong:



    And the Effective C++ by Scott Meyers is good too:


    And lastly design patterns and when they are good to use:
  6. Apr 1, 2017 #5


    Staff: Mentor

    I will be teaching a community college course in C++ starting Monday. This course is the first in a series of three classes, so we just touch on classes at the end of the quarter. Except for the use of the cout and cin streams instead of the C standard library functions, printf() and scanf() and friends, what we'll be doing in this class is not much different from C.

    I agree that students have a difficult time with pointers and references, but I don't agree with the advice to stay away from them. Because C and C++ are call-by-value languages, the only way that a function in these languages can modify its parameters is to make them either pointers or references, which are effectively the same things but with slightly different syntax. I don't see how anyone can really get how a function can modify its parameters without understanding how pointers (or references) work.

    One way to help get across a good understanding of this mechanism is to show many examples throughout a long stretch of the quarter. A technique that seems helpful to me is to use a debugger and single-step through the function and watch what changes.

    Something else that I think is helpful is an image that shows the relationship between a pointer and the memory it points to. In the following image I use something I came up with many years ago that I have seen more recently in a few books.
    The variables, in green boxes, have three things: an identifier, the address in memory of the variable, and the value of the variable. When a pointer variable is initialized with a valid address, the pointer can access the memory at that address, either reading from it or writing to it.
  7. Apr 1, 2017 #6
    Much depends on how you plan to teach C++. It has often been taught as "C with extra stuff", that is, starting with C style. There is an obvious historical basis for this, both in the language's evolution and in the prior experience of those teaching it. Modern C++ has changed very considerably, especially starting with 11, resulting in very different-looking solutions to basic problems.

    You gave the example of pointers. There is no reason for a beginner to use raw pointers in a modern C++ program except as part of a learning exercise. Code using new and delete is a good indicator of obsolete treatment, again with the exception of historically-oriented learning exercises, and with the exception of the kind of library code beginners won't be writing. The raw pointer is heavily overloaded semantically, and there is always a more specific alternative available that gives the compiler more of a chance to prove the correctness of your code.
  8. Apr 1, 2017 #7


    User Avatar
    Gold Member

  9. Apr 2, 2017 #8
    Roughly yes. It gives more opportunity for error. RAII is preferred in modern C++. In general, we create objects by value, and their destruction is guaranteed when they pass out of scope. When an object must live beyond the lifetime of the scope in which it was constructed, std::unique_ptr and friends are used. For every use of new, you must know exactly when, where, and by whom the delete will be performed. It is a surmountable problem that C programmers have managed for several decades, but it is a source of error nonetheless.

    If you are teaching a beginner course, these details will have no context. I am not a professional teacher and take no position on what you ought to teach. I only mention it at all because it can affect the style and content of a beginner course from very early on. There is also the question of what you are teaching: the C++ language, or programming, for which latter purpose you happen to be using C++. In a general course on programming, it makes sense to introduce the pointer concept. In a course on writing modern C++, they can be left out for quite a while.
  10. Apr 2, 2017 #9


    User Avatar

    Staff: Mentor

    I used to teach a two-semester introductory programming course using C++, most recently about 12 years ago. I ended up using pure C++ style, e.g.:
    • stream I/O using cin, cout, fstreams, <<, >>, getline, etc.; not C-style printf(), scanf(), etc.
    • std::string and its member functions for text manipulation, not C-style char* strings and their associated functions.
    • std::vector instead of C-style arrays
    • passing in/out arguments to functions using references, not pointers
    The last few years, I ended up not doing anything with pointers at all. At that level, after introducing the C++ features listed above, I found it very difficult to come up with non-hokey situations that required pointers. The course did not include linked lists, trees, etc. Also, the guy who taught our upper-level computer science courses told me that he preferred that I cover basic object-oriented programming (i.e. class design and implementation) instead of pointers, because he covered pointers in his courses anyway (e.g. computer architecture and data structures).

    This course was for undergraduates who usually did not know much if anything about programming at all, to begin with. If your students have a significant amount of programming experience already, that may indicate a different approach.
    Last edited: Apr 2, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?