1 / 18

Introduction to Programming

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?.

eitan
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Bertrand Meyer Exercise Session 4 30 September 2008

  2. 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

  3. 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

  4. 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.

  5. 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

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. Let’s play the game • See blackboard

  11. 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?

  12. 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)‏

  13. 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?

  14. 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?

  15. 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

  16. 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

  17. 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)

  18. 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.

More Related