Working with classes and objects COSC346

Similar documents
Polymorphism COSC346

Data Structures (list, dictionary, tuples, sets, strings)

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

An Introduction to C++

Mobile Application Programming. Swift Classes

JAVA MOCK TEST JAVA MOCK TEST II

Chapter 4 Defining Classes I

Interview Questions of C++

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Mobile Application Programming. Swift Classes

Chapter 4: Writing Classes

CSC148 Intro. to Computer Science

CS304 Object Oriented Programming Final Term

Principles of Object Oriented Programming. Lecture 4

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1

Introduction to Programming Using Java (98-388)

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Recitation #2 Abstract Data Types, Collection Classes, and Linked List

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

C++ (classes) Hwansoo Han

Anatomy of a Class Encapsulation Anatomy of a Method

ADTs & Classes. An introduction

Ticket Machine Project(s)

Chapter 5 Object-Oriented Programming

Implementing Subprograms

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

And Even More and More C++ Fundamentals of Computer Science

CSC102 INTRO TO PROGRAMMING WITH PYTHON REVIEW MICHAEL GROSSBERG

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

COMP 401 Spring 2013 Midterm 1

CLASSES AND OBJECTS IN JAVA

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

COP 3330 Final Exam Review

G52CPP C++ Programming Lecture 9

CS1004: Intro to CS in Java, Spring 2005

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Object Oriented Programming

The Decaf Language. 1 Lexical considerations

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

UNIVERSITY OF SWAZILAND

Software Design and Analysis for Engineers

Memory management COSC346

C++ 8. Constructors and Destructors

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

Java Object Oriented Design. CSC207 Fall 2014

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CPSC 427: Object-Oriented Programming

More C++ : Vectors, Classes, Inheritance, Templates

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Special Member Functions

Encapsulation. Mason Vail Boise State University Computer Science

Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Chapter 5: Classes and Objects in Depth. Information Hiding

LECTURE 03 LINKED LIST

Object Oriented Programming COP3330 / CGS5409

Introductory ios Development

COMP 250 Winter 2011 Reading: Java background January 5, 2011

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Review (Basic Objective-C)

Object-Oriented Programming

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Swift. Introducing swift. Thomas Woodfin

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Object Oriented Programming in C#

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Chapter 13 Abstract Classes and Interfaces

Padasalai.Net s Model Question Paper

Recap of OO concepts. Objects, classes, methods and more. Mairead Meagher Dr. Siobhán Drohan. Produced by:

ECE 122. Engineering Problem Solving with Java

Object Oriented Programming

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

CMSC 132: Object-Oriented Programming II

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Inheritance (IS A Relationship)

Programming Languages and Techniques (CIS120)

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

ENCAPSULATION AND POLYMORPHISM

The Decaf language 1

C++ (Non for C Programmer) (BT307) 40 Hours

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Porting Objective-C to Swift. Richard Ekle

COEN244: Class & function templates

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

Object-Orientation. Classes Lecture 5. Classes. State and Behaviour. Instance Variables. Object vs. Classes

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Fast Introduction to Object Oriented Programming and C++

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Encapsulated data types

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Transcription:

Working with classes and objects COSC346

Initialisation An object should be self-contained: independent and selfsufficient Should allocate resources (memory) required for its operation Should initialise its member variables to appropriate values Constructors These are special methods invoked upon object creation The place where the internal state of the object can be initialised Typically these methods carry the same name as the class Can take parameters, which allow user-defined initialisation Note: Destructors are methods that are automatically invoked when object is released from the memory more about this when we discuss memory management. 2

