Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 Object Oriented Programming
Objective-C

2 Overview Objective-C is an object oriented language.
follows ANSI C style coding with methods from Smalltalk There is no formal written standard Relies mostly on libraries written by others Flexible almost everything is done at runtime. Dynamic Binding Dynamic Typing Dynamic Linking What is objective-C? Well, for starters it’s a programming language. More specifically it’s a completely object oriented programming language. It can be described as having descended from of Smalltalk and C. Since it uses a modified C compiler and can be implemented with C code. It borrows an object manipulation method (the syntax) from Smalltalk. Unlike C and C++, there is no formal written standard. It must rely on other libraries or else the make have the client make their own from scratch. This is also a dynamic language, where very little is done at compile-time. Using dynamic typing, linking and binding, it allows for more flexibility when running.

3 Inventors Objective-C was invented by two men, Brad Cox and Tom Love.
Both were introduced to Smalltalk at ITT in 1981 Cox thought something like Smalltalk would be very useful to application developers Cox modified a C compiler and by 1983 he had a working Object-oriented extension to C called OOPC. Brad Cox and Tom Love both were at an ITT tech center were they were introduced to Smalltalk and object Oriented programming. Cox took a vested interest in OOP as he thought it would be invaluable in building powerful Developer environments for system developers. In 1983, he started modifying a C compiler to use OOP, and eventually came up with a working extention called OOPC which stands for “Object Oriented Programming in C”

4 Development Tom Love acquired a commercial copy of Smalltalk-80 while working for Schlumberger Research With direct access Smalltalk, Love added more to OOPC making the final product, Objective-C. In 1986 they release Objective-C through their company “Stepstone” Love meanwhile was hired by Schlumberger Research and had the opportunity to work with one of the first commercial copies of Smalltalk-80. Love with is first hand experience and with direct access to Smalltalk, he adds more to OOPC creating the first release Objective-C. The language is released 1986 through the company they created called “stepstone”. That same year Brad Cox writes his book <show book> which describes the language, or an early form of it somewhat. If you want to know more about Obj-C, you should go check it out.

5 NeXT and NeXTSTEP In 1988 Steve Jobs acquires Objective-C license for NeXT Used Objective-C to build the NeXTSTEP Operating System Objective-C made interface design for NeXTSTEP much easier NeXTSTEP was derived from BSD Unix In 1995 NeXT gets full rights to Objective-C from Stepstone In 1988 Steve Jobs who left Apple to start the NeXT Software company, gets a license to use Objective-C from Stepstone. His company then uses it to build an OS for his NeXT computers. Their new Operating System NeXTSTEP was built from the Mach Kernel from BSD Unix, and with Objective-C, interface design and implementation was pretty easy. NeXTSTEP demonstrates the use of Obj-C in practical terms. Since Obj-C has no std lib of its own, NeXT basically wrote the primary implementation of objective-C, they eventually get the rights for Objective-C. Soon other machines wanted to use something like NeXTSEP.

6

7

8 OPENSTEP API Developed in 1993 by NeXT and Sun
An effort to make NeXTSTEP-like Objective-C implementation available to other platforms. In order to be OS independent Removed dependency on Mach Kernel Made low-level data into classes Paved the way for Mac OS X, GNUstep So they started to develop OPENSTEP jointly with Sun Microsystems. Unlike NextStep, which was an OS and a set of libraries, they wanted to make just an OS independent set of libraries. That would mean removing all dependencies on the Mach Kernel that was in NeXTSTEP and BSD. They also made low level data types into classes, a low level data would be something like strings or datestamps, into classes named NSString or NSDate, Where NS stands for NeXT-sun (the developes). I’ll talk more about them later.

9 Apple and Mac OS X NeXT is taken over by Apple in 1996 (294 million) and put Steve Jobs back at Apple. Redesigned Mac OS to use objective-C similar to that of NeXTSTEP Developed a collection of libraries named “Cocoa” to aid GUI development Release Mac OS X (ten), which was radically different than OS 9, in March 2001 In 1996, Steve Jobs’ former place of employment comes in and takes over NeXT and with it, NeXTstep and Objective-C. So then Apple puts them all to work, Jobs included. Since NeXTStep worked wonderfully, they decided to implement it into their Mac OS family of OSes. From NextStep they develop a new API which they called “Cocoa” to work with Objective-C which was Apple’s now. From NeXTSTEP, which was from BSD, they made MAC OS ten. OS ten was radically different than their previous MAC Oses, which sets Apple Inc. forward to new futures.

