Comparing Objective-C versus C++

In the past 6 years I’ve seriously started coding, and tasted many programming languages and syntaxes including C, C++, Java, Python and some others.  As you know, depending on your application domain and the problem you’re going to solve, each of them provide a degree of support on that domain. For example if you’re going to build some sort of heavy physical simulation program which needs real-time speed, I’m sure won’t choose Java or Python or any non-native and high-level language to do the computation in 1.4x time compared to C and C++.

Since last year when we started developing iPhone applications, I found interesting things in Objective-C language which I’m going to summarize them here.

The Syntax

As a matter of fact, Objective-C like C++ is derived from his parent, The C Language and both of them has moved toward adding Object Oriented Programming (AKA OOP) capabilities to C. So much of the syntax is shared between those three and we’re going to discuss the differences here.

One of the major concepts is defining the object itself (known as Class) and modeling the communication between them. Obj-C calls this communication a Message and C++ calls it a Method.

If you haven’t seen objective-c syntax, here I’ll present a few samples comparing to C++ to achieve a basic idea of the language. As the first example if you have an object instance named myObject and you want to call a method named myMethod from it, you should write something like this :

ObjectiveC C++

[myObject myMethod];

| |

And here is a sample code for defining a class in both of these languages :

@interface Car : parent {
// instance variables
+(int) getDefaultModel: (int)parameter;
-(void) Beep:(float) frequency : (float) duration;
-(int) setColorTo: (int) color andModelTo:(int)model;
class Car : parent {
// instance variables// Class (static) functions
static int getDefaultModel(int parameter);
// Instance (member) functions
void Beep(float frequency, float duration);
int setColorAndModel(int color, int model);

Since, I’m not writing an objective-c tutorial, you can take a look at references and links to find out more examples.

Header and implementation files

Both languages use separate definition and implementation files. In short, header for a class specifies the tasks it can do and the data it can contain while the implementation shows how it will do the tasks. The naming conventions suggests the .h extension for header files and .m extension for implementation files in Objective-C and in C++ you might see .h / .hpp for header files and .cpp / .cxx for implementation files.

A common practice for large projects, resulting in large objects, is to organize different parts of implementation in separate files. In C++ you can define a single header file for a class and split the implementations over multiple CPP files. This is done via Categories under Objective-C. In a category you add methods to the class, even if it is not defined in the original prototype. In other words, categories allow the programmer to extend class functionality. A common example for categories is to add resizing functions to UIImage class for example, so you would be able to re-size any UIImage after that.


In the Persian Poem Book project, we had really large books and we faced lags while the user was browsing the books. So we implemented a data pre-fetching logic in a separate thread. And this is exactly where we faced a synchronization problem. Having the system programming and C language experience, I thought, we should start using semaphores via the Posix API.  But I found that, just like Java, it’s as easy as placing a @synchronize keyword and the compiler will support you in that block of code ! Hurray !


Ok, Ok, the answer for C++ is almost anywhere !  Objective-c is natively supported and improved by Apple and you can use it for Mac and iPhone development. In addition you have a choice of GNUStep on many other platforms. From the :

The purpose of GNUStep project is to provide a Free Software version of the Cocoa/OpenStep APIs available on as many platforms as possible. GNUstep seeks to be source code compatible with Cocoa and OpenStep. GNUstep currently supports GNU/Linux, NetBSD, OpenBSD, FreeBSD, Solaris, Darwin and Windows and should be capable of being built and used on any POSIX compliant UNIX platform which has gcc and/or LLVM/Clang.

Standard Libraries and ports

I had an small experience in developing for Symbian mobile operating system. As you probably know, the SDK is based on C++ language. As a typical C++ programmer, I expected to have STL ported into that platform but there were no official support for it. Consider yourself writing C++ code while having no stacks, queues, vectors and even no std::strings ! Although there are some unofficial ports which has memory management problems when I tested them.

When I entered the world of iPhone development, I expected kinda poor support for libraries but thanks to Apple, you have almost everything as you have in a MacOSX development, since they’ve ported the Core Foundation completely plus a really useful documentation and reference.


Objective-C just like its brother C++ and their common parent C, is considered a low-level language, so it won’t have any overhead of Virtual Machine or Interpreter and your application runs as a native machine code. Comparing to Java, you have most of the capabilities and object oriented modeling syntax, while keeping it native and speedy and without any pointers. C++ tries to be much like his parent and in a real coding scenario, you should still use function calls and pointers and so. As a teacher, my experience shows that the pointers was the hardest thing to teach in the whole C++ programming syllabus !

Dynamic typing

Objective-C is dynamically typed. Dynamic typing means that if you have a reference to instance of an object, the type of object will be looked up in the run-time while in C++, you specify the type of object in the code and then start working with it. This implies faster development with Objective-C, the resulting application will be highly extensible after development and allows using dynamic loading easily. It also makes the coding easier to learn since you no longer need to specify the type of object and then start working with it. In contrast the problem with dynamic typing is that this will shift the errors to run-time while you can simply realize the method call errors in compile-time with C++.

Garbage collection and Memory management

C++ introduces two new operators compared to C to allocate and free the objects. the new and delete operators. You should manage the memory yourself and keep track of the objects you create to clean them up.

In Objective-C, the base class NSObject introduces the alloc and release messages to create and destroy items and is not considered a completely managed language (e.g Java and .NET are managed environments) but it introduces a common programming practice to avoid memory leaks. The first cool point is that you can count the object references using the retainCount message and increase or decrease it using retain or release. In addition it introduces the autorelase pools which you can create the object and add it to the autorelease pool so that it will automatically clean up your object at the end of event loop.

Mix them together

C++ adds lots of new features to the C language: classes, virtual methods, non-virtual methods, constness, templates, friend classes, multiple inheritance, pure virtual functions, operator overloading, function overloading, private, public and protected members, constructors etc. Objective-C on the other hand add very little, it is just classes, methods, categories and protocols.

Objective-C is could be considered as just a pre-processor to the C language plus a run-time library. So you may want to use C++ features beside Objective-C. This is nowadays possible using a compiler mode named Objective-C++. Yeah, this time Objective-C is extending C++ :). In development environments such XCode, this would be possible by renaming objective-c implementation file from .m to .mm.

See Also :

The GNUStep Project

GNUStep Objective-C is Fun – (Tutorial for beginners)

Objective-C – Wikipedia Entry

C++ vs Objective-C Quick Comparison

Memory Management in Objective-C

Load Code: Why Objective-C is cool

This entry was posted in Believe Me and tagged , , , . Bookmark the permalink.

6 Responses to Comparing Objective-C versus C++

  1. James says:

    Next time can you cover pure virtual functions, C++ is just elegant and just works. ObjC gave me the shivers 🙂

  2. e says:

    “In fact, there was a feature in C++ which I missed in Objective-c. You can define a single header file for a class and split the implementations over multiple Cpp files while in objective-c you have to stick with only one .m file for the whole implementation. I’m not judging about this limitation and its just a personal idea !”

    Actually you can split up the implementation via Objective-C “Categories”

  3. root says:

    Yeah, I read about objective-c categories recently, but forgot to update the post. Thank you !

  4. Free NX says:

    There is a great share of your knowledge in the field on this blog. I like how you put things. I’ve added this blog to my boomarks and will be sure to spread the word to my friends. I did have an issue with how fast this page loaded. Might be something to check out.

  5. Seriously in detailed... says:

    thanks alot ya….. you are really giving a useful tips to me….

  6. Fernando says:

    i come from java world and just start learning osx programming. Found your post very useful and straight to the point. Thank you so much for the comprehensive summary.

Comments are closed.