180 likes | 270 Views
Introduction to Programming. Bertrand Meyer Exercise Session 4 30 September 2008. Learning goals. You’ll have an idea how program execution works You understand what an object and a class are You will see an analogy for objects and classes. Object-orientied programming?.
E N D
Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008
Learning goals • You’ll have an idea how program execution works • You understand what an object and a class are • You will see an analogy for objects and classes
Object-orientied programming? • The general idea: At runtime, a program consists of objects that have properties and interact (similar to people). • An object has features, that can be called by other objects: • Queries: answering a question • Commands: executing actions • Features are defined in classes • An object has a type that is described in a class
Who are Adam and Eve? • An object needs to be created by another object • In Eiffel: • An object is defined as the root object • This object creates other objects, which in turn create other objects, etc.
Playing a game • Assume: • I am the root object • My behavior is defined in a class DIRECTOR class DIRECTOR create prepare_and_play feature -- Main execution prepare_and_play is do -- See later end
You are an ACROBAT When you are asked to Clap, you will be given a number. Clap your hands that many times. When you are asked to Twirl, you will be given a number. Turn completely around that many times. When you are asked for Count, announce how many actions you have performed. This is the sum of the numbers you have been given to date.
You are an ACROBAT_WITH_BUDDY When you are given this card, you will get someone else as your Buddy. When you are asked to Clap, you will be given a number. Clap your hands that many times. Pass the same instruction to your Buddy. When you are asked to Twirl, you will be given a number. Turn completely around that many times. Pass the same instruction to your Buddy. If you are asked for Count, ask your Buddy and answer with the number he tells you.
You are an AUTHOR When you are asked to Clap, you will be given a number. Clap your hands that many times. Say “Thank You.” Then take a bow (as dramatically as you like). When you are asked to Twirl, you will be given a number. Turn completely around that many times. Say “Thank You.” Then take a bow (as dramatically as you like). When you are asked for Count, announce how many actions you have performed. This is the sum of the numbers you have been given to date.
You are a CURMUDGEON When given any instruction (Twirl or Clap), ignore it, stand up and say (as dramatically as you can) “I REFUSE” If you are asked for Count, always answer with 0. Then sit down again if you were originally sitting.
Let’s play the game • See blackboard
ACROBAT Hands-On class ACROBAT feature clap (n: INTEGER) is do -- Clap `n’ times and adjust `count’. end twirl(n: INTEGER) is do -- Twirl `n’ times and adjust `count’. end count: INTEGER end What are the commands/queries?
ACROBAT_WITH_BUDDY class ACROBAT_WITH_BUDDY inherit ACROBAT redefine twirl, clap, count end create make feature make (p: ACROBAT) is do -- Remember `p’ being the buddy. end clap (n: INTEGER) is do -- Clap `n’ times and forward to buddy. end … (continued on next page)
ACROBAT_WITH_BUDDY (2) Hands-On … (continued from last page) twirl (n: INTEGER) is do -- Twirl `n’ times and forward to buddy. end count: INTEGER is do -- Ask buddy and return his answer end buddy: ACROBAT end What are the commands/queries? What features are defined?
AUTHOR Hands-On class AUTHOR inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) is do -- Clap `n’ times and bow. end twirl(n: INTEGER) is do -- Twirl `n’ times and bow. end end What features are defined?
CURMUDGEON class CURMUDGEON inherit ACROBAT redefine clap, twirl end feature clap (n: INTEGER) is do -- Say “I refuse”. end twirl(n: INTEGER) is do -- Say “I refuse”. end end
Concepts seen Eiffel Game Class Telling person to behave according to a specification Objects People Interface What queries What commands Polymorphism Telling different people to do the same has different outcomes Instruction Telling a person to do something Expression Asking a question to a person Arguments E.g. how many times to clap
Concepts seen Eiffel Game Inheritance All people were some kind of ACROBAT Creation Persons need to be born and need to be named Return value E.g. count in ACROBAT_WITH_BUDDY Entities Names for the people Chains of feature calls E.g. partner1.buddy.clap (2)
Summary • To call a feature on another object, the caller must know the callee directly or indirectly. Direct: object.command Indirect: object1.query1.command • To be able to execute a command, the generating class must define it. By looking at the class you can find out these things.