1 / 23

Chapter 8: More About OOP and GUIs

Chapter 8: More About OOP and GUIs. Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake. 8.1 Classes and Objects.

stevegordon
Download Presentation

Chapter 8: More About OOP and GUIs

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. Chapter 8: More About OOP and GUIs Extended Prelude to Programming Concepts & Design, 3/e byStewart Venit and Elizabeth Drake

  2. 8.1 Classes and Objects A class is a datatype that allows us to create objects. It provides the definition for a collection of objects by describing its attributes (data) and methods (operations) that may be applied to that data. For example, consider the following definition of the class television: • Its attributes include brand name, model number, dimensions, screen size, and so on • Its methods include turning it on and off, changing the volume, changing channels, and so on

  3. Classes and Objects (continued) • An object is an instance of a class. With one class, you can have as many objects as required. • This is analogous to a variable and a data type. The class is the datatype and the object is the variable. Declare Number As Integer Declare ObjectName As ClassName

  4. Classes and Objects (continued) Class Cube Side As Real Volume As Real Subprogram SetSide(NewSide) Set Side = NewSide End Subprogram Subprogram ComputeVolume() Set Volume = Side ^ 3 End Subprogram Function GetVolume() As Real Set GetVolume = Volume End Function Function GetSide() As Real Set GetSide = Side End Function End Class

  5. Classes and Objects (continued) Here is an example of some code that makes an object of type Cube, and calls the functions that are part of the object. Main Program Declare Cube1 As Cube Write “Enter a positive number:” Input Side1 Call Cube1.SetSide(Side1) Call Cube1.ComputeVolume Write “Volume of a cube of side”, Cube1.GetSide Write “is ”, Cube1.GetVolume End Program

  6. The Constructor • A constructor is like a model or plan to construct an object. In programming, it is a special method, included in the class definition, which performs setup tasks when an object is created. • The constructor initializes the objects attributes and establishes conditions that do not change in the class.

  7. The Constructor (continued) • In the Cube example, a constructor might have given initial values to the two attributes – Side and Volume – of 1 and 1, respectively. • Then, when the main program calls the function ComputeVolume before the Side attribute is given a value, a value of 1 is already there, ready to be replaced by whatever value is necessary, and there will not be a problem.

  8. 8.2 More About Object-Oriented Programming • Procedural programming and object-oriented programming are not mutually exclusive. • The emphasis in procedural programming is to define the modules that represent processes and to attach these processes together to create a complex system. In OOP the emphasis is on the definition of the objects that are needed to solve a problem and how those objects interact with each other.

  9. Object-Oriented Programming (continued) • Benefits of object-oriented languages • OOP is better equipped to deal with extremely complex software than procedural languages • OOP uses inheritance, encapsulation, and polymorphism (to be discussed) • OOP became the natural way to program GUI interfaces

  10. Object-oriented Programming (continued) • Encapsulation— The incorporation of data and operations on that data into a single unit in such a way that the data can only be accessed through these operations. This is the fundamental idea behind classes and objects. • Inheritance — The ability to create new classes that are based on existing ones. The methods (operations) and attributes (data) of the original class are usually incorporated into the new class, together with methods and attributes specific to the latter. • Polymorphism — The ability to create methods that perform a general function, which automatically adapts itself to work with objects of different classes.

  11. Inheritance • In the previous example of Class Cube, we saw that a Cube has a Volume. The ‘has a’ relationship identifies the properties of the class. There is also an ‘is a’ relationship that defines inheritance. • For example a Truck is aVehicle. If we have defined a ClassVehicle, and included all of the properties that all Vehicles have, then it is very efficient to define new classes as specialized versions of existing classes. We say that the ClassTruck inherits the properties of the ClassVehicle.

  12. Polymorphism • Polymorphism allows a method to take on many definitions when applied to objects in a hierarchy of classes. • If a method or attribute that is already defined in the parent class is then redefined in the child class, the class closest to the calling instance takes precedence.

  13. Polymorphism In an imaginary parent (base) class called Vehicle, we might have methods to compute the Speed, given MilesTraveled and Time and to compute GasMileage given GasUsed and MilesTraveled. A child (derived) class called DeiselCar might use the Speed method from the parent class but use its own method for GasUsed since the formula for a deisel engine might differ from that of other engines.

  14. Developing an OOP Program The analysis phase of developing an OOP program entails: • Identifying the classes to be used in the program • Determining the attributes needed for the classes • Determining the methods needed for the classes • Determining the relationships among the classes In real-life programs there may hundreds or thousands of methods. Encapsulating them in objects makes it easier to manage them.

  15. Modeling Languages • An object modeling language is a standardized set of symbols used to model parts of an object-oriented software design • Unified Modeling Language (UML) is one of the primary languages • non-proprietary, general purpose • used to create an abstract model of a system

  16. 8.3 Graphical User Interface Revisited • Object-oriented programming is used heavily for the development of GUI applications. • Each of the GUI components, such as a button, a check box, a text box, etc. is a class • Each instance of a button, check box, a text box, etc. is an object of that class. • Some languages have GUI development as a central part of the language. • Visual Basic in particular is most well known for the rapid creation of GUIs for Windows platforms.

  17. GUIs • Although there is some variation among the languages and operating systems, the components that make up a GUI application are fairly standard, and the way to interact with them is also fairly standard. • For example, if there is a text box, the user will click once to put the cursor in the text box, and then type some information into that box.

  18. Window Components • Command buttons • Check boxes • Labels • Option buttons • Drop-down list boxes • List boxes • Text boxes

  19. 7.3 Functions Example GUI

  20. 8.4 Event-driven Programming • The use of a GUI is based on the user’s interaction with the interface – clicking buttons, pulling down menus, etc. • Each of these user actions generates an event. • Event-driven programming is not separate from procedural, nor from GUI programming, but the emphasis is on changing the flow of control in the program based on the events generated by the user.

  21. Event-driven Programming (continued) • In event-driven programming there is no main program. • When an event driven program is executed, it presents a GUI and waits for the user to generate an event to determine which code is executed next. • It is up to the designer of the program to create a GUI that only allows the user to generate events that make sense at any given time in the execution of the program.

  22. Event-driven Program Design The analysis phase of an event-driven program design is similar to that of an object-oriented program. Here are the basic steps involved: 1. Identify the windows needed in the program. 2. Determine the relationships among the windows; for example, which window can open another (so that the latter appears on the screen). Such relationships can be pictured in a flow diagram 3. For each window: • Determine the components (command buttons, text boxes, etc.) needed for that window. • Draw a rough sketch of the resulting window. • Determine the properties and methods needed for the window and each of its components. (The methods need not be ‘fleshed out” in this phase.)

  23. Defining A Class Class ClassOne Variable1 As Type Variable2 As Type Subprogram Method1(parameter) … End Subprogram Function Method2() As Type … End Function End Class Define and use an object Main Declare Obj1 As ClassOne … Call Obj1.Method1(params) Call Obj1.Method2 … End Program Pseudocode Language (Ch 8) In this chapter we added some more syntax to our language. We discussed how create classes and objects.

More Related