Download presentation
Presentation is loading. Please wait.
1
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 11/16/2009
2
James Atlas Overview Review Review Inheritance Cell Arrays Polymorphism Polymorphism What is it? Why use it? How does it work in Matlab?
3
James Atlas Inheritance The is a relationship The is a relationship Inheritance Hierarchy Inheritance Hierarchy state (properties) behavior (methods) Superclass Superclass Subclass Subclass Animal ReptileBird
4
James Atlas Cell Arrays Normal array Normal array zeros(1,5) is 5 doubles [1 2 3 4] is 4 doubles Cell array Cell array {1 2 3 4} is 4 cells, each with a 1x1 array of double values inside
5
James Atlas What is a cell? A cell is like a window into a room, and inside the room can be data of any size A cell is like a window into a room, and inside the room can be data of any size A cell array is like a hallway with many doors A cell array is like a hallway with many doors 15437 52 Student age=19 name=‘Joe’ credits=78 ‘hello’ 0 1.1 normal arraycell array
6
James Atlas Polymorphism: What is It?
7
James Atlas Polymorphism: What is It? One name, many forms One name, many forms Capability to perform different actions based on the object instance
8
James Atlas Polymorphism: What is It? One name, many forms One name, many forms Capability to perform different actions based on the object instance Each band member is a Musician Each band member is a Musician We could tell all band members to Play Play for a drummer would perform a different action than Play for a guitarist
9
James Atlas Polymorphism: Why use it? Easily extensible systems Easily extensible systems Add new features with low impact on system
10
James Atlas Polymorphism: Why use it? Easily extensible systems Easily extensible systems Add new features with low impact on system For example, we are hosting American Idol for Musicians For example, we are hosting American Idol for Musicians In Season 1 we have guitarists, drummers, and pianists In Season 2 we add keyboardists, trumpeters and bagpipers
11
James Atlas Polymorphism: Why use it? Easily extensible systems Easily extensible systems Add new features with low impact on system For example, we are hosting American Idol for Musicians For example, we are hosting American Idol for Musicians In Season 1 we have guitarists, drummers, and pianists In Season 2 we add keyboardists, trumpeters and bagpipers We don’t need to change the competition format or hosting protocol!
12
James Atlas Polymorphism: How does it work in Matlab? Overriding Overriding Matlab constructs Matlab constructs Methods (the behavior) Classes Abstract tag
13
James Atlas Shape.m Class classdef Shape properties properties center_x center_x center_y center_y color color end end methods (Abstract) methods (Abstract) result = area(shape) result = area(shape) handle = draw(shape) handle = draw(shape) end endend
14
James Atlas Circle.m Class classdef Circle < Shape properties properties radius radius end end methods methods function circle = Circle(center_x, center_y, radius, color) function circle = Circle(center_x, center_y, radius, color) circle.center_x = center_x; circle.center_x = center_x; circle.center_y = center_y; circle.center_y = center_y; circle.radius = radius; circle.radius = radius; circle.color = color; circle.color = color; end end...endend
15
James Atlas Circle.m Class (cont’) classdef Circle < Shape...... methods methods... function handle = draw(circle) function handle = draw(circle) handle = rectangle('Position', [... handle = rectangle('Position', [... circle.center_x - circle.radius,... circle.center_x - circle.radius,... circle.center_y - circle.radius,... circle.center_y - circle.radius,... 2 * circle.radius,... 2 * circle.radius,... 2 * circle.radius],... 2 * circle.radius],... 'Curvature', [1 1], 'FaceColor', circle.color); 'Curvature', [1 1], 'FaceColor', circle.color); end end...endend
16
James Atlas Circle.m Class (cont’) classdef Circle < Shape...... methods methods... function result = area(circle) function result = area(circle) result = pi * circle.radius * circle.radius; result = pi * circle.radius * circle.radius; end endendend
17
James Atlas Example: Shape sorter Create rectangle and triangle Create rectangle and triangle Create a cell array of shapes Create a cell array of shapes Update each shape’s x and y center Update each shape’s x and y center Draw shapes and then put them in order by area Draw shapes and then put them in order by area Animate swaps Animate swaps
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.