1 / 44

Object Oriented Programming

Learn about component-based design, OOP concepts, performance issues, and LSMearth example software. Explore robust, re-usable, and concurrent development. Transition from component-based to object-oriented. Understand classes, inheritance, polymorphism, and encapsulation.

Download Presentation

Object Oriented Programming

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. 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. Q move_to(Q) Object • An object has • Properties (Variables / Data ) • Behaviour (Methods / Operators / Functions…) r P

  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; } ; Particle(Vector3D &P,real r) ; ~Particle() ; Particle & move_to(Vector3D &Q) ;

  16. Instances of Class Vector3D A(0,0,0), B(1,2,2); Particle P1(A,1.0),*P2 ; P2 = newParticle(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() ;

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

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

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

  23. Inheritance (example) 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 ; class A { int i; } ; class B { int j; } ; class C : A, B { int k ; }; class D { class A *a ; int l ; };

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

  25. Encapsulation (example) //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 } ; 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(); } ;

  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. r r Volume() Volume() r’ 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)

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

  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) 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 class A { public: virtual int function(); } ; class B : public A { public: virtual int function(); } ;

  31. Polymorphism (C++ limitation) 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) AnObject Volume() • w,h • Volume(s) h w

  33. Lattice AnObject AnObject Volume() r 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.

  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

  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<float> • Matrix<double> • Minimize duplication of code

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

  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<AnObject> 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

More Related