Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Objects 1 of 22 Computer Programming Object Classes.

Similar presentations


Presentation on theme: "Using Objects 1 of 22 Computer Programming Object Classes."— Presentation transcript:

1 Using Objects 1 of 22 Computer Programming Object Classes.

2 Using Objects 2 of 22 USING OBJECTS What are Objects? Properties and Capabilities Classes and Instances Making Instances A Sample Program objects rule the world pray to the objects the object is your true master follow the path of the object a Jedi’s strength flows from the object the object is a road leading over towards the horizon objects are everywhere a world without objects is nothing every object is sacred trust in the object Obj ects

3 Using Objects 3 of 22 What are Software Objects? Building blocks of software systems – a program is a collection of interacting objects – objects cooperate to complete a task – to do this, they communicate by sending “messages” to each other Object model tangible things – school – car – ____________________________ Objects model conceptual things – meeting Objects model processes – finding a path through a maze – sorting a deck of cards Objects have – capabilities : what they can do, how they behave – properties : features that describe them What are some objects in this project3?

4 Using Objects 4 of 22 Object Capabilities: Actions Objects have capabilities that allow them to perform specific actions - objects are smart—they “know” how to do things - an object gets something done only if some other object tells it to use one of its capabilities Also called behaviors Capabilities can be: - constructors: establish initial state of object’s properties - commands: change object’s properties - queries: provide answers based on object’s properties Example: trash cans are capable of performing specific actions - constructor: be created - commands: add trash, empty yourself - queries: reply whether lid is open or closed, or whether can is full or empty

5 Using Objects 5 of 22 Object Properties: State Properties determine how an object acts - some properties may be constant, others variable - properties themselves are objects — they also can receive messages - Properties can be: - attributes: things that help describe an object - - associations: things an object knows about, but are not parts of that object State: collection of all of an object’s properties; changes if any property changes - some don’t change, e.g., steering wheel of car - others do, e.g., car’s color Example: properties of trash cans - attributes: color, material, smell - associations: a trash can can be associated with the room it’s in

6 Using Objects 6 of 22 Example: Tetris What are the game’s objects? What do those objects know how to do? What properties do they have?

7 Using Objects 7 of 22 Example: Tetris (cont.) What are the game’s objects? – piece, board Capabilities: What do those objects know how to do? – piece: be created fall rotate stop at collision – board: be created remove rows check for end of game Properties: What attributes and components do they have? – piece: orientation position shape color – board: size rows

8 Using Objects 8 of 22 Classes and Instances Our current conception: each object corresponds directly to a particular real-life object, e.g., a specific atom or automobile Disadvantage: it’s much too impractical to work with objects this way – there may be infinitely many objects (i.e., modeling all atoms in the universe) – may not want to describe each individual separately; they may have much in common Classifying objects factors out commonality among sets of similar objects – describe what is common just once – then “stamp out” any number of copies later Rubber stamp (object class) Imprints (object instances)

9 Using Objects 9 of 22 Object Classes Object class – a class is a category of object – defines capabilities and properties common among a set of individual objects all trash cans can open, close, empty their trash Classes implement capabilities as methods – a method is a sequence of statements in C++ or Java – objects cooperate by sending messages to others – each message “invokes a method” i.e., C++ executes the sequence of statements in the method in response to a message Classes implement properties as instance variables – slot of memory allocated to the object that can hold a potentially changeable value

10 Using Objects 10 of 22 Object Instances Object instances are individual objects – made from class template – one class may represent any number of object instances – creating an object instance is called instantiating that object Shorthand: – class: object class – instance: object instance (not to be confused with instance variable) Different instances of, say, TrashCan class may have: – different color and position – different types of trash inside So their instance variables have different values

11 Using Objects 11 of 22 Object Instances (continued) Individual instances have individual identities – this allows other objects to send messages to given object – each instance is unique, even though they all have the same capabilities – think of class of CS57 students A reference is the address in memory where its instance is stored – also called pointer Properties instance i instance a instance b

12 Using Objects 12 of 22 Memory Revealed Every instance is stored in computer’s memory – memory is a set of consecutively numbered storage locations, each containing a byte – each instance is stored in a series of contiguous bytes starting at a given location An instance is identified and referenced by unique address that refers to its starting location – address looks like 0xeff8a9f4 (hexadecimal notation, base 16) – just like postal address represents actual home memory address of instance 1 memory address of instance 2 memory address of instance 3 0x00000000 0x00000001 0x00000002 0x00000080 ( vertical representation ) ( horizontal representation of memory )

