Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY.

Slides:



Advertisements
Similar presentations
1 C++Tutorial Rob Jagnow This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
Advertisements

CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Python Objects and Classes
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Web Application Development Slides Credit Umair Javed LUMS.
Abstract Data Type Fraction Example
Inheritance Writing and using Classes effectively.
Written by: Dr. JJ Shepherd
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
More on Classes Inheritance Copyright © 2012 by Yong-Gu Lee
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Objective-C OOP Spring OOP Conceptually the same as C++, Java, and all other object oriented languages The syntax, however… …is, well, different.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Objective-C1 CS151 Presentation: Objective C Kai Tai Pang William Sze.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Polymorphism, Dynamic Typing, and Dynamic Binding Copyright © 2012 by Yong-Gu Lee
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Fall 2012.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
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…
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
OBJECTIVE C Kurt Ladendorf. General Knowledge  Super set of C  Smalltalk  Mainly used for iOS and other Apple development.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Classes, Interfaces and Packages
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
SEG4110 – Advanced Software Design and Reengineering TOPIC I Introduction to C++ For those who know Java And OO Principles in General.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Modern Programming Tools And Techniques-I
Introduction to Classes
Object Oriented Programming (OOP) LAB # 8
null, true, and false are also reserved.
Enumerations & Annotations
Enumerations & Annotations
CSC 113: Computer programming II
Enumerations & Annotations
Simple Classes in Java CSCI 392 Classes – Part 1.
Object-Oriented Programming in PHP
An Example of Inheritance
Review: C++ class represents an ADT
Chap 2. Identifiers, Keywords, and Types
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Chapter 11 Classes.
SPL – PS3 C++ Classes.
Presentation transcript:

Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY

F ILE E XTENSIONS  Header files  “.h file”  Implementation files  “.m file”

C REATING C LASSES T HE INTERFACE (F RACTION. H ) Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; -(int) numerator; -(int) Inheritance Class:Parent Instance variables Instance methods -= instance + = class (static)

C REATING C LASSES T HE IMPLEMENTATION (F RACTION. M ) #import "Fraction.h“ Fraction -(void) print { printf( "%i/%i", numerator, denominator ); } -(void) setNumerator: (int) n { numerator = n; } -(void) setDenominator: (int) d { denominator = d; } -(int) denominator { return denominator; } -(int) numerator { return numerator;

T HE C LIENT (M AIN. M ) #import #import "Fraction.h" int main( int argc, const char *argv[] ) { Fraction *frac = [[Fraction alloc] init]; // create a new instance // alloc gets the memory and init is the constructor // set the values [frac setNumerator: 1]; // calling setNumerator on frac and passing 1 [frac setDenominator: 3]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); // free memory [frac release];// if we alloc it, we need to release it return 0; } Output: The fraction is: 1/3

M ULTIPLE P ARAMETERS F UNKY S YNTAX In fraction.h... -(void) setNumerator: (int) n andDenominator: (int) d;...

M ULTIPLE P ARAMETERS F UNKY S YNTAX In fraction.m... -(void) setNumerator: (int) n andDenominator: (int) d { numerator = n; denominator = d; }...

M ULTIPLE P ARAMETERS F UNKY S YNTAX – I N M AIN. M #import #import "Fraction.h" int main( int argc, const char *argv[] ) { Fraction *frac = [[Fraction alloc] init]; // create new instances Fraction *frac2 = [[Fraction alloc] init]; [frac setNumerator: 1]; // set the values [frac setDenominator: 3]; [frac2 setNumerator: 1 andDenominator: 5]; // combined set printf( "The fraction is: " ); [frac print]; printf( "\n" ); printf( "Fraction 2 is: " ); [frac2 print]; printf( "\n" ); [frac release]; // free memory [frac2 release]; return 0; } Output: The fraction is: 1/3 Fraction 2 is: 1/5

C ONSTRUCTORS  They are just a method named init  Not a special construct like in Java or C++  Default constructor  -(id) init;  Id is a generic type for an arbitrary object

C ONSTRUCTORS In Fraction.h... -(Fraction*) initWithNumerator: (int) n denominator: (int) d;... In Fraction.m... -(Fraction*) initWithNumerator: (int) n denominator: (int) d { self = [super init]; // call the base class constructor if ( self ) { // if (self != nil) – making sure we didn’t run out of memory on last call [self setNumerator: n andDenominator: d]; } return self; // I’m a constructor }... In main.m Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];

A  Default

A CCESS. H Access: NSObject int int privateVar; // private doesn’t go before each variable, unlike Java int int protectedVar;

A CCESS E XAMPLE – M AIN. M #import "Access.h" #import int main( int argc, const char *argv[] ) { Access *a = [[Access alloc] init]; // works a->publicVar = 5; // notice the -> notation here. printf( "public var: %i\n", a->publicVar ); // doesn't compile //a->privateVar = 10; //printf( "private var: %i\n", a->privateVar ); [a release]; return 0; }

O BJECT -O RIENTED F EATURES  Inheritance (not multiple)  Override parent class methods by putting implementation in the child class  In a Square.h file… #import Square: Rectangle // square inherits from rectangle -(Square*) initWithSize: (int) s; // constructor -(void) setSize: (int) s; -(int)

B RACKETS VS. D OTS IS C ONFUSING  Bracket Notation  [object method]  Or dot (.) notation  object.method  [frac setNumerator: 1] becomes frac.Numerator = 1

MISC  The symbol is used to introduce Objective-C keywords so they won’t conflict with the C or C++ stuff  You will see a lot of “NS” – “NextStep” – code from back in the day.  YES and NO instead of TRUE and FALSE