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 GNU GCC, 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).
using namespace std;
Here is what is happening:
#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.
cout<<”Hello world”: Here we print strings “Hello World” to the standard output (mainly your display). Nothing bigger than that.
<<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:
RESOURCES FOR LEARNING C++
- Thinking in C++
- A Beginner C++
- Teach Yourself C++ in 21 days
- Compilers and compiler generators: an introduction to C++
- Data Structures and Algorithms with Object-Oriented Design Patterns in C++
- C++ Anotations
- Superb introduction with PDF download available
- Another word class tutorial collection
- C++ reference
- Another reference
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!