Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Object Programming Barry Sosinsky Valda Hilley Programming the Web Chapter.

Similar presentations


Presentation on theme: "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Object Programming Barry Sosinsky Valda Hilley Programming the Web Chapter."— Presentation transcript:

1 McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Object Programming Barry Sosinsky Valda Hilley Programming the Web Chapter 6

2 6-2 Learning Objectives To learn why object programming offers efficiencies. To understand what an object is, and how it uses properties and methods. To examine some basic examples in code on how objects, classes and other OOP structures are expressed. To get an introduction to the construction of object programs To learn about object relationships and models

3 6-3 What is Object-Oriented Programming? In an object-oriented programming language, logical entities called objects perform a set of activities which define the object’s behavior. The language is implemented by sending messages to objects. The goal of object programming is to create the appropriate set of objects necessary to perform the tasks you require – and then program those objects to perform the actions required to do those tasks.

4 6-4 What Do Programmers Value in Object Oriented Programming? Reusability is the key to efficient and practical programming. Portability lets a program run on different computers or a module run in different applications. Object programming also offers two very important properties: modularity and information hiding

5 6-5 What Do Programmers Value in Object Oriented Programming? (2) Object oriented programming is used for higher-level programming because: –it enables the programmer to reuse more of the code that is developed –it enables programming to be modularized more easily than traditional languages. Modularization produces less debugging, more standardized routines, and an easier assignment of programming pieces of a project.

6 6-6 Programming: Some Essential Terminology Higher-level languages (like C, Java, or BASIC) require a compiler to transform their programming into a compact and efficient code used by the computer. An interpreter then converts the code line-by-line into a machine language (like Assembly), which communicates directly with a microprocessor. For languages such as C++, the ++ means that this language includes object support.

7 6-7 Porting Programming Languages to Different Platforms Just as operating system developers worked to make kernel code platform-independent, recent work on programming languages has also endeavored to do the same. Example: For Java to run on different platforms (such as Windows, Macintosh, or Linux), each platform must have a Java Virtual Machine (JVM) loaded

8 6-8 PEM Approach: Properties, Events, and Methods Objects have properties, which are attributes that describe the objects. A set of events can act on each object, and in an object oriented environment, each event can be coded to program a certain action. Additionally, each object has a set of methods, or functions, that it exposes that can be called on as a service by its reference in outside code.

9 6-9 What Is the Event Driven Model? Object programming makes a good companion to the event- driven programming model in use today in many operating systems. Most modern operating systems sit in a wait state, anticipating events that will trigger specific operations. Event driven programming is largely associated with the Graphical User Interface (GUI), the visual display seen on Macintosh, Windows, Unix and Linux desktops.

10 6-10 Web Programming Languages Many Web programming languages support the concept of objects: –VisualBasic –Java –C# (C Sharp) –Java Script –VBScript

11 6-11 What is an Object? An object is an entity or thing that: –you can name –you can assign a set of attributes or properties to –you can assign actions (methods) to. An object has two important features: state and behavior. An object is a software package of variables and methods

12 6-12 What is an Object? (3) Objects can represent either physical objects (like a screen) or ideas or actions (such as a timer). An object’s properties can include size, resolution, color, or whether to display or hide, among others. All objects have associated behaviors built into them.

13 6-13 Key Concept: Containment Containers allow for logical collections of objects to be made and named –For example, an input screen or form is a container of numerous objects –When a user goes to a checkout screen for a shopping cart on a Web site, they see text fields, radio buttons, and checkboxes. Each is an object.

14 6-14 Example: Event Log Let’s consider the simple example of an event log. The log needs to do the following things: 1.Store events of interest and their description. 2.Organize events into logical groups. 3.Provide a sorting function. 4.Provide a search function. 5.Provide a reporting function. 6.Create a print output. 7.Provide an export function. 8.Have the ability to clear the log or flush the log in some logical way.

15 6-15 An Event Log Object With Its Associated Methods

16 6-16 The Properties of an Object Properties (or attributes) are a data structure or container that is considered part of the object’s definition. Properties have an assigned data type. Examples include: INT, LONGINT, CHAR or TEXT, DOUBLE, and NUM, among others. A property can have only one data type. To store more than one data type, you need to define more than one property for that data.

17 6-17 A First Look at Properties // Log_entry string name; int event_type; string description; time timedate; Here we’ve defined four different properties for a Log_entry object, along with three different data types. If you were programming an object in code, then you might do so as follows: Event_Log.Log_entry.name = "Share Access" To use these properties programmatically you would address them using the following method: This specifies that the name property be set to "Share Access", and differentiates the name property of the Log_entry object.

18 6-18 Object Programming: The Visual Approach Objects consist of properties or attributes and methods. Visual languages typically express these using a property sheet. Objects also can be grouped into classes of similar objects. Programs often define a hierarchy of objects so that descendent objects can inherit the properties of their parents. As you create objects in your program – a process called instantiating an object – the properties of that object as defined by the class will follow.

19 6-19 An Object’s Property Dialog Box in Visual Basic 6

