object oriented programming d. place quakes, the university of queensland
TRANSCRIPT
Object Oriented Programming
D. Place
QUAKES, The University of Queensland
Content
Introduction: component based des
ign
Object Oriented Programming Conc
epts
Performance issues
An example: LSMearth
Introduction
Software Requirements
RobustAvailability MaintainableRe-usableConcurrent development
Component based architecture
Minimize component dependency– Reduce complexity– Reusability– Concurrent development
“Simple” components– Robust
Hierarchical architecture– Easy to maintain
Basic design
From general to specific Base components
– Simple– Robust– Re-usable
LSM modules
Build on independent modules– Separate hardware dependence modules– Hidden complexity
• Model development does not require knowledge of MPI…
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
Programming Language
Simula (1967)Smalltalk
– “Pure” object oriented programmingC++, Java
– Limited functionalityEiffel, ADA(~), Objective C, CLOS…
Object Oriented Programming Concepts
Object Oriented programming
Object ClassInheritance Polymorphism
Object
An object has– Properties (Variables / Data )
rP
move_to(Q)
Q
– Behaviour (Methods / Operators / Functions…)
Object & Class
An object is an instance of a class– For example:
•Real a -> a is an object, Real is a class
•Sphere s
Class
Specification of structure (instance variables), behaviour (methods), and inheritance (hierarchy).– Type of object– Classify objects by their
properties and/or behaviour
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() ;
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 ;
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)
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”
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 = 2b.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 = 2b.l = 3 ;b.function() ;
Inheritance
Simple Inheritance
r r
r’
QR,G,B +
Q
Multiple InheritanceDynamic
Inheritance
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 {... } ;
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 ; …};
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 ;
Object Encapsulation
Protect or hide objects – Hide objects that do not contribute to
the essential characteristics• Public• Protected• Private
Encapsulation (example)
class A {private: int i;protected:
int j ;public: int k ;} ;// all members can be
accessed inside Aclass B : public A {public: int function();} ;
class A {private: int i;protected:
int j ;public: int k ;} ;// all members can be
accessed inside Aclass 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} ;
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
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 rr’
Volume()Volume()
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 Ab1.function(); // call function() in B
b1.function(2); // function(int) in A
A a1;B b1;
a1.function(); // call function() in Ab1.function(); // call function() in B
b1.function(2); // function(int) in A
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++
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 Ab1.function(); // call function() in Bpa = & 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 Ab1.function(); // call function() in Bpa = & a1 ;pa->function(); // call function in A...pa = & b1 ;pa->function(); // call function in B
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} ;
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
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.
rVolume()
AnObject
LatticeAnObject
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
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
Parameterised class (example)
template<class T> class Lattice{protected:
Collection<T> allparticle ;public: T & get_object(int n) ;} ;
Class Particle;Class Molecule;
template<class T> class Lattice{protected:
Collection<T> allparticle ;public: T & get_object(int n) ;} ;
Class Particle;Class Molecule;
Lattice<Particle> L;Lattice<Molecule> L2;particle p ;Molecule m ;
p = L.get_object(100) ;M = L2.get_object(10) ;
Lattice<Particle> L;Lattice<Molecule> L2;particle p ;Molecule m ;
p = L.get_object(100) ;M = L2.get_object(10) ;
Performance
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…
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
An example: LSMearth
Particle hierarchy
Interacting Unit Dynamic Objet
Grain
Frictionless Particle
Wall (driving plates) Particle Unit
Pos, Vel, Acc, FrcNeighborhood Table
Pos, Orientation
Spring Constant
Coefficient of Friction
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 ;... } ;
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 ;... } ;
Model & Lattice classes
M odel
A la tticeA tim e in teg ra tionA se t o f pa ram ete rsA d isp la y in te rfa ceA p a rs e r in te r fa ce (sc rip tin g )
M odel w ithout fric tion
Fm odel
A la ttice F la ttice
G ra inC om p lex o b jec t w ith fr ic tion
M odel w ith fric tion
Tem plate la ttice
Lattice
F ric tion le ss P a rtic le sC om p lex o b jec t (w a lls )
Concluding remarks
Object Oriented Programming– Emphasis on modelling the real-
world – Enable incremental, iterative,
evolutionary and concurrent development