Programming for newbies, Part 2: C++

Last time I did a write up on Python. Here is my short treatise on another programming language, C++. Bear in mind that I’m not an expert C++ boy, but I have enough to pass knowledge to newbies. I’m still learning it and I’ll probably keep learning . :)

That being said, I will jump on very short history of C++. I won’t try to re-invert the wheel by saying what is already said: Just go to Wikipedia to get the feel of the language. Or, if you are lazy, here is a summarized version from the creator of C++ himself:

C++ is a general purpose programming language with a bias towards systems programming that

So now you have all the basics to know what C++ is all about (assuming you read it =P). So why should you learn this beast anyway? Why not stick with my good friend, gentle Python? Well Python is better than C++ in some ways, but it also has its limitations. After all, Python was designed not to replace C++ but to extend it; trying to replace C++ with Python will be doing injustice to the very purpose of Python itself.

Python has some limitations when it comes to “speed critical” applications, and high performance programming. This doesn’t mean Python cannot do it, but it means C++ will do it better, at optima speed and performance. So still we go back to the rule of thumb “Right tool for the right job“. If you need to combine advantages in both ways, don’t worry, Python is C/C++ extensible. In other words, you can do all Pythonic works in Python and just those speed critical parts in C/C++ and combine them. For those interested in trying out the extensibility for themselves, I have this as a dotTech assignment for you.

Now let us jump to simple intro to the C++ animal itself. C++ is designed to be OOP language. Also, contrary to Python, in C++ you handle all low level stuff like memory management. Of course, human freedom comes with consequences. Yes C++ is very powerful, enough to shoot yourself in the toes (yes, not toe – toes) if used improperly, but any mistake in coding C++ will backfire and ruin your application. I am sure you now understand now why I call C++ a “beast”.

Ok, to start coding in C++ you need at least two things, an editor and a compiler. The editor is for writing your code, called the source code, and the compiler is for changing source code to machine language. There are many compilers, both free and non free. The well known free compiler is the GNUGCC, from the Free Software Foundation. The famous commercial compiler is Microsoft Visual C++. It comes with Visual Studio (Visual Studio costs $$$ but students can get Visual Studio for free via DreamSpark) or you use the limited Visual C++ Express Edition for free. There are other compilers too, but I hope you know how to Google the keyword is “C++ compilers”.

First get the compiler. As a personal suggestion, I say you try MINGW, a port of GCC. You can download it and install as per these instructions. After that add the MINGW’s bin path to your system path. The Path might look like X:\MINGW\bin where X is the drive where MINGW is installed. To test if MINGW path is correct just type g++ at commandline. If you get error that g++ is not recognized as system command, check if you correctly set up your path. If it says g++ no files, or any g++ error, bravo you are ready to go!

Second part is to get an editor for writting actual code. I recommend nice editor called Notepad++ for editing. It helps to highlight words so that you can know whether you are mistakenly using a special C++ keyword. It also helps in code folding and many more goodies, including but not limited to line numbering. Grab it and install it. Nothing is convoluted about installation. Once you finish installing, you are ready to start coding!

A TASTE OF C++

Here we have our first program written in C++. It is a type of “Hello world!” program; just to give a feel of a language. I will write the code for you to copy and paste (writing it yourself is a virtue).

#include <iostream>: This preprocessor commands tells the compiler that we are going to use some functions/classes defined in this header. cout is the function in our case.

using namespace std: This instructs a compiler that we are going to use the stuffs in the std namespace so that we don’t write std::endl . If not included in your code, you will need to specify the namespace exclusively like std::endl for the end line. Not so funny!

int main(): Every C++ program (like its father C), must have at least one function, of which one must be main. Here is where program execution begins.

<<endl: Here we tell the program to start print on a new line after “Hello World” is printed. This is equivalent to the well loved “\n” for new lines. I recommend the later if you need portability.

