8

I am very confused about this programming language, Objective-C, which I heard is used to develop iOS applications.

I know that it uses the principles of OOP. Would it be easier to learn if I already knew C++? What about it's name? is it a combination between the C programming language and the OOP principles I use in C++?

4 Answers 4

12

Brad Cox and friends added a thin layer of Smalltalk on top of C.

Objective-C thus has much more in common with Smalltalk's highly dynamic message-sending style OO than C++'s.

One major difference is that in Objective-C you don't worry too much about what class something is: you care about what messages something understands. You can have objects that change the set of messages they understand, at runtime.

Having said that the two languages have very different ideas of what OO is, many ideas/principles are still shared: inheritance, delegation, polymorphism, and so on. You'll easily find many examples of the various OO patterns in code written in either language.

2

Others have pointed out that Objective-C is a SmallTalk style OO layer added to C, I'll add that C++ started as a Simula style OO layer added to C (and then templates were added to make a good measure).

1

From the horse's mouth:

Objective-C is a hybrid programming language[…]formed by grafting the Smalltalk-80 style of object-oriented programming onto a C language rootstock. Objective-C adds precisely one new data type, the object, to those C provides already, and precisely one new operation, the message expression. Like Smalltalk-80, Objective-C makes no compile-time distinction between different kinds (classes) of objects.

To address "Would it be easier to learn if i already knew C++?" I don't think so, but I don't think it'd hurt either. You might be more comfortable with concepts like classes and objects if you already know C++, but will find that Objective-C implements classes in a very different way to C++. To borrow some terminology from Stroustrup, all ObjC messages are "virtual" and can be redefined by subclasses, or even have their implementations swapped on the defining class. This and other subtleties probably cancels out some of the headstart you'll get from knowing C++.

2
  • Objective-C's methods are much more virtual ("only apparently existent") than C++'s virtual: An object need not even understand a message to handle it. It can quietly forward the message on to some internal object that can understand the message, allowing transparent proxying to support, say, lazy loading an instance from a database. May 5, 2012 at 20:03
  • Indeed, I was borrowing the terminology to make the idea familiar. C++ uses vtables to dispatch virtual methods, which ObjC only recently added as a performance optimisation.
    – user4051
    May 5, 2012 at 20:15
0

Objective-C is C, with a layer of Smalltalk.

According to Wikipedia:

Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.

It is Object-Oriented, but is not from C++ directly.

So yes, it is C with some of the OO abilities of Smalltalk.

1
  • 5
    nor indeed is it from C++ at all. C++ borrows its class model from Simula, and the two languages were being worked on at around the same time.
    – user4051
    May 5, 2012 at 13:45

Your Answer

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

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