Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I learned Java and I'm pretty competent programming in it. I also learned C recently.

I looked at a comparison between Java and C++ and I like C++'s features like operator overloading and its speed.

So, C++ is an OOP language, but it also has all the things java doesn't have and C does, like pointers and memory allocation. Do I need to change something in my OOP design to consider pointers and memory allocation? If so, what do I need to change? If not, why not?

share|improve this question
    
I think D language would be a better candidate, since it has both the speed of C++ and class syntax similar to Java's: DLang.org –  GoToLoop Jun 22 at 10:56
    
I don't agree. D doesn't have the speed of C++, given that we write equivalent code. The language compilers are still immature, with lack of tool chains and libraries. –  concept3d Jun 22 at 11:38

3 Answers 3

up vote 10 down vote accepted

In terms of OO design, you don't have to change all that much when going from Java to C++. The majority of the design principles is actually language independent and applicable to both Java and C++.

How you express your design in code can differ quite a bit between languages. The most important thing to remember is C++ does not have automatic garbage collection. That and the fact that any C++ object can be created without using new are the two driving factors behind the differences in expressing your design in C++ or Java code.

In particular, it is very common in C++ to express composition by directly having a member of type X in class Y without involving a pointer.
More in general, if you can avoid dynamic memory allocation in C++, do so. If you can't, see if there is a smart pointer available that can ensure the memory is released again at the right moment.

share|improve this answer
    
Is there anything in Java that is analogous to templates in C++? –  R Sahu Jun 23 at 4:22
    
@RSahu: Java has a form of generics, but I am not familiar enough with it to tell how comparable that is to C++ templates. –  Bart van Ingen Schenau Jun 23 at 7:18
    
@RSahu Read this article about Generic vs. Templete –  Anton Dozortsev Jun 24 at 19:14

No, you don't need to change anything in your design if your design was well thought out from the start. OOD should be language agnostic, that means you should be able to pick any OO language and implement it.

share|improve this answer
    
So it's just syntax from here? –  shoham Jun 22 at 11:33
    
It should be. If it's not, something was wrong with the design. –  nvoigt Jun 22 at 11:36

As Bart van Ingen Schenau points out, C++ doesn't have garbage collection. That means for the most part you must restrict your designs to ones where every object has a single unambiguous owner that disposes of it when the owner goes out of scope. Anything more complicated runs the risk of a memory leak.

share|improve this answer
    
not so, you can use a smart pointer to encapsulate the ownership - last owner is the one that cleans up in that case, so you don't need an unambiguous owner. What you wrote is true for raw pointers and C-style code though. –  gbjbaanb Jun 22 at 18:48
    
@gbjbaanb With reference counting you have to make sure you don't have any cycles. It's not insurmountable, but the risk of a leak is there. –  Doval Jun 22 at 20:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.