Apple Enterprise - NeXTanswers Support Archive
Enterprise
[Index]
[Download]


Search NeXTanswers for:

Title: C++ vs. Objective C
Entry Number:
2589
Creation Date: January 27, 1998

....Overview
....Benefits of C++
....Benefits of Objective C
....An Objective C Primer

Overview

"I already know C++. Why do I have to learn Objective C?"


Objective C and C++ are different in their design and intent. C++ extends and modifies C significantly to create a new object-oriented language. The complexity of these changes sometimes leads to confusing interactions between features. Learning C++ can be a lengthy process, and without a stable standard, differences between C++ compilers can add to the confusion.

Objective C is a set of simple extensions that provide ANSI C (or C++) with a simple but powerful standard object model. Most C programmers will be able to learn enough Objective C for most applications in a few days.

Because of this difference in design philosophy, each language has its own benefits and limitations. OPENSTEP's development tools give you the widest range of possibilities by allowing C programmers to write object-oriented code quickly with Objective C, while allowing C++ programmers to mix Objective C and C++ in their projects. Because of its simplicity, Objective C does lack some features that C++ provides; however, most of C++'s functionality can be accessed in other ways in Objective C. OPENSTEP also allows you to use all of the features of C++ by including native C++ objects in your OPENSTEP programming projects.


Benefits of C++

Some of the unique features of C++ are:

+ Multiple inheritance

Objective C, like Java, does not support multiple inheritance. Instead, it allows the user to define a protocol. Declaring an Objective C class to conform to a protocol is similar to declaring a Java class to implement an interface; it defines a standard set of methods (member functions) that the class must respond to. Another object can check at runtime to see if a particular class conforms to a particular protocol. Thus, Boat would be a subclass of Vehicle and Duck would be a subclass of bird, and both would conform to the FloatingThings protocol. Unlike with multiple inheritance, only method declarations (member function declarations) are included, not definitions or instance variables (data members). Alternatively, the dynamic nature of Objective C allows you to simulate multiple inheritance: see NeXTAnswer # 2567.

+ A friend-ly language

C++ gives finer access control over all of an object's elements than Objective C, with private, public, and protected keywords and the concept of friend functions. Objective C does allow instance variables (data members) to be set as public, private, or protected, but does not have an equivalent construct for methods (member functions).

+ iostream.h

C++ provides a new implementation of the ANSI input/output library for stream-based i/o. Objective C does not have an analogous library, but some implementations of Objective C++ do allow you to call the C++ iostream library from your Objective C++ projects.

Benefits of Objective C

+ Dynamic typing and the id type

Any variable in an Objective C object may be declared as type id, meaning a pointer to an object of an undetermined type. The type can be determined at run time, and the appropriate messages sent. Using Objective C's SEL type, you can determine at runtime which message is sent to the object, and therefore what method (member function) is accessed. To avoid runtime problems, the programmer can also test at runtime (with the respondsToSelector: method) to see which messages an object recognizes. Class methods (static member functions) are also available; unlike C++ classes, Objective C classes are true objects and exist at runtime.

+ Categories and dynamic loading

The dynamic nature of Objective C allows existing classes to be extended at runtime. Objective C allows you to define categories, related sets of extensions to objects you've already created. For example, in converting a text-based app into a graphics app, the code your objects needed to draw themselves could be compiled as a category and loaded at run-time only when needed. This saves memory and allows you to leave your original objects unmodified.

+ Simple and standard

C++ is a complex and evolving language, leading to differences between compilers and implementations; as a result, C++ users need to be more careful about using features like boolean types that may not be supported on all compilers. Objective C 's simplicity makes these problems less likely; it has also helped it maintain a stable standard. (And yes, Objective C does have a BOOL data type.)

+ Shortcuts

C++ doesn't have a monopoly on convenient features, either. Objective C's additions include C++-style // comments and the #import directive, which acts like the #include directive but insures that each referenced file is included only once per project.


An Objective C Primer

A C++ programmer who starts working in Objective C is likely to notice the differences in syntax before the more subtle differences in implementation. For example, a call to a C++ Employee class instance's promoteToManager member function might look like this:

anEmployee.promoteToManager();

In Objective C, this same call would look like this:

[anEmployee promoteToManager];

A similar call with two arguments in C++...

anEmployee.promote(newPosition, newSalary);

...would look like this in Objective C:

[anEmployee promoteToPosition:newPosition atSalary:newSalary];

Here, the member function being called (in Objective C terminology, the instance method being messaged) is called promoteToPosition:atSalary:

Objective C's syntax and naming conventions are discussed in more detail in the book Object-Oriented Programming and the Objective C Language. This book also contains information on combining Objective C and C++ code in OPENSTEP projects and goes into more detail on the principles behind Objective C's object model. It is available as part of OpenStep's online documentation, or printed copies can be purchased from Apple Enterprise Software. More information, including a link to the Objective C Usenet FAQ, is available from the GNU Objective-C Project at http://world.std.com/~gsk/gnu-objc.html .


OpenStep | Alliances | Training | Tech Support | Where to Buy