views:

337

answers:

7

Currently I am working with Java and its object oriented-design aspects (I also work with PHP/MySQL a lot). I don't implement it on the web; I just use it for designing programs for general purposes. However, now I need to learn C right now, and I have an interest in C++. I got The C Programming Language, which some people say is the best book on the subject. Can someone tell me what will be the biggest challenges, except of the String handling, in the way I think about programming design, and how I create programs? I understand that I can't have classes, but how will this impact me specifically (i.e. will I have to redesign methods and always design everything with the idea that it is harder to edit)? Also, is the jump to C++ from those languages hard? Everyone says it's a really hard language, but would some previous experience help? And with that experience would Accelerated C++ be to hard of a book to start out with?

Thanks a million.

+8  A: 

If you are used to OOP, the hardest part of the transition to a non-OO language is going to be in getting adjusted to the logistics of "simulating objects" using existing mechanism. In C, this typically involves having a struct, and then having a bunch of functions that take that struct as a parameter. With C++, you can avoid this.

However, the biggest challenge of the transition to C or C++ is going to be in getting used to pointers and to memory allocation. You will undoubtedly make mistakes initially in referencing and dereferencing, and get confused about C++ references when you work with them. You will also undoubtedly cause memory leaks or errors. And since you are working "on the wire", the crashes will not be pretty. I'm not sure that there's a way to overcome these pains but practice.

Uri
+3  A: 

Don't worry one bit. I started programming with Java, then moved on to C++. And then I learned x86 assembly and now I'm into C and then I came back to use some features of C++ like objects. I even did a Java project not long ago. The order is not important, as long as you put work in learning these languages, you will have success with them.

Now, you asked about design. I think that this is the part that you will like best. In OOP, if you wanted to create a simple wrapper over a database, you would create an Object and then you could set up an inheritance system with polymorphic functions etc.

In C, you would just make a file named sqlite_wrapper.c, include #sqlite3.h and you start writing the code. You'll make a couple of functions, decide on the parameters you want to send (structures and values) and you're done.

In C you don't have classes but you have FILES. Having different files already separates the logic. In Java you have files + classes inside them to separate the logic and that's also very good.

Good luck, have fun.

toto
Thanks for the really in-depth answer. The example helped understand it a lot more.
Brian
+1  A: 

Like many others said. It is possible if you get to know the differences. At some points you have to use other ways at some points you are able to try to program into the language and not with the language e.g. "simulating" objects with structs and a pointer to the struct as first argument. If you have experience and are forced to work with it you will learn it.

Two things I want to recommend you:

  1. After learning basics from the language try to get a deep understanding for pointers and memory handling, where to use *, &, . , ->, there are important differences and I used to try around until the program seemed to work. This is a very bad idea.

  2. Think of a good way you can really test your programs use a debugger as much as possible to understand why your program is running or failing and what is happening. You can make errors that never show up but when it is important and you don't have the time they will eat you up

Janusz
+2  A: 

I went from being a 2nd year Uni -level programmer in Java (with lots of experience in other non-c like languages) to a C++ novice with about 2 hours of tutoring from a friend. Along the lines of "This is how you declare a class, here is hello world, etc".

The transition from there to C is much more painful, as C++ has most of Java's features (the biggest one missing being garbage collection). I would suggest that you look into Boost (a C++ library), especially the smart pointers. Also, time spent mastering the C++ Standard Template Library is time well spent!

Tom Leys
+4  A: 

I would like to say that it would be easier to learn C++ on the bases of Java, than on the bases of C.

Accelerated C++ would not be harder, its a beginner level book. There are a many other good books out there. Thinking in C++ can be a good start, but it might bore you down in the start with a lot of familiar stuff. So, I would suggest just go through it, and then grab Effective Series. Then Exceptional, Modern C++ will be good to go.

C++ is not that difficult, the only thing is you need to do wuite a few things on your own. The most tricky part can be memory management, as Uri stated in his post. In fact its vast and difficult to grab fully, and that might be the reason its tough.

This SO thread provides a good and important readings list for C++.

Adeel Ansari
+3  A: 

There are substantial differences between C and C++. The major one is the use of object oriented design in C++. There are also minor differences which are more of 'getting used to' type of deal than anything. Here is a few of them just to paint a general picture.

  1. Differences in dynamic memory allocation.
  2. No function overloading in C
  3. No function prototypes in C
  4. Different casting styles
  5. A more extensive library in C++.

Since you're moving from Java, OO programming should be something of a habit for you and it would be easier to transition to C++. If you absolutely have to start with C, it's no big deal. You're still going to be able to grasp the same general concepts used in both languages.

Sergey
+1  A: 

As all said, C is not very hard, and can be a nice addition to your programming arsenal.

C When compared to Java:

  • There are pointers, this allows you lower level accesses, and makes interesting bugs.
  • The language is lean - There are much less features.
  • There is no JVM to protect you with a nice throw that explains the error and line number, There is some OS support, but much limited, so you'll need to use tools.
  • No garbage collection, unless you'll use one, you'll have to take care of your memory yourself.

To sum it up: The main issue will be debugging, You'll encounter some new bugs when it comes to memory and use of pointers, and you'll have to use tools/think harder to solve it, as there is no JVM.

Liran Orevi