Sunday, April 8, 2012

Learning C++ after Java - My experience

As I have stated in previous posts and on the other pages of this blog, I attended a course at my community college to learn the Java programming language. Java is object oriented, allowing for inheritance, polymorphism, and encapsulation. However, after a while of programming in Java, I decided that the inefficient way that it executed would be too much of a burden when I started to write larger programs (and I was starting; one reached about 4000 lines). I began to learn another language: python. Python is a scripting language that has some of the same disadvantages of Java, so after I learned some of the key principles, I began to teach myself my first compiled language: C++.

C++ is object oriented as well. In fact, when it was first created it was called "C with classes". So, using online documentation I began to write a few simple programs in C++.

The C++ Hello World
#include <iostream>
using namespace std;
int main(){
cout << "Hello, world!"<<endl;
return 0;
}

As a reader of this article, you are probably a Java programmer interested in knowing how hard switching to C++ is. Java is designed to be similar to C++, so some may think that it is an easy transition; and it is.

Similarities
The syntax between the two languages is pretty similar. if, switch, int, double, float, short, void, etc. are all the same. This makes porting Java programs into C++ a pretty easy task. Most things that java can do without an import that are not in C++ can also be added with the #include directive. Cstdlib and boost are two other libraries that will provide enough functions to cover pretty much any usage that you will need for common tasks that you may have used in Java.


Differences
In Java, functions (methods) are called in such a fashion as class.func(arg); In C++, functions can be called that way from classes, structures, and unions, but they are commonly called by themselves, like this: func(arg). For functions following this format, it may be difficult to organize functions, which is why C++ offers class support. Also, C++ allows for pass-by-reference with pointers, and the syntax of pointers tend to really confuse newcomers (it confused be a lot. The video below really helps with them.


Also, Java includes its own graphical toolkit. In C++ a third-party library like GTK+ or Qt must be used.

It is also my opinion that no C++ IDE compares to the usefulness of a Java IDE. Eclipse and Netbeans, for example, are huge projects that make development a fun and easy task. With C++ I use Code::Blocks but many stick to vim and their compiler.
My personal experience
At first, it seemed like smooth sailing. However, when I started to do some of the more complicated things (changing parameter values in functions, for instance) I ran into some issues. Pointers were a really hard thing for me to grasp, and I could not see why they were needed. Aside from pointers and the god awful mess that seemed to be  the C++ library, it has been a surprisingly easy transition from Java to C++ and anybody looking to attempt it should have no trouble. To travel from C++ to Java, you will probably have no issues whatsoever, other than finding the equivalents to  functions in the C++ library.

1 comment:

  1. This is the information that I was looking for and let me tell you one thing that is it is very useful for who is looking for Java Online Training.

    ReplyDelete