Presentation is loading. Please wait.

Presentation is loading. Please wait.

Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.

Similar presentations


Presentation on theme: "Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined."— Presentation transcript:

1 Types(2)

2 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined in terms of problems that are closer to the simple case  By applying this redefinition process every time the recursive function is called, eventually the problem is reduced entirely to simple csaes, which are relatively easy to solve. if this is a simple case solve it else redefine the problem using recursion

3 3 Example int multiply (int m, int n) { int ans; if (n==1) ans = m; else ans = m + multiply ( m, n-1 ) return (ans); }

4 4 Tracing multiply Function m is 6 n is 3 3==1 false multiply (6,2) ans is 6 + Return (ans) m is 6 n is 2 2==1 false multiply (6,1) ans is 6 + Return (ans) m is 6 n is 1 1==1 is true ans is 6 Return (ans) multiply (6,3) 18 12 6

5 5 Recursive Data Type (1)  Recursive data type uses itself in its declaration  Recursive data types are important to recursive algorithms  Used to represent data whose size and structure is not known in advance  Examples: lists and trees

6 6 Recursive Data Type (2) Struct CharList { char data; Sturct CharList next; /* illegal C*/ };  Needs a unlimited storage space (illegal C)  In the analogy with recursive functions Int factorial (int n) { return n * factorial (n-1); }/* illegal C*/  No base case (test to stop the recursion)

7 7 Recursive Data Type (3)  The solution: using pointers to allow manual dynamic allocation Struct CharListNode { char data; Sturct CharListNode *next; /* legal C*/ };  Each individual element in a CharListNode has a fixed size  Elements can be linked together to form a list of arbitrary size

8 8 Type Equivalence  The assignment statement serves to replace the current value of a variable with a new value specified as an expression. The variable (or the function) and the expression must be of identical type.  Name Equivalence Two types are equivalent if they have the same name  Structure Equivalence Two types are equivalent if they have the same structure

9 9 Example:  Name equivalent c and d have the same type a and c haven’t the same type  Structure equivalent a, b, c, d have the same type d and e haven’t the same type  Field names are different struct complex { float re, im; }; struct polar { float x, y; }; struct y { float re, im; }; struct complex c, d; Struct y a, b; struct polar e; int f[5], g[10];

10 10 Subtypes  A subtype is a type that has certain constraints placed on its values or operations.

11 11 Ada Example subtype one_to_ten is Integer range 1.. 10; type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); subtype Weekend is Day range Saturday.. Sunday; type Salary is delta 0.01 digits 9 range 0.00.. 9_999_999.99; subtype Author_Salary is Salary digits 5 range 0.0.. 999.99;

12 12 Java example  An object s of class S can be assigned to object t of class T  t = s Either S and T are the same class Or S is a subclass of T Widening Conversion

13 13 Polymorphism and Generics (1)  Polymorphism Comes from Greek Means: having many forms  A function or operation is polymorphic if it can be applied to any one of several related types and achieve the same result  Operators overloading is a kind of polymorphism  An advantage of polymorphism is that it enables code reuse.

14 14 Polymorphism and Generics (2)  To enables code reuse Ada introduced the concept of generics or template  A generic function is a template that can be instantiated at a compile time with concrete types and operators Binding of arguments to a type is deferred from programming time to compile time.

15 15 Polymorphism and Generics (3)  C++ provides generics or templates  The implementation mechanism Actual parameters are textually substituted for generic parameters Resulting text is then compiled


Download ppt "Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined."

Similar presentations


Ads by Google