10 The Cocoa API Primarily the most frequently used frameworks nowadays.
Developed by Apple from NeXTSTEP and OPENSTEP Has a set of predefined classes and types such as NSnumber, NSstring, Nsdate, etc. NS stands for NeXT-sun Includes a root class NSObject where words like alloc, retain, and release come from The Cocoa API is now days more frequnetly used when using Objective-C. Cocoa saw influence from OpenStep and Nextstep. In that they use NSObjects like in Openstep. Almost any object made in cocoa must inheret from NSObject in order to use the methods frovided by the framework.

11 Dynamic Language Almost everything is done at runtime
Uses dynamic typing, linking, and binding This allows for greater flexibility Minimizes RAM and CPU usage Objective-C is a wholly dynamic language. Dynamic typing allows minimal memory usage as the OS does not need to keep track of what things are untill they need to use them. DB means it won’t have to merge function calls with parameters until it needs to run them. An DL allows more things to be added to the program while it runs. All 3 aspects are perfect for running an OS where changes can be made while the system is still running.

12 To Import or Include? #import head.h
C/C++’s #include will insert head.h into the code even if its been added before. Obj-C’s #import checks if head.h has been imported beforehand. Obj-C introduces a precompile command “Import” which unlike C and C++’s include would check to see weather or not the file (in this case “head.h”) has been imported before. If the file has been added before, then it will not import it again. This is to eliminate the redundancy of adding the same file over and over again. And to prevent compilation errors when duplicate functions are declared. Importing will aid in inheritence and adding classes to a program, because one import could call so many other imports so you’ll need as little as one import.

13 Messages Almost every object manipulation is done by sending objects a message Two words within a set of brackets, the object identifier and the message to send. Because of dynamic binding, the message and receiver are joined at runtime [Identifier message ] The primary aspect Obj-C derived from smalltalk was the messaging system. At runtime, a message (which could be a function) is sent to an object and it does what that message says. This is what Dynamic Binding is. The syntax consists of 2 words in BRACKets usually the Identifier which represents an object or class, and a message to be a method or a “function” to you C++ enthusiasts out there.

14 Basic syntax structure
void function(int x, int y, char z); Object.function(x, y, z); Objective-C syntax A brief overview of syntax structure. -(void) function:(int)x, (int)y, (char)z; [Object function:x, y, z];

15 Keyword: id The word ‘id’ indicates an identifier for an object much like a pointer in c++ This uses dynamic typing For example, if Pen is a class… extern id Pen; id myPen; myPen = [Pen new ]; A good way to implement DT, is to use generic identifers like this type id. Id work like pointers to objects. A good example, of how ids work, as written by Cox in his book. (Cox, 59)

16 Memory Allocation Objects are created dynamically through the keyword, “alloc” Objects are dynamically deallocated using the words “release” and “autorelease” autorelease dealocates the object once it goes out of scope. NOTE: None of these words are built-in Like smallTalk, objects in Obj-C is created dynamicly When an object gets created it must be deallocated later later on It should be noted that none of these words are built in, thay are all functions of a Root class like NSObject in cocoa.

17 Ownership Objects are initially owned by the id that created them.
Like C++ pointers, multiple IDs can use the same object. However, like in C++ if one ID releases the object, then any remaining pointers will be referencing invalid memory. A method like “retain” can allow the object to stay if one ID releases it. Objects are owned by the ids that point to them, <multiple ids can own> Much like C++ pointers, the IDs can point to the same object. However like with c++ pointers if the object its referencing goes out of scope or otherwise gets released by another ID, than any other IDs using the object become invalidated. Using the retain keyword, the object will remain in the system when any ID releases it. However, retain will keep the object until it gets EXPLICITLY deleted.

18 Prototyping functions
When declaring or implementing functions for a class, they must begin with a + or - + indicates a “class method” that can only be used by the class itself. In other words, they’re for private functions. - indicates “instance methods” to be used by the client program (public functions). Read Slide A + would be used for something like object allocation (constructor), which is the only thing I seen it sued for, so I’m not sure if it can be used for other things. Primarily the id is a Class in this case

