Using Objects 1 of 22 Computer Programming Object Classes.

Slides:



Advertisements
Similar presentations
When is Orientated Programming NOT? Mike Fitzpatrick.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Chapter 41 Defining Classes and Methods Chapter 4.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
© 2006 Pearson Education Using Objects 1 of 32 USING OBJECTS What are Objects? Properties and Capabilities Classes and Instances Making Instances A Sample.
Chapter Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Lecture 1 Introduction to Computers and Object-
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 10 Classes Continued
Understanding class definitions Looking inside classes.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
State,identity and behavior of objects Sem III K.I.R.A.S.
TCU CoSc Introduction to Programming (with Java) Getting to Know Java.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 8: Modelling Interactions and Behaviour.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (1/2)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 5 Introduction to Defining Classes
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Introduction to Object-Oriented Programming Lesson 2.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
September 7, 2006Using Objects 1 of 32 USING OBJECTS What are Objects? Properties and Capabilities Classes and Instances Making Instances A Sample Program.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Programming Fundamentals Enumerations and Functions.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Topic 4: Distributed Objects Dr. Ayman Srour Faculty of Applied Engineering and Urban Planning University of Palestine.
Understand the Fundamentals of Classes
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Concepts -I
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Object Oriented Programming in java
Defining Classes and Methods
Data Structures & Algorithms
Introduction to Object-Oriented Programming
Defining Classes and Methods
CS2013 Lecture 7 John Hurley Cal State LA.
Introduction to Computer Science and Object-Oriented Programming
Introduction to Classes and Objects
Presentation transcript:

Using Objects 1 of 22 Computer Programming Object Classes.

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

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?

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

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

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?

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

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)

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

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

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

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 0x x x x ( vertical representation ) ( horizontal representation of memory )

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

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

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

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

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.

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; };

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); }

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; }

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); }

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; }