20 6-20 Instantiation Objects invoke a function by initiating a call to that function. Any function that instantiates the object and makes a value assignment is called a constructor. Most often the name of the object is part of the constructor, as the simple expression Log_entry() might be. A constructor has no return value assigned to it.

21 6-21 Program Calls and Functions A call is a program statement that names a function and, if necessary, passes appropriate values or variables (called parameters) to that function. A call to a function associated with an object requires that the object be named as part of the function call. Object_name.function_name (parameter list) EventLog.Sort(Time,Date)

22 6-22 Classes and Constructors Similarly grouped objects and their functions in an object oriented programming language are organized into groups called classes. A class can have one or more constructors for an object. –In some instances, the constructor assigns a static value –In another, the constructor might get a system variable such as your computer’s internal time stamp –The constructor might post a dialog box for a user to enter a value assigned to the object the constructor creates.

23 6-23 Classes and Constructors (2) Eventtype_cbox ( ) { title = "System Events" box1 = "Input/Output" default; box2 = "Memory access"; … } A constructor to instantiate a check box group might look like this: In instances where you want to use a parameter list for later assignment, the check box instantiation would look like this: Eventtype_cbox (string ctitle, boolean bbox1, Boolean bbox2, …) { title = ctitle box1 = bbox1 box2 = bbox2 … }

24 6-24 Classes and Functions The scope resolution operator ( :: ) is used to relate a function to a particular class. There could be numerous instances where objects have a sort ( ) function. In order to know which object the sort function is being associated with, you scope it. An example is shown below: integer event_log :: sort ( ) Here the scope operator identifies the sort function as a member of the event_log object class, and assigns an integer value as the return type.

25 6-25 Classes and Inheritance In programming, each object has only one parent. The purpose of inheritance is to reuse as much of a class’s definition for each object as possible. Any property or method that is standard should be defined. –Methods that are part of a class definition are called class methods. –Parameters or variables that are defined as part of a class are called class variables. –Altering a method or variable for an object creates an instance method or instance variable, respectively.

26 6-26 Classes and Inheritance (2) A child object's class is called a subclass. The parent object's class is called the superclass. When you create a subclass it inherits all of the superclass’s properties and methods. However, a subclass can not only override the inherited methods and properties, but subclasses can add additional properties and methods to suit its more specialized function. The purpose of an object hierarchy is to go from a general category to a specialized one.

27 6-27 Messages One object typically sends a message to another to have the target object perform one of its functions. When the function must act on a particular piece of data, parameters are sent as part of the message. Event_Log.Clear (Security); Target object The name of the method The value of the object to be passed to the method

28 6-28 File Organization Many languages use header files to store object definitions, as well as the headings for the functions associated with those objects. An implementation file stores the details and code for the functions that are going to be used. The program file stores the code for your program. A client file is then used to declare class objects, and to access the functions that the class supports. Client files are so named because they require the class definitions in order to execute.

29 6-29 File Organization: A Sample Client File. <-- log_event is the object log_event.Add_entry ( ); log_event.Input_data ( ); log_event.Sort (TimeDate);. This is a client file for the program used to create the Event Log described earlier: The log_event object is calling on three functions: Add_entry, Input_data, and Sort. The code for implementing each of these functions is hidden. The naming conventions help developers understand what is being accomplished in this part of the program,

30 6-30 Preprocessor Directives A number of languages support a feature called preprocessor directives, which lets you set up your programming environment prior to the running of the program. Preprocessor directives are often called Include files. They are so named because they are text files in languages like C++ that begin with an #Include statement.

31 6-31 Interfaces In an object environment, an interface lets unrelated objects interact with one another. For an interface to work in an object environment, its protocols must be available to any class – regardless of that class’s position in the object hierarchy. An interface’s protocols also lets you declare standard methods for one or more classes.

32 6-32 Interfaces and APIs A collection of methods exposed as part of an interface is called an Application Programming Interface or API. The program calls are expected to remain constant or change little over time. The purpose of an API is to provide the functionality of the objects it contains, while hiding the internal mechanisms or methods used to effect those functions.

33 6-33 Object Naming Conventions Object Name cFirstName iAge bPicture tTime Description a first name field containing character data an age field with integer values a BLOB (binary large object) field that contains a picture file a field with time data It's useful to apply naming conventions to objects, just as you would variables in a program. A common naming convention puts the data type into the name of the object. For example:

34 6-34 Object Naming Conventions (2) If you have a collection of screen objects: buttons, radio buttons, check boxes, text input fields, labels, and so forth, you could collect them into groups and assign them codes that make a particular object’s assignment obvious. ORiRadi_Button_Name O is the class R (for radio buttons) is the object type i denotes that this object stores an integer value

35 6-35 An Object Browser in Visual Basic 6 Environment

36 6-36 Object Collections If you have an object related to an unknown number of other objects, the link between them can be described as a collection. A collection can be named as a class (collection class). A namespace, or universe, of related object classes collected for a common purpose, is commonly called an object model. –Common Object Request Broker Architecture (CORBA) –Microsoft’s Component Object Model (COM)

37 McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. 6-37 The End


Download ppt "McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Object Programming Barry Sosinsky Valda Hilley Programming the Web Chapter."

Similar presentations


Ads by Google