01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

56 CHAPTER 3 An introduction to objects<br />

3.1 A whirlwind tour of object-oriented programming concepts<br />

In this chapter we can’t do justice to every concept associated with object-oriented<br />

programming (also termed OOP, for short). Instead, the goal of this chapter is to<br />

make sure you have a solid understanding of the fundamental concepts and benefits<br />

of object-oriented programming and a working knowledge of the terminology.<br />

Throughout this book, we discuss object-oriented programming concepts as they<br />

apply to <strong>Objective</strong>-C and expand on what’s covered in this chapter. Let’s get started by<br />

learning what’s so wrong with C.<br />

3.1.1 What’s wrong with procedural-based languages such as C?<br />

In very broad brush strokes, a procedural language requires greater concentration<br />

and observance of manually enforced or informal rules than does an object-oriented<br />

language.<br />

One reason for this is that procedural languages focus on dividing the application’s<br />

source code into individual functions, but generally their ability to control<br />

access to data is less fine-grained. Data will generally belong to a particular function or<br />

be globally accessible by any function. This causes problems when several functions<br />

need to access the same data. To be available to more than one function, such variables<br />

must be global, but global data can be accessed (or worse yet, modified in inappropriate<br />

ways) by any function in the application.<br />

Object-oriented programming, on the other hand, attempts to merge these two concepts.<br />

When developing a new application, you first think of the different kinds of<br />

things represented in it, then the type of data it’s required to store, and then the actions<br />

each thing should be able to perform. These “things” are commonly called objects.<br />

3.1.2 What are objects?<br />

When developing an object-oriented application, you’re creating a miniature model<br />

of a system. That model is constructed from one or more building blocks called objects.<br />

As an example, in a drawing application, a user may create three objects representing<br />

a circle and two rectangles. Each object has associated data that is relevant to itself.<br />

The circle object’s data may describe a radius, while the rectangle objects, to be recognized<br />

as rectangles, will probably require a width and height to be stored as their data.<br />

Objects in an application can usually be grouped into similar types. For example,<br />

all circles will require that similar attributes be specified to describe their size, shape,<br />

and color. But the attributes required to describe circles will most likely be different<br />

from the attributes required for all of the objects of type rectangle.<br />

Individual objects in an application are created from a cookie-cutter-type template<br />

called a class. A class describes what type of data an object must store and the type of<br />

actions or operations it can perform.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!