Changes

An Overview of Objective-C Object Oriented Programming

1,197 bytes added, 20:20, 13 October 2009
Declaring an Objective-C Class Implementation
</pre>
We are now at the point where we can write some code to work with our new BankAccount class.
== Declaring, Initializing and Releasing a Class instance ==
So far all we have done is define the blueprint for our class. In order to do anything with this class, we need to create instances of it. The first step in this process is to declare a variable to store a pointer to the instance when it is created. We do this as follows:
 
<pre>
BankAccount *account1;
</pre>
 
Having created a variable to store a reference to the class instance, we can now initialize the class:
 
<pre>
account1 = [BankAccount alloc];
</pre>
 
In the above statement we are calling the ''alloc'' method of the BankAccount class (note that ''alloc'' is a ''class method'' as opposed to an ''instance method'').
 
Having allocated memory for the class instance, the next step is to initialize the instance by calling the ''init'' instance method:
 
<pre>
account1 = [account1 init];
</pre>
 
For the sake of economy of typing, the above three statements are frquently rolled into a single line of code as follows:
 
<pre>
BankAccount *account1 = [[BankAccount alloc] init];
</pre>
 
== Calling Methods and Accessing Instance Data ==