Download presentation
Presentation is loading. Please wait.
Published byDiana Beatrix Bell Modified over 9 years ago
1
Inheritance
2
class RECTANGLE inherit POLYGON RECTANGLE includes features of POLYGON r: RECTANGLE; p: POLYGON Polymorphism p := r -- legal assignment Dynamic binding p := r p.perimeter -- perimeter of RECTANGLE is called class TA inherit TEACHER STUDENT Inheritance: Multiple Inheritance:
3
Special syntax (1) class RECTANGLE inherit POLYGON redefine perimeter end feature perimeter : REAL is … end … end Redefinition allows the feature clause of RECTANGLE to contain a new version of method perimeter Redefine:
4
Special syntax (2) class TA inherit TEACHER rename id as teacher_id end STUDENT rename id as student_id end feature … end Several or none features can be renamed for each parent Rename:
5
Example class TA inherit TEACHER -- first inherited class rename -- renaming features of TEACHER id as teacher_id, mark as get_mark redefine – redefining methods of TEACHER get_mark, -- need to use last defined name add_factor end STUDENT – second inherited class rename -- renaming of features of STUDENT id as student_id, mark as set_mark end …
6
Abstract class deferred class DIGIT feature value : INTEGER -- current value lowest: INTEGER is -- lowest legal value deferred end A deferred class is a class that has at least one deferred feature There is no need in redefinition for features inherited in deferred form
7
The role of deferred class Capturing high-level classes, with common behaviors. Defining the components of an architecture during system design, without commitment to a final implementation. Describing domain-specific concepts in analysis and modeling.
8
Example deferred class STACK [G] feature -- Access count : INTEGER is-- Number of inserted elements deferred end item: G is-- Last pushed element require not_empty: not empty deferred end;
9
Example (cont) feature -- Status report empty : BOOLEAN is-- Is stack empty do Result := (count = 0) end full : BOOLEAN is-- Is stack full deferred end feature – Element change put(x : G ) is-- Push x on top require not_full: not full deferred ensure not_empty: not empty pushed_in_top: item = x one_more: count = old count + 1 end
10
Assertion Redeclaration rule In the redeclared version of a routine, it is not permitted to use a require or ensure clause. Instead you may: Introduce a new condition with require else, for boolean or with the original precondition. Introduce a new condition with ensure then, for boolean and with the original postcondition. In the absence of such a clause, the original assertions are retained.
11
Example (1) deferred class A … foo (x : INTEGER ) is require r1 deferred … end end; class B inherit A … foo (x : INTEGER ) is require r2 do … end end; The actual requirement is
12
Example (2) deferred class A … foo (x : INTEGER ) is deferred … ensure e1 end end; class B inherit A … foo (x : INTEGER ) is do… ensure e2 end end; The actual promise is
13
Invariants Redeclaration rule The invariant property of class is the boolean and of the assertions appearing in its invariant clause and of the invariant properties of its parents if any.
14
Example class A … invariant i1 end; class B inherit A … invariant i2 end; The actual invariant is
15
Example (1) deferred class COURSE feature -- mark treatment average : INTEGER-- average of marks num_marks : INTEGER-- marks number add_mark(mark : INTEGER) is require legal_new_mark: mark >= 0 and mark <= 100 deferred ensure num_marks = old num_marks + 1 end
16
Example (2) calc_factor (new_avg : INTEGER) : INTEGER is require legal_new_avg: new_avg >= 0 and new_avg <= 100 deferred ensure legal_calc_factor: average + Result = new_avg end invariant legal_avarage: average >= 0 and average <= 100 legar_num_marks: num_marks >= 0 end;
17
Example (3) class LOGIC inherit COURSE creation feature add_mark(mark : INTEGER) is require legal_new_mark: mark >= 0 and mark <= 55 do if mark = 0 then average := mark else average := (average*num_marks + mark)//(num_marks + 1) end num_marks := num_marks + 1 end
18
Example (4) calc_factor (new_avg : INTEGER) : INTEGER is require legal_new_avg: new_avg >= 0 and new_avg <= 55 do if average > 50 then Result := new_avg - average else Result := 0 end ensure legal_factor: Result <= 0 end invariant legal_avarage: average >= 0 and average <= 55 end;
19
Example (5) class MAIN logic: LOGIC -- logic.add_mark(56) -- for now average >= 55 so LOGIC invariant violation logic.add_mark(46) logic.add_mark(56) io.put_line("Average is " + logic.average.out) -- average = 51 < 55 -- io.put_line("factor for average 55 is " + logic.calc_factor(55).out) -- factor = 4 so LOGIC ensure legal_factor violation io.put_line("factor for average 50 is " + logic.calc_factor(50).out) -- factor -1 logic.add_mark(40) -- average 47 -- io.put_line("factor for average 50 is " + logic.calc_factor(50).out) -- factor = 0 so COURSE ensure legal_calc_factor violation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.