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

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Lecture 13: Object- Oriented Concepts Anita S. Malik Adapted from Schach (2004) Chapter 7.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
C++ fundamentals.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Object Oriented Programming using VC++. Introduction Program – Set of instruction written in a high level language High level language used for writing.
Comparison of OO Programming Languages © Jason Voegele, 2003.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object-oriented programming and software development Lecture 1.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Design and Programming Alan Goude Room: Sheaf 9323.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Chapter 12 Support for Object oriented Programming.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Learners Support Publications Object Oriented Programming.
Abstraction ADTs, Information Hiding and Encapsulation.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
Classes, Interfaces and Packages
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
ISBN Chapter 12 Support for Object-Oriented Programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
Object-Oriented Programming Concepts
OOP What is problem? Solution? OOP
Polymorphism.
11.1 The Concept of Abstraction
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object Oriented Analysis and Design
Inheritance Basics Programming with Inheritance
More Object-Oriented Programming
Computer Programming with JAVA
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Chapter 11: Inheritance and Composition
Object-Oriented PHP (1)
11.1 The Concept of Abstraction
Presentation transcript:

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

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

Introduction

Software Requirements Robust Availability Maintainable Re-usable Concurrent 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 programming C++, Java –Limited functionality Eiffel, ADA(~), Objective C, CLOS…

Object Oriented Programming Concepts

Object Oriented programming Object Class Inheritance Polymorphism

Object An object has –Properties (Variables / Data ) r P 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 = 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() ;

Inheritance Simple Inheritance r r r’ Q R,G,B + Q Multiple Inheritance Dynamic 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 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 } ;

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 r r’ 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 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

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

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

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 –Minimize duplication of code

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) ;

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

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 ;... } ;

Model & Lattice classes

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