What I learned while learning C++

C++ is regarded by many as one of the most difficult programming languages to become proficient in. After spending the past couple of months learning and writing a lot of C++ I am positive that while I quite like the language, it is definitely not a good first language for someone just starting out. There’s a reason a lot of first year computer science courses lean towards teaching Python and Java, leaving C++ for later, when students have a good grip on the fundamentals of programming.

One of the things I’ve discovered since I started learning C++ by myself online, is that there are many, many websites out there with tutorials and guides to the language. They looked professionally done and I was happy to use them. However, after I wrote my first tutorial on the C++ language and asked others for feedback on reddit, it was pointed out to me that a lot of C++ resources online can be misleading to learners. I’m not sure if its due to the complexity of the language or the authors’ of such tutorials wanting to keep things simple, but the result is a lot of tutorials online encourage bad practice, and even code that is not compliant with the C++11 standard.

What do I mean by this? Well, I’ll give you an example. When I initially wrote my tutorial on C++, I used the string data type like this:

#include <iostream>
using namespace std;
 
int main()
{
    string sentence = "This is a sentence";
    cout << sentence << endl;
    return 0;
}

I had learned this from many of the tutorials I had gone through, and as the code compiled with error, I believed that it was completely correct. In reality, there is one mistake and an example of bad practice in the code, which should instead look like this:

#include <iostream>
#include <string> 
int main()
{
    std::string sentence = "This is a sentence";
    std::cout << sentence << std::endl;
    return 0;
}

Adding “using namespace std” to the program is an example of bad practice as it defeats the purpose of having namespaces. It might not be a big deal with this tiny program, but once you start writing bigger and bigger programs, you’ll find that removing this line of code will help you avoid conflicts with names, especially once you start working with a lot of large libraries.

Now, the mistake in the program was not to add “#include <string>”. Although the program still worked without <string>, this was because my g++ compiler <iostream> header included <string> automatically. However, this is not a standard that C++ compilers have to abide by, and will get you into trouble if you attempt to compile the program on other computers which have less-forgiving compilers.

I’ve since added the <string> header to my C++ tutorial and am planning a major rewrite of that post to take into account all the feedback and corrections I received.

The following two tabs change content below.
Computational biology PhD candidate at the Australian National University. I love writing (both articles and software), learning more about the world around us, and beekeeping. I also write for BioSky.co

Latest posts by Jack Simpson (see all)

Comments are closed.