Jon offered to help people learn C++, and I thought that I’d comment a little bit on what skills we’re going to need to teach people, relative to what they already know. I was working with our undergraduate researcher, Eric, and he showed me some projects from his basic programming class. I’ll use this info to separate what is taught in an introductory engineering class with some additional skills that are helpful for our work.
See also: Matt’s Thoughts on C++
In both Eric’s and my undergraduate class, we were effectively taught a version of C with C++ syntax. We weren’t fully taught C++, because we didn’t learn the object-oriented programming paradigm, but we did learn about C++ style I/O (i.e., cout), the ability to declare variables after performing calculations in a function, and declaring iterators inside a loop (i.e., (for int i = 0; i < N; i++)). Specifically, the following fundamental skills are typically taught:
- Control structures – while, for, if, switch
- Scalar variable types – int, double, string
- Functions – how to declare them, how to call them
- Basic input/output, from files and from the console
- How to compile codes where all source code is found in one file
- Statically-allocated 2d arrays
All this is good knowledge and is the fundamental stuff for writing programs such as ours.
More Advanced Topics for Research Programming
Dynamically Allocated Arrays Statically allocated arrays have their size determined at compilation time. Most applications, though, require the user to set the size of an array or other data structure. Arrays allocated with new and delete and C++ vectors allow us to do that.
Command Line Parameters Typically, really important program options such as a parameter file, the number of realizations, and whether or not you want to output to a file are passed through the command line. These parameters can be handled manually or using a function like getopt.
Source Code in More than One File, and Makefiles Larger projects cannot simply be compiled using a single source file. Makefiles, and projects using other development environments, allow you to organize your code across multiple source files and compile it using a single command.
Classes and Structures Fully object-oriented C++ programming abstracts all program elements into “objects”, that can contain functions and data structures in their own right. For example, a “boxcar” class could contain the weight of the boxcar, its height, and functions to calculate its volume or how much it can carry. Not many of our codes are fully object-oriented, but it helps to know how to do some of this for certain situations. We can elaborate on that in a future post.
Template Functions and Overloading C++ allows you to create two versions of the same function that accept different arguments. You may define a “sum” function for ints, and another for doubles, for example. You should know how to do this, and you can also experiment with template functions, which allow you to pass in a generic template for function parameters instead of overloading a function 10 times to consider all the different versions of function parameters possible in your application.
Learning Your Own Coding Style Sometimes there’s a number of ways to skin a cat. For example, you may need to use a goto statement, whereas other times a goto statement is highly inappropriate. With experience, you’ll learn certain “tricks” that will make your life easier.
For my fellow “trainers”: Are there more skills that need to be taught? How is the best way to go about it?
For the “trainees”: The situation isn’t as bad as it would look at first. A lot of these skills aren’t that hard to pick up, especially given good examples. In the coming days I may post some examples of some of the more straightforward things, such as a template function you can use in your own program. In the meantime, you can browse http://www.cplusplus.com/ to learn more about these language features.