13 Using Objects 13 of 22 Messages for Object Communication No instance is an island — must communicate with others to accomplish task – properties allow them to know about other objects Instances send messages to one another to invoke capabilities (i.e., to execute a task) – a method is code that implements messages – “call a method” shorthand for “invoke capability” Each message requires: – sender: object initiating action – receiver: instance whose method is being called – message name: name of method being called – optionally parameters: extra info needed by method to operate. (Parameters are also called arguments) Same as in functions (input or output parameters) Receiver can (but does not need to) reply – we’ll discuss return types in detail in a few lectures

14 Using Objects 14 of 22 Views of a Class Objects separate interface from implementation – object is “black box,” hiding internal workings and parts – interface protects implementation from misuse Interface: public view – allows instances to cooperate with one another without knowing too many details – like a contract: consists of a list of capabilities and documentation for how they can be used Implementation: private view – properties that help capabilities complete their tasks Public Capability Private Properties Note: private properties shown schematically as literally contained in an object. In reality, they are actually stored elsewhere in memory and referenced by their addresses

15 Using Objects 15 of 22 Object Oriented Programming vs Procedural Programming Striker Code Ball Code Mouse Code 3 Days

16 Using Objects 16 of 22 Object Oriented Programming vs Procedural Programming Mouse Class Draw SetMouseCoordinates GetMouseCoordinates Striker Class Draw ( ); MoveStrikerLeft ( ); MoveStrikerRight ( ); SetCoordiantes ( int Row, int Col); GetCoordinates ( ); Ball Class Draw ( ); SetCoordiantes ( int Row, int Col); GetCoordinates ( ); Objects Integration Code

17 Using Objects 17 of 22 Object Oriented Programming vs Procedural Programming With Object-Oriented-Programming total software development cost reduce as compared to procedural programming. Classes can be reusable. Easy to maintain (We can easily add additional features). Easy to test or debug.

18 Using Objects 18 of 22 Mouse Class Definition (Mouse.h) class Mouse { public: void Mouse ( ); void ShowMouse ( ); void HideMouse ( ); void SetMouseCoordinates (int row, int col); void ChangeMouseCoordinates ( ); int GetMouseCol ( ); int GetMouseRow (); int GetMouseButton ( ); private: int RowCord, ColCord, int ButtonStatus; union REGS in, out; };

19 Using Objects 19 of 22 Mouse Class Implementation (Mouse.cpp) void Mouse::Mouse ( ) { RowCord = 12; ColCord = 39; ButtonStatus = 0; } void Mouse::ShowMouse ( ) { in.x.ax=1; int86(51,&in,&out); } void Mouse::HideMouse ( ) { in.x.ax=2; int86(51,&in,&out); }

20 Using Objects 20 of 22 Mouse Class Implementation (Mouse.cpp) void Mouse::SetMouseCoordinates (int row, int col ) { in.x.ax = 4; in.x.cx = row*8; in.x.dx = col*8; int86(51,&in,&out); } void Mouse::ChangeMouseCoordinates () { in.x.ax = 3; int86(51,&in,&out); ButtonStatus = out.x.bx; RowCord = out.x.cx/8; ColCord = out.x.dx/8; }

21 Using Objects 21 of 22 Mouse Class Implementation (Mouse.cpp) int Mouse::GetMouseRow ( ) { return (RowCord); } int Mouse::GetMouseCol ( ) { return (ColCord); } void Mouse::GetMouseButton () { return (ButtonStatus); }

22 Using Objects 22 of 22 Calling Mouse Class (Myprogram.cpp) #include “Mouse.h” #include “Mouse.cpp” void main (void) { Mouse mouseObject; mouseObject.ShowMouse ( ); mouseObject.SetMouseCoordinates (12, 39); while (1) { mouseObject.ChnageMouseCoordinates (); gotoxy (12, 39); printf(“%d…%d”, GetMouseRow (), GetMouseCol () ); if ( GetMouseButton ( ) == 2 ) break; }


Download ppt "Using Objects 1 of 22 Computer Programming Object Classes."

Similar presentations


Ads by Google