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?

  • 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
    Commented Jun 22, 2014 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
    Commented Jun 22, 2014 at 11:38

3 Answers 3


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.

  • Is there anything in Java that is analogous to templates in C++?
    – R Sahu
    Commented Jun 23, 2014 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. Commented Jun 23, 2014 at 7:18
  • @RSahu Read this article about Generic vs. Templete Commented Jun 24, 2014 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.

  • So it's just syntax from here?
    – shoham
    Commented Jun 22, 2014 at 11:33
  • It should be. If it's not, something was wrong with the design.
    – nvoigt
    Commented Jun 22, 2014 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.

  • 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
    Commented Jun 22, 2014 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
    Commented Jun 22, 2014 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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