19
February
10,385 views
Facebook Share
Twitter Tweet

Here are the major difference between C++ and Obj C:

  • C++ allows multiple inheritance, Objective-C doesn’t.
  • Unlike C++, Objective-C allows method parameters to be named and the method signature includes only the names and types of the parameters and return type (see bbum’s and Chuck’s comments below). In comparison, a C++ member function signature contains the function name as well as just the types of the parameters/return (without their names).
  • C++ uses bool, true and false, Objective-C uses BOOL, YES and NO.
  • C++ uses void* and NULL, Objective-C prefers id and nil.
  • Objective-C uses “selectors” (which have type SEL) as an approximate equivalent to function pointers.
  • Objective-C uses a messaging paradigm (a la Smalltalk) where you can send “messages” to objects through methods/selectors.
  • Objective-C will happily let you send a message to nil, unlike C++ which will crash if you try to call a member function of NULL
  • Objective-C allows for dynamic dispatch, allowing the class responding to a message to be determined at runtime, unlike C++ where the object a method is invoked upon must be known at compile time (see wilhelmtell’s comment below). This is related to the previous point.
  • Objective-C allows autogeneration of accessors for member variables using “properties”.
  • Objective-C allows assigning to self, and allows class initialisers (similar to constructors) to return a completely different class if desired. Contrast to C++, where if you create a new instance of a class (either implicitly on the stack, or explicitly through new) it is guaranteed to be of the type you originally specified.
  • Similarly, in Objective-C other classes may also dynamically alter a target class at runtime to intercept method calls.
  • Objective-C lacks the namespace feature of C++.
  • Objective-C lacks an equivalent to C++ references.
  • Objective-C lacks templates, preferring (for example) to instead allow weak typing in containers.
  • Objective-C doesn’t allow implicit method overloading, but C++ does. That is, in C++ int foo (void) and int foo (int) define an implicit overload of the method foo, but to achieve the same in Objective-C requires the explicit overloads - (int) foo and - (int) foo:(int) intParam. This is due to Objective-C’s named parameters being functionally equivalent to C++’s name mangling.
  • Objective-C will happily allow a method and a variable to share the same name, unlike C++ which will typically have fits. I imagine this is something to do with Objective-C using selectors instead of function pointers, and thus method names not actually having a “value”.
  • Objective-C doesn’t allow objects to be created on the stack – all objects must be allocated from the heap (either explicitly with an alloc message, or implicitly in an appropriate factory method).
  • Like C++, Objective-C has both structs and classes. However, where in C++ they are treated as almost exactly the same, in Objective-C they are treated wildly differently – you can create structs on the stack, for instance.

Source of Information: Stackoverflow

Objective C

9 Responses so far.

  1. Paul says:

    Couple of things:

    1) C++ can have named parameters too. See http://cpp-netlib.github.com/0.9.1/reference_http_server.html#constructor for an example.

    2) Generally, calling a method on a null pointer in c++ won’t necessarily segfault. Most implementations segfaults when you acces a member variable.

    Also, I believe, Objective-C lacks polymorphic functions. All polymorhpism must be done throught subtype polymorphism.

    Furthermore, all of objective-Cs dynamic behaviour can be simulated in C++ by using a map of functions. However, a compile-time type-checked algrebriac type system is impossible in objective-C. For example, using a variant(which is like a union) in C++, the compiler can check that all cases are handled. In objective-C, you have to rely on it being checked at run time. So the edge cases could possibly never be checked for.

    • Shahzaib Khan says:

      Thank you Paul for your contribution.

  2. Nadeem Jamali says:

    Very Nice Shahzeb!
    It cleared my much ideas about Objective-C. I was worrying about programming in Objective-C. After reading this article, I feeling relax…

    • Shahzaib Khan says:

      Thanks Nadeem.

      • Khal says:

        Thinking about this further, I do not think it pobslsie (or even desirable) to avoid super() completely. If I were to create a class Foo and you created a subclass Bar, what would happen if you were somehow able to bypass Foo’s constructor? And how about Object? Scary code indeed!

  3. G. Pramod Kumar says:

    Very informative and helpful .

    Thanks G. Pramod Kumar

  4. Leonardo Piedade says:

    very useful for objective-C beginners!

  5. George says:

    Cool

  6. Bob says:

    C++ now officially uses nullptr, not NULL.