19 Class Declaration (Interface)
#import <Cocoa/Cocoa.h> @interface Node : NSObject { Node *link; int contents; } +(id)new; -(void)setContent:(int)number; -(void)setLink:(Node*)next; -(int)getContent; -(Node*)getLink; @end node.h For my example program a stack program, it uses nodes. In our H file, we put The name of the class after the followed by its superclass. Since I’m using cocoa, its superclass is going to be NSObject. Inside the braces below the class name, is the object’s “Instance variables” or private data for all you c++ fans out there. A node has link to another node, and a value that it stores. This class will be inhereted from my stack example I’ll show later Functions:

20 Class Definition (Implementation)
#import "node.h” @implementation Node +(id)new { return [Node alloc];} -(void)setContent:(int)number {contents = number;} -(void)setLink:(Node*)next { [link autorelease]; link = [next retain]; } -(int)getContent {return contents;} -(Node*)getLink {return link;} @end node.m The class is defined or “implemented” in an m file. Where m stands fro iMplimentation Note that I imported node.h, but did not have to import cocoa

21 C++ VS. Objective-C Adds OOP, metaprogramming and generic programming to C Comes with a std library Has numerous uses Large and complex code for OOP Only adds OOP to C Has no standard library; is dependant on other libraries Mostly used for application building Simpler way of handling classes and objects Use animation here to help show each comparison at a time

22 Objective-C 2.0 In October 2007, Apple Inc. releases Objective-C 2.0 for Mac OS 10.5 (Leopard) Adds automatic garbage collection Instance Methods (public functions) are defined differently Fairly reciently Apple release Obj-C 2 which I have never used or know much about. Other than the fact that its much different and there is little documentation about it at the moment. Since I’m using OS 10.4 I can’t use this, so I stuck with Objective-C 1.

23 linkList class linkList.m #import "linkList.h"
@implementation linkList +(id)new {return [linkList alloc];} -(void)insert:(int)value { id temp = [Node new]; [temp setContent:value]; [temp setLink:head]; head = [temp retain]; [temp release]; } -(void)append:(int)value { id last = [head getLink]; while ([last getLink] != nil) {last = [last getLink];} id temp = [Node new]; [temp setContent:value]; [last setLink:temp]; [temp release]; } -(void)remove { id temp = head; head = [head getLink]; [temp release]; -(int)getValue { return [head getContent];} @end For my example, here link list inherets from Node, such that I can use node specific functions in the code without calling for Node explicitly.

24 Stack class stack.h stack.m #import "linkList.h”
@interface Stack : linkList {} +(id)new; -(void)push:(int)value; -(int)pop; @end #import "stack.h” @implementation Stack +(id)new {return [Stack alloc];} -(void)push:(int)value {[self insert:value];} -(int)pop { int ret = [self getValue]; [self remove]; return ret; } @end And Stack inherets from Linklist, and All I need to to wrap push and pop around LL functions. What I forgot to mention is that the word self is one of the few built-in words that reference the object itself like C++’s “this” keyword. stack.h stack.m

25 Example: main.c main.c #import "stack.h” int main(){
Stack *s = [Stack new]; [s push:1]; [s push:2]; printf("%d\t", [s pop]); [s push:3]; [s release]; return 0; } main.c Note that I only needed to import “stack.h” and nothing else becaue stack imports LinkList.h which imports Node.h which imports cocoa.h In this example I make a little stack I push 1 and 2 and pop once and push 3 and pop twice, so I should get in that order. I run it in GCC (on MAC OS 10.4 mind you) and get just that. $ gcc -x objective-c node.m linkList.m stack.m main.c -framework Cocoa -o stackTest $./stackTest

26 References Cox, Brad. Object Oriented Programming: an Evolutioary Approach Sebesta, Robert. Concepts of Programming Languages Apple Inc. Apple Developer Connection Stevenson, Scott. Theocacao Various Authors. Wikipedia: the Free Encyclopedia Now if you enjoyed this presentation as much as I enjoyed making it then you should go check the sources out especiall Dr. Cox’s book. Any questions?


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google