Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming D. Place QUAKES, The University of Queensland.

Similar presentations


Presentation on theme: "Object Oriented Programming D. Place QUAKES, The University of Queensland."— Presentation transcript:

1 Object Oriented Programming D. Place QUAKES, The University of Queensland

2 Content Introduction: component based design Object Oriented Programming Concepts Performance issues An example: LSMearth

3 Introduction

4 Software Requirements Robust Availability Maintainable Re-usable Concurrent development

5 Component based architecture Minimize component dependency –Reduce complexity –Reusability –Concurrent development “Simple” components –Robust Hierarchical architecture –Easy to maintain

6 Basic design From general to specific Base components –Simple –Robust –Re-usable

7 LSM modules Build on independent modules –Separate hardware dependence modules –Hidden complexity Model development does not require knowledge of MPI…

8 From Component Based to Object Oriented Many languages (Fortran…) allow component based programming OOP provides the same functionality but at a deeper level –Provides a hierarchy –No separation between data & algorithms: provide the data with the functions to manipulate the data

9 Programming Language Simula (1967) Smalltalk –“Pure” object oriented programming C++, Java –Limited functionality Eiffel, ADA(~), Objective C, CLOS…

10 Object Oriented Programming Concepts

11 Object Oriented programming Object Class Inheritance Polymorphism

12 Object An object has –Properties (Variables / Data ) r P move_to(Q) Q –Behaviour (Methods / Operators / Functions…)

13 Object & Class An object is an instance of a class –For example: Real a -> a is an object, Real is a class Sphere s

14 Class Specification of structure (instance variables), behaviour (methods), and inheritance (hierarchy). –Type of object –Classify objects by their properties and/or behaviour

15 Classes (example) Class Particle { Vector3D Position; double radius; } ; Class Particle { Vector3D Position; double radius; } ; Particle & move_to(Vector3D &Q) ; Particle(Vector3D &P,real r) ; ~Particle() ;

16 Instances of Class Vector3D A(0,0,0), B(1,2,2); Particle P1(A,1.0),*P2 ; P2 = new Particle(B,2.0) ; P1.move_to(B) ; P1.radius = 2.0 ; P2->radius = 3.0 ; delete P2 ; Vector3D A(0,0,0), B(1,2,2); Particle P1(A,1.0),*P2 ; P2 = new Particle(B,2.0) ; P1.move_to(B) ; P1.radius = 2.0 ; P2->radius = 3.0 ; delete P2 ;

17 Is a Class an Object ? 1 level system (single hierarchy) –Classes are objects and objects are classes 2 level system (classical view) –Class & Object distinction (class are not object) 3 level system –Classes are instance of meta-class –Meta-classes are instance of themselves 5 level system (object, class, class class, meta-class, meta-class class)

18 Is a Class an Object? In C++, classes are not objects –They are not instance of class However classes may contain –Properties (Variables/data) –Methods –“static”

19 Class (example) class A { public: Static int i,j,k; int l,b ; static void classfn() ; void function() ; } ; A a,b ; A::classfn() ; a.i = 2 ; // same as A::i = 2 ; or b.i = 2 b.l = 3 ; b.function() ; class A { public: Static int i,j,k; int l,b ; static void classfn() ; void function() ; } ; A a,b ; A::classfn() ; a.i = 2 ; // same as A::i = 2 ; or b.i = 2 b.l = 3 ; b.function() ;

20 Inheritance Simple Inheritance r r r’ Q R,G,B + Q Multiple Inheritance Dynamic Inheritance

21 Inheritance (example) class A ; class B ; class C : A {... } ; // class C { // class A a; //... } ; class D : A, B {... } ; class A ; class B ; class C : A {... } ; // class C { // class A a; //... } ; class D : A, B {... } ;

22 Dynamic Inheritance C++ Limitation –Dynamic Inheritance is done through dynamic binding. class A ; class B ; class C : A, B { … }; class D { class A *a ; … }; class A ; class B ; class C : A, B { … }; class D { class A *a ; … };

23 Inheritance (example) class A { int i; } ; class B { int j; } ; class C : A, B { int k ; }; class D { class A *a ; int l ; }; class A { int i; } ; class B { int j; } ; class C : A, B { int k ; }; class D { class A *a ; int l ; }; C c1; D d1; c1.i = 2 ; c1.j = 2 ; c1.k = 2 ; d1.l = 2 ; d1.a = &c1 ; d1.a->i = 2 ; ((C *)(d1.a))->j = 2 ; C c1; D d1; c1.i = 2 ; c1.j = 2 ; c1.k = 2 ; d1.l = 2 ; d1.a = &c1 ; d1.a->i = 2 ; ((C *)(d1.a))->j = 2 ;

24 Object Encapsulation Protect or hide objects –Hide objects that do not contribute to the essential characteristics Public Protected Private

25 Encapsulation (example) class A { private: int i; protected: int j ; public: int k ; } ;// all members can be accessed inside A class B : public A { public: int function(); } ; class A { private: int i; protected: int j ; public: int k ; } ;// all members can be accessed inside A class B : public A { public: int function(); } ; //inside the class B… int B::function { k = 1 ; // public ok j = 1 ; // protected ok i = 1 ; // private denied } ; // outside the class… int main() { B b1; B.k = 1; // public ok B.j = 1; // protected denied } ; //inside the class B… int B::function { k = 1 ; // public ok j = 1 ; // protected ok i = 1 ; // private denied } ; // outside the class… int main() { B b1; B.k = 1; // public ok B.j = 1; // protected denied } ;