Initialisation class Complex { var real: Float var imag: Float init() { self.real = 0.0 self.imag = 0.0 var x: Complex x = Complex() Initialiser is invoked with the name of the class followed by initialiser arguments in parentheses In constructors are referred to as initialisers Any class that uses stored properties must implement at least one initialiser Compiler will give an error if a property is not initialised An object instance must be created through an initialiser Compiler will not allow the use of an object that hasn t been initialised 3

Initialisation multiple initialisers class Complex { var real: Float var imag: Float init() { self.real = 0.0 self.imag = 0.0 init(real: Float, imag: Float) { self.real = real self.imag = imag var x: Complex x = Complex() x = Complex(real: 1.0, imag: 2.3) There can be more than one initialiser The arguments in the initialisation call determine which initialiser is used The arguments (if there are any) must be named in the initialisation call 4

Initialisation multiple initialisers class Complex { var real: Float var imag: Float init() { self.real = 0.0 self.imag = 0.0 init(real: Float, imag: Float = 0.0) { self.real = real self.imag = imag Initialiser arguments can be declared with a default value: these arguments can be omitted from initialisation call var x: Complex x = Complex() x = Complex(real: 1.0, imag: 2.3) var y: Complex = Complex(real: -4.4) 5

Designated initialiser Often classes will have several available initialisation methods The designated initialiser is a method that is eventually invoked by all other initialisation methods Use of a designated initialiser lowers the chance of initialisation errors 6

Initialisation - multiple initialisers class Complex { var real: Float var imag: Float convenience init() { self.init(real: 0.0) Designated Convenience init(real: Float, imag: Float = 0.0) { self.real = real self.imag = imag var x: Complex x = Complex() x = Complex(real: 1.0, imag: 2.3) var y: Complex = Complex(real: -4.4) In an initialiser that doesn t write to properties directly, but initialises them through another initialiser is referred to as a convenience initialiser 7

Member/internal variables Member/internal variables are the variables encapsulated inside a class. There are two types of member variables: Instance variables each object instance carries a dedicated copy of these variables, and so values can different from object to object Class variables shared by all objects of a given class: change in value affects every instance x=2 x=3 Object instance scope Object instance scope Class scope x=2 8

Member/internal variables Instance variables: Stored properties their values may differ for different object instances of the class Class variables: Static stored properties values are shared by all objects of a given class class ClassA { static var firsttime: Bool = true; //Class variable var someproperty: Any //Instance variable init(x: Any) { if(classa.firsttime) { //Do something that can be done //only once, regardless how many //objects of this class are created ClassA.firstTime = false self.someproperty = x; 9

Methods Instance methods Methods invoked on class instance Can access instance variables for read/write Class methods Methods that can be used without creating an instance of the class Cannot access instance variables class Fraction { var num: Int; var den: Int; init(num : Int, den : Int) { self.num = num self.den = den func add(_ f: Fraction) { self.num = self.num*f.den + self.den*f.num self.den = self.den*f.den var u = Fraction(num: 2, den: 3) var v = Fraction(num: -7, den: 9) u.add(v) Operation on an instance var f: Fraction f = Fraction.add(u, to: v) Operation on the class static func add(_ f1: Fraction, to f2: Fraction) -> Fraction { return Fraction (num: f1.num*f2.den + f1.den*f2.num, den: f1.den*f2.den) 10

Access Control Generally in access control is class based: Private methods and member variables Accessible only from the code within class definition Visible to the programmer writing the class code, not to the programmer using objects of that class Protected methods and member variables A bit like private, except visible to derived classes more about this in the lecture on inheritance Public methods and member variables Accessible from anywhere Visible to the programmer writing the class as well as the programmer using objects of that class More restrictive Less restrictive 11

Access Control In access control is file/module based: Private methods and member variables Accessible only from the file where the class has been defined Visible to the programmer editing the class source file, even if working with object instances outside the class definition Internal methods and member variables (the default) Accessible from any file that is part of the module where the class is defined where the class has been defined Visible in any source file to the programmer writing within the module (project) where the class is defined Once the module is distributed as a framework, and included in another project, the internal methods and variables are not accessible to the programmer using objects of that class Public methods and member variables Accessible from any source file Visible to the programmer writing the class as well as the programmer using objects of that class in any source file of the project More restrictive Less restrictive 12

Accessor methods Sometime it makes sense to control the access to object s state The state of the object can be made private, so that the user of the object can t access it directly Setter & getter methods are the interface to the state of the object These methods can check for indexes out of bounds, invalid values, etc., to ensure that state doesn t get corrupted http://www.evinw.com 13

Accessor methods class Fraction { private var _numhidden: Int = 0 private var _denhidden: Int = 1 init(num : Int, den : Int) { self._numhidden = num; self._denhidden = den func getnum() -> Int { return _numhidden Private state Getter and setter for _numhidden Generic setters and getters func setnum(_ newvalue: Int) { _numhidden = newvalue func getden() -> Int { return _denhidden Getter and setter for _denhidden func setden(_ newvalue: Int) { assert(newvalue!= 0, "Can't set den to 0!") _denhidden = newvalue var f: Fraction = Fraction(num: 1, den: 3) f.setnum(2) f.setden(3) print("f=\(f.getnum())/\(f.getden())") 14

Accessor methods class Fraction { private var _numhidden: Int = 0; private var _denhidden: Int = 1; init(num : Int, den : Int) { self._numhidden = num; self._denhidden = den var num: Int { get { return _numhidden set(newvalue) { _numhidden = newvalue Private state Getter and setter for _numhidden setters and getters as computed properties var den: Int { get { return self._denhidden set(newvalue) { assert(newvalue!= 0, "Can't set den to 0!") self._denhidden = newvalue var f: Fraction = Fraction(num: 1, den: 3) f.num = 2 f.den = 3 print("f=\(f.num)/\(f.den)") Getter and setter for _denhidden 15

Overloading Same method name but different implementations for different parameter signatures Constructor overloading is probably the most ubiquitous use of overloading class Fraction { var num: Int var den: Int init(num: Int, den: Int) { self.num = num self.den = den convenience init(string: String) { var num: Int = 0; var den: Int = 1; var tokens = string.components(separatedby:"/") if tokens.count > 0 { if let n = Int(tokens[0]) { num = n if tokens.count > 1 { if let d = Int(tokens[1]) { den = d self.init(num: num, den: den) var x: Fraction = Fraction(num: 1, den: 2) var y: Fraction = Fraction(string: "4/3") 16

Object reference What happens when you create an object instance? First, you create an object reference if it s not initialised it doesn t point anywhere var f: Fraction f Then, you create an object instance and set the reference value so it points to the address where the object is located f = Fraction(num: 1, den: 3) f 00000000001079e0 17

Object reference What happens when you assign an object reference to another reference? var y: Fraction = f f 00000000001079e0 y 00000000001079e0 Two references point to the same object instance: operation through either reference changes the state of the object 18

Copying objects Create a new object instance and copy the values of all instance variables var y: Fraction = f.copy() f 00000000001079e0 y 0000000000108fc0 Two references now point to different object instances: operation through one reference does not affect the object associated with the other reference 19

Copying objects Create a new object instance and copy the values of all instance variables class Fraction { private var num: Int; private var den: Int; init(num : Int, den : Int) { self.num = num self.den = den func copy() -> Fraction { return Fraction(num: self.num, den: self.den); var f: Fraction = Fraction(num: 1, den: 3) var y = f.copy() 20

Copying objects What if an instance variable is an object? Shallow copy copy its reference Deep copy create new instance of the internal object and copy the state 21

Comparing object references Do you mean to check whether two references point to the same object instance? var f: Fraction var y: Fraction... if y === f { print("y and f refer to the same object") 22

Comparing objects values What does it mean for different object instances to be equal? func == (left: Fraction, right: Fraction) -> Bool { if (left.num == right.num) && (left.den == right.den) { return true else { return false var f: Fraction var y: Fraction... if y == f { print("objects are the same") 23

Comparing objects by value Can the objects be ordered in some fashion? func < (left: Fraction, right: Fraction) -> Bool { if left.decimal < right.decimal { return true else { return false var f: Fraction var y: Fraction... if y < f { print("y is smaller than f") else { print("f is smaller than y") Computed property that calculates a float value from Fraction s state 24

Mutable and immutable Mutable object internal state can be modified at any point Immutable object internal state does not change after initialisation: a read only object 25

Mutable and immutable Declaring stored properties as constant makes the class objects immutable Properties can only be set once in an initialiser class Fraction { let num: Int; let den: Int; init(num : Int, den : Int) { self.num = num self.den = den Stored properties declared as constants The only place where the stored properties can be set func copy() -> Fraction { return Fraction(num: self.num, den: self.den); static func add(f1: Fraction, to f2: Fraction) -> Fraction { return Fraction (num: f1.num*f2.den + f1.den*f2.num, den: f1.den*f2.den) var f: Fraction = Fraction(num: 1, den: 3) var y = f.copy() y.num = 2 print ("f=\(f.num)/\(f.den)")! Cannot assign to num in y