Language Concepts


C was conceived and created as a procedural programming language, whereas Objective-C was to be object-oriented, hence the name. In a procedural language, the code is focused around variables, data, and functions—how to store data and what to do with the data. In contrast, an object-orientated language focuses on creating objects, which are then used to do certain things, just like objects, or “things”, do in real life.

Object-orientated code seems to involve more work initially—there is a lot of “boilerplate” code for even the simplest objects. Fortunately, most of this code is already provided in Xcode’s templates, and the objects will quickly become more useful.

So what is an object? Put simply, it is a “thing.” Throughout this book, one of the objects that we will be creating will be a Die—the kind you might find in a board game. From the program’s perspective, the die is a “black box”—it hides its inner workings; the object performs any task that is asked of it (assuming that the object has been programmed to actually perform the task), and when it finishes, the object is no longer used. How the object performs its task is irrelevant, as far as the program itself is concerned.

Once you create an object, you can then tell your program to produce as many of them as you need. Therefore, your die object can create a pair of itself—a pair of dice.

These dice have traits, such as color, size, or the number of faces. You can also perform actions with these dice—you can roll one, or you can roll both of them. After rolling both of them, you would then add, or perhaps multiply, the resulting numbers.

From a higher-level viewpoint, all the program has to do is ask the dice to roll themselves, and report a total. The program does not have to know how the dice do that. In fact, if you were not the original creator of the die object, you wouldn’t either—and that’s perfectly fine.

Object-orientated programs allow developers to hide the inner workings of their program, while also making the program more efficient to run, as well as maintain. It has become the de facto programming language convention for most large programs, and likely will remain as such for years to come.

About these ads
Leave a comment

3 Comments

  1. I’ve recently been bothered by the notion that Objective-C (and C++) are Object-Oriented while C is not.

    Though I’m not more than casually familiar with Objective-C, I suspect it’s “as object oriented as” C++. But object-orientation being a design paradigm, I’m having a hard time believing that C does not have at least one foot in the “object-oriented door.”

    In C, an object is defined as “a region of memory.” An integer is an object, as is a function, as well as a struct, or anything that makes it to the text area of the program after compile-time and anything created during run-time. That’s pretty vague, and is indeed pretty much everything.

    But the real hang-up for me is in structs. In C, if you want to get anything done, really, you make structs to represent the objects you’re going to deal with, such as a list node (which might contain a pointer to the next node, the previous node, maybe the head and/or tail, a serial number, some other stuff if need be, and most importantly, a pointer to some other struct that is the principal payload). You might define a “point” as three floats, and a “rectangle” as two points, and a “cuboid” as two rectangles, then malloc 50 of them and attach them to list nodes. Just because these data structures don’t have member functionality, to me, doesn’t mean that the program wasn’t designed in an object-oriented way. I understand the use of member-functionality and private data, and data and code hiding, but it seems like “object-oriented” isn’t the best phrase the industry could have come up with to describe the paradigm.

    That and the industry likes to say that C++ is object-oriented, where it is best described as “multi-paradigmic,” (regardless of its original designers intention) because its object-supporting features in no way detract from its procedural facilities, while even offering a good handful of functionally-oriented facilities (statement evaluation, lambda, and the ternary operator).

    I don’t know if, but I assume the same is true of Objective-C.

    Thoughts?

    Reply
    • That’s a very good point. Note that my knowledge of plain C isn’t very extensive, so…

      I’ll begin by quoting Scott Rosenberg in his book “Dreaming In Code”: “Object-oriented techniques organize programs not around sequential lines of commands but instead around chunks of code called objects that spring to life and action when other objects call on them. Objects relate to other objects via strictly defined inputs and outputs, so that programmers writing code that must interact with them need not concern themselves with what’s happening inside them…Software objects interact with one another by sending ‘messages’ and triggering ‘events.’ Typically, objects can be organized into ‘classes’ that share traits, and they can inherit characteristics from their ‘parents.’”

      My definition of an object is a “thing” in a specific piece of memory. The object has characteristics (as ints and structs do); these characteristics are the values or variables. But an object is also capable of doing something; it is capable of being called on to do something, without having the caller worry about how that something is done. The most important part (IMO) is that the object encapsulates, or hides, its variables/values, and how it accomplishes a task. The object is a “black box” that does what it’s told, and returns values as you ask for them.

      Browsing through my C reference, the first thing that came to mind is that a struct appears to be more like an array with objects at pre-defined indices—almost like a dictionary. The struct stores data transparently (I’ll get back to that later), but doesn’t store the instructions for any actions—you can’t call on a struct to do something. Also, a struct does not provide all that much flexibility in accessing its values—you have to know the index, and in certain cases you have to access them sequentially—this is especially true when you are creating a new struct and setting all its values. You have to comma-delineate each value, without being able to call the name of the value (like the name of a variable).

      Finally, objects (especially in Objective-C) provide access to their inner ivars (instance variables) only through getter and setter methods—as in

      (NSString *)someString;

      and

      (void)setSomeString

      The black box nature of objects means that you shouldn’t directly touch the variables; you should use the getter/setter methods instead. Same thing with methods (actions).

      Languages such as Objective-C were built around this object-orientated philosophy—for example, Obj-C provides the @property directive, which creates getters and setters for you, something that would not fit into the world of C.

      I’ll leave you with the Wikipedia article on Procedural Programming and the corresponding discussion surrounding the concept of a module as the equivalent of an object.

      Unfortunately, I have almost zero knowledge of C++.

      Hope this helps!

      Reply
  1. Learn Objective-C in 24 Days « Programming for iOS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

  • Welcome

    My goal is to make CupsOfCocoa into a beautiful source for beginners to the iPhone platform to get started. Subscribe below for more, and stay tuned!

  • Contact Me

    If you need to contact me for any reason, feel free to send me an email.
  • The Giving Spirit

    If you've found this site helpful, would you consider donating a little sum? Any amount is appreciated...Thanks so much!

  • Roadmap

  • Enter your email address to follow this blog and receive notifications of new posts by email.

    Join 226 other followers

  • Back to the Past

    September 2010
    S M T W T F S
    « Aug   Oct »
     1234
    567891011
    12131415161718
    19202122232425
    2627282930  
  • Time Machine

  • You count!

    • 590,687 views
  • Worldwide Stats

    free counters
Follow

Get every new post delivered to your Inbox.

Join 226 other followers

%d bloggers like this: