Object Oriented Programming

Slides:



Advertisements
Similar presentations
Lecture 2 - Introduction Objective C is used primarily for application development on Apple's Mac OS X and iPhone. On the Apple it is used together with.
Advertisements

Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Compiler Design PROJECT PRESENTATION : COMPILER FOR OBJECTIVE C Harshal Waghmare Jeet Kumar Nandan Kumar Vishal Agrawal.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
IPhone Development Crash Course By Dylan Harris
IPhone Development JaxCodeCamp Who am I David Fekke.NET Developer, ColdFusion Work at LPS Presenter JaxDug, JaxJug, JSUG & JaxFusion Mac User 1986.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
Objective-C Quinton Palet. Introduction  Objective-C was developed to bring large scale software development the power of the object-oriented approach.
Mac OS COMP 240. Mac OS OS X previously Mac OS X, is a series of Unix- based graphical interface operating systems developed, marketed, and sold by Apple.
“C” Programming Language CIS 218. Description C is a procedural languages designed to provide lowlevel access to computer system resources, provide language.
Comparison of OO Programming Languages © Jason Voegele, 2003.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Introduction to Collections – Stacks Modified
Toward iOS programming. Overview  Objective-C is an object oriented language.  follows ANSI C style coding with methods from Smalltalk  Flexible almost.
Ruby! Ronald L. Ramos. What is Ruby? Ruby is a scripting language designed by Yukihiro Matsumoto, also known as Matz. It runs on a variety of platforms,
Programming Languages and Paradigms Object-Oriented Programming (Part II)
OS X Development Tom Schroeder. Table of Contents Who cares? HistoryObjective-CCocoaEnvironmentDesign.
Objective-C First Part adapted from a presentation by Kevin Layer.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.
Introduction to Object Oriented Programming CMSC 331.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Object-Oriented Programming in C++
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Computer Programming 2 Why do we study Java….. Java is Simple It has none of the following: operator overloading, header files, pre- processor, pointer.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Objective-C Drew Cheng CS420. Overview True superset of ANSI C Object-Oriented methods from SmallTalk There is no formal written standard for the language.
Object-Oriented Programming Chapter Chapter
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Introduction to Objective-C Spring Goals An introduction to Objective-C As implemented by the Apple LLVM Compiler 4.0 (a.k.a. Clang) Only the basics…
The iOS Platform and SDK. iOS iPhoneiPad Mini iPad.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
MORE on classes in …... OOP terminology in Objective-C Class: defines the grouping of data and code type of entity Object or Instance: a specific instantiation.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Objective-C: Intro Michelle Alexander COMS E6998 2/4/2013.
ISBN Chapter 12 Support for Object-Oriented Programming.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Oriented Programming CMSC 331. Concept of Abstraction “An abstraction is a view or representation of an entity that includes only the attributes.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Andy Wang Object Oriented Programming in C++ COP 3330
Classes C++ representation of an object
Introduction to Objective-C
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Lecture 2 Memory management.
Checking Memory Management
Introduction Enosis Learning.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Introduction Enosis Learning.
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Data Structures and Generics
Stacks and Queues.
Classes C++ representation of an object
Lecture 2 Memory management.
Presentation transcript:

Object Oriented Programming Objective-C

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.

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”

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.

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.

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.

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.

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.

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.

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.

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.

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];

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)

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.

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.

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

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 word @interface 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:

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

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

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 using @property 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.

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.

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

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 2 3 1 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 2 3 1

References Cox, Brad. Object Oriented Programming: an Evolutioary Approach Sebesta, Robert. Concepts of Programming Languages Apple Inc. Apple Developer Connection http://developer.apple.com Stevenson, Scott. Theocacao http://theocacao.com/document.page/510 Various Authors. Wikipedia: the Free Encyclopedia http://en.wikipedia.org 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?