1 / 53

Week 2: Catalysts, Queues , Sorting, Component Coupling, and Inheritance

Week 2: Catalysts, Queues , Sorting, Component Coupling, and Inheritance. Jimmy Voss Disclaimer: Not all material is original. Some is taken from the official course slides, and some is taken from Anna’s slides. Announcements. Lab 1: Due: Tues Jan 17

Télécharger la présentation

Week 2: Catalysts, Queues , Sorting, Component Coupling, and Inheritance

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. Week 2: Catalysts, Queues, Sorting, Component Coupling, and Inheritance • Jimmy Voss • Disclaimer: Not all material is original. Some is taken from the official course slides, and some is taken from Anna’s slides.

  2. Announcements • Lab 1: • Due: Tues Jan 17 • http://www.cse.ohio-state.edu/sce/now/222/labs/lab01/Lab01/Hello_World.html

  3. Scope • A variable’s scope refers to the portion of the program body where it is active. • Variables come into scope at the location of their declaration. • Local variables leave scope upon the end of its code block via “}”

  4. Catalyst objects • An object which is in its default state both when it comes into scope and when it leaves scope is said to act as a catalyst. • We use the key word catalyst to denote such variables.

  5. Catalyst Objects Example global_procedureNaiveSwap( altersSequence_Of_Integer & x, altersSequence_Of_Integer & y) /*! requires: |x| = |y| ensures: x = #y y = #x */ { Integer I = 0; while ( I < x.Length() ) { object catalyst Integer temp; temp = x[I]; y[I] = x[I]; x[I] = temp; Temp.Clear(); I = I + 1; } }

  6. Queues • Templated container object. • Traditionally, queues act like a grocery store line. • FIFO – First in first out. • RESOLVE/C++ allows access of “current” item at the front of the Queue. • Mathematical model: Sequence of Item

  7. Queues • Operations: • Enqueue( x ) // consumes x • Dequeue( x ) // produces x • Accessor, i.e., [current] • requires |self| > 0 • Length()

  8. Code Using Queue_Of_Integer global_procedureFind_Min( preservesQueue_Of_Integer & q, produces Integer & min ) /*! . . . !*/ { object Integer I = 0; min = q[current]; while ( I < q.Length() ) { object catalyst Integer temp; q.Dequeue( temp ); if ( temp < min ) { min = temp; } q.Enqueue( temp ); I = I + 1; } }

  9. Records • Recall: A class is an object which contains both functions and variables. The variables are typically encapsulated. • Records are objects which contain variables but not functions. The variables are NOT encapsulated. • Example: Suppose a University wants to maintain a record for each student. What would we associate with a student?

  10. Records in RESOLVE/C++ • Records are a templated objects which can hold up to 10 fields. • Fields are accessed via the accessor operator []. • Field names are set using the function: field_name.

  11. Instantiating a Record concrete_instance class Name_And_Grade : instantiates Record < Text, Integer > {}; field_name (Name_And_Grade, 0, Text, name); field_name (Name_And_Grade, 1, Integer, grade);

  12. self Keyword • Used in the code for member functions of classes. • In addition to the variables passed in via the argument list, the variable “self” is always passed in. • self represents the object which called the member function.

  13. Example using self (not following RESOLVE/C++ conventions) class foo { public: Initialize() { value = 0; } procedure_body Increment() { self.value = self.value + 1; } procedure_bodyIncrementTwice() { self.Increment(); self.Increment(); } private: object Integer value; };

  14. Public Class Inheritance • Recall: inheritance occurs in the form: • class child : instantiates parent • The child class “inherits” all public functions of the parent class. • Functions from the parent class can be overwritten in the child class if one desires. • Encapsulation: parent’s private variables cannot be accessed by the child class. • RESOLVE/C++ inheritance keywords: • instantiates, specializes, extends, encapsulates, etc.

  15. Public Class Inheritance Example class Animal { public: ... void Make_Sound( alters Character_Ostream & out) { out << “No Sound Available\n”; } void Set_Weight( preserves Real & wt ) { self.weight = wt; } private: Real weight; };

  16. Public Class Inheritance Example class Dog : public Animal { public: ... void Make_Sound( Character_Ostream & out) { out << “Arf\n”; out << self.weight << “\n”; } }; Violates encapsulation

  17. Public Class Inheritance Example • Suppose that we have a Dog object Dog1. Are the following lines of code legal, and what do they do? • Dog1.Make_Sound( out ); • Dog1.Set_Weight( 100.5 );

  18. Relations between classes • A class is Abstract if its functions have no implementation (that is, only headers). • Abstract base classes are used for all RESOLVE/C++ types. These include formal comments for specifications. • Design-by-Contract – Each function has a formal comment which states what input to the function is valid, and how the function will effect input variables. This DOES NOT give any insight about the implementation.

  19. Relations Between Classes • Types of Class Dependencies: • Abstract to Abstract – When an abstract class inherits from an abstract base class • Concrete to Abstract – When a concrete child class gives an implementation of an abstract base class. • Concrete to Concrete – When a concrete child class inherits from a concrete base class.

  20. Class Inheritance Relations • Checks Relation – Child class checks that the ensures clauses of a parent class are met. (Used in debug code) • Extends Relation – child class adds additional functionality (i.e. via additional functions) to a parent class. • Instantiation – Child class fills in the base class template parameters. • Implementation – Child class adds implementation code to an abstract base class’s formally specified functions/procedures.

  21. Class Inheritance Relations • Partial Instantiation (specializes relation) – The child class fills in some of the parent class templates, but the child class still has unfilled template parameters. • Uses relation – Concrete to concrete relation. Child uses the implementation of the base class. • Component Coupling Diagrams (CCDs) are used to show the relations between classes.

  22. Template Variable Usage • Template variables act as an arbitrary type name, filled in by the compiler at compile time. • Can be: • A variable type in a function. • An arbitrary base class type in inheritance. • A variable type used within a class (i.e. Item in container classes). • Instantiation via angle brackets < >.

  23. Checks Relation Example concrete_template < concrete_instanceclassItem, concrete_instanceclassSequence_Base > classSequence_Kernel_C : checksconcrete_instance Sequence_Base { public: procedure_bodyAdd ( preserves Integer pos, consumes Item& x ) { assert (0 <= pos, "0 <= pos"); assert (pos <= self.Length (), "pos <= |self|"); self.Sequence_Base::Add (pos, x); } procedure_bodyRemove ( /*...*/ ) { /*...*/ } function_body Item& operator [] ( preserves Integer pos ) { /*...*/ } };

  24. Note: Sequence_Kernal is abstract. Sequence_Kernel_C is concrete. Black box represents templated base class. (templated to remove concrete-to-concrete dependence).

  25. Checks Relation Example concrete_template < concrete_instanceclassItem, concrete_instanceclassSequence_Base > classSequence_Kernel_C : checksconcrete_instance Sequence_Base { public: procedure_bodyAdd ( preserves Integer pos, consumes Item& x ) { assert (0 <= pos, "0 <= pos"); assert (pos <= self.Length (), "pos <= |self|"); self.Sequence_Base::Add (pos, x); } procedure_bodyRemove ( /*...*/ ) { /*...*/ } function_body Item& operator [] ( preserves Integer pos ) { /*...*/ } }; Checks a template class base.

  26. Extends Relation • Child class adds additional functionality to parent class. • Encapsulation – child class can only use public (or protected) functions and variables from parent class. The variables within the parent classes Representation are said to be encapsulated and cannot be directly modified by member functions defined in the child class. • Can be Abstract-to-Abstract or Concrete-to-Concrete.

  27. Example Sequence_Reverse abstract_template < concrete_instanceclass Item > classSequence_Reverse : extends abstract_instanceSequence_Kernel <Item> { public: procedure Reverse () is_abstract; /*! ensures self = reverse (#self) !*/ };

  28. This removes a concrete-to-concrete dependency. • The class which instantiates Sequence_Kernel’s member functions can be chosen by the client.

  29. Software Engineering View • Goals: • Reusable code • Flexibility of Implementation used • Modular design • Separation of Code into logically related chunks. • Takes advantage of the Object Oriented programming framework.

  30. Utility Classes • Utility Class – a class which provides a set of operations. • Reasons for use: • Package together related operations. • Operations need to be defined at the same time as other template parameters. • Modular code.

  31. Utility Class Notation • Function calls are made in the form: • Utility_Class_Name::Function( <params> ) • Examples: • objectInteger hash = • Text_Hash::Hash( name ); • objectBoolean flag = • In_Order_Tester::In_Order( a, b );

  32. Utility Class Example concrete_template < concrete_instance class Item, concrete_instanceutility_classItem_Are_In_Order, /*! . . . !*/ concrete_instance class Queue_Base /*! . . . !*/ > class Queue_Sort_1 : implements abstract_instanceQueue_Sort <Item>, extends concrete_instanceQueue_Base { . . . };

  33. Utility Class Example • Item_Are_In_Order::General_Are_In_Order(a, b) – form of the function call as it would be made within the class Queue_Sort_1 • Item_Are_In_Order is a template class, and hence is not directly linked to Queue_Sort_1. • Version of Item_Are_In_Order used must provide General_Are_In_Order which works on objects of type Item.

  34. Summary (Class relationships) • Checks – Base class checks parent’s requires. • Implements – Base provides implementation of parent’s specs. • Extends – Base class adds functionality to parent class. • Uses – Concrete to concrete dependency – child uses parents implementation. • Partial Instantiation / Specializes – Child class gives a partial instantiation of parent class.

  35. Sorting Algorithms • Sorting is one of the “traditional,” heavily studied problems in Computer Science. • Goals: • Efficiency • Correctness • Comparison based sorting algorithm – one which either directly or indirectly compares elements against each other to determine an ordering • Simple comparison-based sorting Algorithms: • Insertion Sort, Selection Sort, and Bubble Sort

  36. Insertion Sort • Idea: Have a sorted and unsorted portion of a sequence. Each step takes 1 item from the unsorted portion and inserts it into the sorted portion.

More Related