cin.get(): Try to omit this and see. Your program starts and disappears. This enables program to pause until you type something and/or press return key. It just takes input and outputs no where. :)

return 0: This tell program to exit gracefully.

COMPILE THE PROGRAM

After writing (didn’t you copy and paste?) your code, save it somewhere as helloworld.cpp, then open your terminal. If you are completely new to MINGW, then here are instructions for you. Just move to the directory using your terminal (whether cmd.exe or bash) and in that directory type:

g++ helloworld.cpp -o helloworld.exe

If correctly done (and system path, remember?), then you will see the exe file. (We assume you are working with Windows.) Just run your executable file to see your program in action! Bravo, you have just compiled your first C++ program!!

APPLICATIONS WRITTEN IN C++

Many newbies loves to get a taste of what a given stuff can do. As said above, C++ is very capable. Here are some links to a list of applications written in C++. I’m lazy too to collect them by myself, so here are people who took the trouble to do so:

So, you now have this introduction, and maybe you are thinking of driving Adobe or Microsoft out of business by your superb, low cost applications in future. That is possible and you can do it. C++ is painful in learning (error in 1 line and compiler throws gazillions of errors). But once you grasp it, you will really be happy with errors and warnings. They will be best friends (believe me, it is easy to debug program with errors/warning than one that just don’t work).

So practice until you become familiar. You can join open source projects to improve your skills (Firefox, anyone?) or you can start your own. You can make a chat program and improve it until Pidgin and MS messenger are out of business, or just code your media player that will be top download in all major download. Remember, every journey begins with first step, so jump in the wagon and let’s go!

Related Posts

Subscribe to dotTech to never miss an article again!

About Stephen

You probably know that Stephen means crowned with glory eh! Yes, that is all I am. I'm a Electronics and Telecommunication Engineer, as well as an ICT guy. If you want to know me more, I'm a Christian Minister too. You'll not find me posting things of faith here because you read the URL right, a tech site....I love dotTech.org what about you??

I really appreciate your programming articles, which seem to be focusing on the very languages that I am interested in learning. If you need any suggestions for future selections, you might try Curl, Scheme, C# and/or Java, and Ruby just to name some possibilities

@Emrys If you plan to switch to *nix, then go for Mandriva or ubuntu. I walk with mandriva on external HDD, and I have used ubuntu. They are very nice.

If you plan to use windows, I recommend XP as very stable. Vista is said to have some issues (Apart from windows update failure I have no problem). I havent tried Win7 so I cannot comment. MINGW is available for download. Also you can get it with IDEs like wxDevcpp, Code::Blocks or codelite

@Samuel, Thanks for encouraging word. I love to post these stuffs but as alot of dotTechies here, I lack time to do so. Wherever I get time, I’ll keep posting. I’m waiting to see your post on VB.Net and more :)

@Rob: That is true, but most people have the .NET Framework making .NET based programs worth using. Though you could skip all this by coding native apps in things like C++ :D

Rob

VB6 is so much simpler (easier)

Also The runtimes are already present in XP, Vista, Windows 7 So you do not have to install most of your programs (if you avoid dependencies like I do). You can just send the user an exe, and it will run.

@Emrys: What I would go for next is to decide what platform do you want to program for? Windows? Mac? Linux? For Windows you might want to ues the Visual Studio Editor/Compiler since it gives you the ability to use the .NET framework. For Mac, I believe what they call Coco is just a renamed C++ with some add-ons. Linux I can’t say but there is probaly some special way to code for it too.

@M.D. Stephen: very good intro to c++, I personally can only read C++, and thats mostly to convert it to VB and/or C#. And I really must get working on my VB article…

Emrys

If anyone has a good idea where to go from here, please let me know; Picard, that means you too… :D

Emrys

I think this is all beyond me. I feel proud to have replaced my expired Win7 7600 with the older RC Win7 Enterprise and got my system up. Giggles for me!!!