90 likes | 208 Views
OBJECT ORIENTED FEATURES AND IMPLEMENTATION OF UNIVERSAL QUANTIFICATION IN C++. Karoly Bosa RISC SS2000. OVERVIEW: - Class definition - Object declaration - Inheritance - Virtual functions - Templates and Universal Quantification. 1. Class definition. C++: class Cell {
E N D
OBJECT ORIENTED FEATURES AND IMPLEMENTATION OF UNIVERSAL QUANTIFICATION IN C++ Karoly Bosa RISC SS2000
OVERVIEW: - Class definition - Object declaration - Inheritance - Virtual functions - Templates and Universal Quantification
1. Class definition C++: class Cell { public : int contents; Cell() { contents=0; } int get() { return contents; } void set(int n) { contents = n; } } In the untyped -calculus: Cell = [Cell = (z)[contents = (s)z.contents(s), get = (s)z.get(s), set = (s)z.set(s)], contents = (s) 0; get = (s) s.contents; set = (s) (n) s.contents:=n] When the constructor method is called, the system substitute class name Cell for self parameter Z.
2. Object declaration C++: Cell cellobject; The C++ call the constructor automatically. In the untyped -calculus: cellobject = Cell.Cell = [contents = (s) 0; get = (s) s.contents; set = (s) (n) s.contents:=n] When a method is called: cellobject.get() s.contents {{scellobject}} = cellobject.contents;
3. Inheritance C++: class reCell : public Cell { public : int backup; reCell() { backup = 0; } void set(int n) { backup = contents; Cell::set(n); } void restore() { contents = backup;} } • In the untyped -calculus: • reCell = • [reCell = (z)[contents = • (s)z.contents(s), get = • (s)z.get(s), set = • (s)z.set(s), backup = (s)z.backup(s), restore = (s)z.restore(s)], • contents = Cell.contents, • get = Cell.get, • set = (s) (n) Cell.set(s.backup := • s.contents)(n), • backup = (s) 0; • restore (s) s.contents := s.backup ]
4. Virtual functions (Self Application Semantics) I. C++ Example: class A{ public : void aa() { bb(); } virtual void bb() { cout << “Called: method bb of class A \n”; } } class B : public A { public: void bb() { cout << ”Called: method bb of class B \n”; } } ... Declaration of objects: A objectA; B objectB; Self Application Semantics: objectA [aa = (x1){x1.bb}, bb = (x2){cout << ...bb of A;}] When B extends A: A.bb() (y){cout <<...bb of B;} objectB [aa = (x1){x1.bb}, bb = (y){cout << ...bb of B;}] Question : if we call objectB.aa(), which method bb() will be done?
4. Virtual functions (Self Application Semantics) II. If we call objectA.aa() x1.bb() {{x1 objectA}} objectA.bb() then the output will be: Called: method bb of class A If we call objectB.aa() x1.bb() {{x1 objectB}} objectB.bb() then the output will be: Called: method bb of class B
5. Templates and Universal Quantification Simple Template: template<class T> class Vector { ...} Function Template: template<class T> void sort(Vector<T>& v) { unsigned int n = v.size(); for (int i=0; i<n-1; i++) for (int j=n-1; i<j; j--) if (v[j] < v[j-1]) { T temp = v[j]; v[j] = v[j-1]; v[j-1] = temp; } } Vector <int> vi; ... sort(vi); Vector<complex> vc; ... sort(vc); Universal Quantification: type Generic_sort = T.Generic_sortWRT[T] type Generic_sort[T] = {sort:(Vector[T] Vector[T]} value int_sort : Generic_sortWRT[int] = {sort=fun(v : Vector[int]) st(v);} In this case, the generic block contains only one function always.