26 Polymorphism Having, assuming, or passing through many or various forms At run time, objects can change of class –Such as a window becoming an icon The same operation may behave differently for –Different classes, or –Different parameters

27 Overriding & Multi-methods Overriding (Polymorphism) –Behaviour/methods can be overloaded (ie. re-defined) Multi-methods (or multiple-polymorphism) –more than one parameter can be used in the selection of a method (usually only the type of the object is used to select a method) –For instance int operator + (int) int operator + (double) r r r’ Volume()

28 Overriding & Multi-Methods (example) class A { public: virtual int function(); int function(int i); } ; class B : public A { public: virtual int function(); } ; class A { public: virtual int function(); int function(int i); } ; class B : public A { public: virtual int function(); } ; A a1; B b1; a1.function(); // call function() in A b1.function(); // call function() in B b1.function(2); // function(int) in A A a1; B b1; a1.function(); // call function() in A b1.function(); // call function() in B b1.function(2); // function(int) in A

29 Dynamic Typing Dynamic Typing is used to allow polymorphism The type of objects are determined at execution time (not at compilation time) Limitation in C++, Java (strong typing) Run Time Type Identification (RTTI) provide a limited dynamic typing in C++

30 Polymorphism (example) class A { public: virtual int function(); } ; class B : public A { public: virtual int function(); } ; class A { public: virtual int function(); } ; class B : public A { public: virtual int function(); } ; A a1; A *pa; B b1; a1.function(); // call function() in A b1.function(); // call function() in B pa = & a1 ; pa->function(); // call function in A... pa = & b1 ; pa->function(); // call function in B A a1; A *pa; B b1; a1.function(); // call function() in A b1.function(); // call function() in B pa = & a1 ; pa->function(); // call function in A... pa = & b1 ; pa->function(); // call function in B

31 Polymorphism (C++ limitation) class A { public: virtual int function(); } ; class B : public A { public: double function(); // not allowed } ; class A { public: virtual int function(); } ; class B : public A { public: double function(); // not allowed } ;

32 Abstract & Incomplete Class Used to specify an Interface. –Partially defined behaviour. –Specify how the objects/classes are used –To create an instance all behaviour must be defined. r r Volume(s) w,h Volume(s) w h Volume() AnObject

33 Abstract class Pure abstract (base) classes are only defined by their behaviour. –Allow the refinement of classes without altering the dependent classes –Instances of the derived class must comply to the specifications. r Volume() AnObject Lattice AnObject

34 Abstract & incomplete class (example) class AParticle { public: virtual int calcforce() = 0 ; } ; class ElasticParticle : public AParticle { public: virtual int calcforce() ; } ; Int ElasticParticle::calcforce() { … } ;... AParticle P // forbidden, AParticle is incomplete class AParticle { public: virtual int calcforce() = 0 ; } ; class ElasticParticle : public AParticle { public: virtual int calcforce() ; } ; Int ElasticParticle::calcforce() { … } ;... AParticle P // forbidden, AParticle is incomplete

35 Parameterised polymorphism Ability to parameterise classes and functions with classes or types –For example, use to specify the type of the elements of a matrix Matrix –Minimize duplication of code

36 Parameterised class (example) template class Lattice { protected: Collection allparticle ; public: T & get_object(int n) ; } ; Class Particle; Class Molecule; template class Lattice { protected: Collection allparticle ; public: T & get_object(int n) ; } ; Class Particle; Class Molecule; Lattice L; Lattice L2; particle p ; Molecule m ; p = L.get_object(100) ; M = L2.get_object(10) ; Lattice L; Lattice L2; particle p ; Molecule m ; p = L.get_object(100) ; M = L2.get_object(10) ;

37 Performance

38 Dynamic binding… Dynamic binding costs time & memory –Objects contain information about their classes. –Automatic optimisation (such as inlining) cannot be performed since the compiler cannot determinate which functions are called. –Using “virtual” only when needed…

39 Function calls Function calls cannot be optimised unless the compiler has access to the source code. –Inline functions (similar to macros) Uses a simplified function call mechanisms Not suited for automatic parallelisation –Use of directive

40 An example: LSMearth

41 Particle hierarchy

42 The particle class… Class DynObject : virtual public AnObject { protected: Vector Vel, Acc, Frc ;... } ; Class InteractingUnit { protected: list neighbors ; public: virtual int nbNeighbours() ; virtual AnObjet * getNeighbour() ;... } ; Class AParticle : virtual public DynObject, virtual public InteractingUnit { public: void calcforces() = 0 ; void time_integrate(AnIntegrationScheme &TI) = 0 ;... } ; Class DynObject : virtual public AnObject { protected: Vector Vel, Acc, Frc ;... } ; Class InteractingUnit { protected: list neighbors ; public: virtual int nbNeighbours() ; virtual AnObjet * getNeighbour() ;... } ; Class AParticle : virtual public DynObject, virtual public InteractingUnit { public: void calcforces() = 0 ; void time_integrate(AnIntegrationScheme &TI) = 0 ;... } ;

43 Model & Lattice classes

44 Concluding remarks Object Oriented Programming –Emphasis on modelling the real-world –Enable incremental, iterative, evolutionary and concurrent development


Download ppt "Object Oriented Programming D. Place QUAKES, The University of Queensland."

Similar presentations


Ads by Google