Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 5 6 October 2008
Chair of Software Engineering 2 Assignments Deadline for assignment 3 on Thursday
Chair of Software Engineering 3 Today’s learning goals Declaring the type of an object Reading class interfaces and understanding feature calls
Chair of Software Engineering Declaring the type of an object The type of any object you use in your program must be declared somewhere Where can such declarations appear in a program? in feature declarations: argument types return type for queries in the local clauses of routines where you declare any objects that only the routine needs and knows
Chair of Software Engineering 5 Queries and commands Command: Query (attribute): Query (function): commandname (a1: T1; a2, a3: T2) is -- Comment require … do … ensure … end attributename: TYPE -- Comment functionname (a1: T1; a2, a3: T2): TYPE is -- Comment require … do … ensure … end optional mandatory
Chair of Software Engineering 6 Feature declaration vs. feature call Feature declaration feature_name_1 (arg1: A; arg2: B; …) is -- Comment do … end feature_name_2 (arg1: C; arg2: D; …): SOME_RETURN_TYPE is -- Comment do … end Feature call object1.feature_name_1 (a1, b1) object2 := object3.feature_name_2 (c1, d1) The underlined entities denote objects The red entities are type names
Chair of Software Engineering 7 Objects or classes? (example) class game... feature -- Access map_name: string -- Name of the map to be loaded for the game last_player: player -- Last player that moved... (continued on next slide) Hands-On
Chair of Software Engineering 8 Objects or classes? (example) ... feature -- Status report is_occupied (a_location: traffic_place): boolean is -- Check if `a_location' is occupied by some flat hunter. require a_location_exists: a_location /= Void local old_cursor: cursor do Result := False -- Remember old cursor position. old_cursor := players.cursor... Hands-On
Chair of Software Engineering 9 Objects or classes? (example) -- Loop over all players to check if one occupies -- `a_location'. from players.start -- do not consider estate agent, hence skip the first -- entry in `players'. players.forth until players.after or Result loop if players.item.location = a_location then Result := True end players.forth end -- Restore old cursor position. players.go_to(old_cursor) end Hands-On
Chair of Software Engineering 10 Objects or classes? Terms “class” and “type” used interchangeably for now Queries (attributes and functions) have a return type. However, when executing the query, you get an object. You call features on objects. Arguments given to a routine are objects, they all have certain types. Local variables declared in a routine are objects, but they all have certain types. Result and Current are objects. Inheritance is a relation defined between classes.
Chair of Software Engineering 11 Queries and commands Command: Query (attribute): Query (function): commandname (a1: T1; a2, a3: T2) is -- Comment require … do … ensure … end attributename: TYPE -- Comment functionname (a1: T1; a2, a3: T2): TYPE is -- Comment require … do … ensure … end optional mandatory
Chair of Software Engineering 12 Query or command? (example) class GAME... feature -- Access map_name: STRING -- Name of the map to be loaded for the game last_player: PLAYER -- Last player that moved... (continued on next slide) Hands-On
Chair of Software Engineering 13 Query or command? (example)... feature -- Status report is_occupied (a_location: TRAFFIC_PLACE): BOOLEAN is -- Check if `a_location' is occupied by some flat hunter. require a_location_exists: a_location /= Void local old_cursor: CURSOR do Result := False -- Remember old cursor position. old_cursor := players.cursor... Hands-On
Chair of Software Engineering 14 Query or command? (example) -- Loop over all players to check if one occupies -- `a_location'. from players.start -- do not consider estate agent, hence skip the first -- entry in `players'. players.forth until players.after or Result loop if players.item.location = a_location then Result := True end players.forth end -- Restore old cursor position. players.go_to(old_cursor) end Hands-On