1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
VBA Modules, Functions, Variables, and Constants
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Lecture 9 Concepts of Programming Languages
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Object Oriented Software Development
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Programming Languages and Paradigms Object-Oriented Programming.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
UML for OOADStefan Kluth 1 2UML for OOAD 2.1What is UML? 2.2Classes in UML 2.3Relations in UML 2.4Static and Dynamic Design with UML.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
More C++ Features True object initialisation
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
ISBN Chapter 12 Support for Object-Oriented Programming.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
OOP What is problem? Solution? OOP
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Object Oriented Programming in Java
Dynamically Allocated Memory
CS212: Object Oriented Analysis and Design
Overview of C++ Polymorphism
Lecture 10 Concepts of Programming Languages
Presentation transcript:

1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism 1.9Summary

1 Objects and ClassesStefan Kluth 1.6What is an Object? Identity (which one is it): Address or instance ID State (what happened before): Internal variables Functions (Methods) Data An object has: interface behaviour identity state Interface (how to use it): Method signatures Behaviour (what it does): Algorithms in methods

1 Objects and ClassesStefan Kluth 1.6Object Interface Create an object (constructors) from nothing (default) from another object (copy) from 3 coordinates A dot product A cross product And possibly many other member functions The object interface is given by its member functions described by the objects class Magnitude how to use it

1 Objects and ClassesStefan Kluth 1.6Object Identity... ThreeVector a; ThreeVector b(1.0,2.0,3.0);... ThreeVector c(a); ThreeVector d= a+b;... ThreeVector* e= new ThreeVector(); ThreeVector* f= &a; ThreeVector& g= a;... double md= d.mag(); double mf= f->mag(); double mg= g.mag();... There can be many objects (instances) of a given class: Symbolically: a ❐ b ❐ c ❐ d ❐ e but f = g = a Pointer (*): Address of memory where object is stored; can be changed to point to another object Reference (&): Different name for identical object which one is it

1 Objects and ClassesStefan Kluth 1.6Object State The internal state of an object is given by its data members Different objects of the same class have different identity different state possibly different behaviour but always the same interface Objects of the same class can share data (explicitly declared class variables) e.g. static data members in C++ what happened before

1 Objects and ClassesStefan Kluth 1.6Object Behaviour class ThreeVector { public: ThreeVector() { x=0; y=0; z=0 };... double dot( const ThreeVector & ) const; ThreeVector cross( const ThreeVector & ) const; double mag() const;... private: double x,y,z; } Default constructor sets to 0 Dot and cross are unambigious Magnitude, user probably expects 0 or a positive number const means state of object does not change (vector remains the same) when this function is used what it does

1 Objects and ClassesStefan Kluth 1.6Object Interactions Objects interact through their interfaces only Objects manipulate their own data but get access to other objects data through interfaces Most basic: get() / set(... ) member functions, but usually better to provide "value added services", e.g. - fetch data from storage - perform an algorithm

1 Objects and ClassesStefan Kluth 1.6Message Passing Objects pass messages to each other Objects send messages to other objects and react on messages a says to b "getY" and receives a value for Y in return

1 Objects and ClassesStefan Kluth 1.6Objects keep data hidden Stop others from depending on the class data model Provide algorithms which use the data instead Can give direct and efficient access to data in controlled way pass (const) references or pointers Can change member class data model without affecting other objects Can replace member data e.g. by database lookup

1 Objects and ClassesStefan Kluth 1.6Private Object Data ● Object state a priori unknown ● The object knows and reacts accordingly ● Decisions (program flow control) encapsulated ● User code not dependent on algorithm internals, only object behaviour ● Object state can be queried (when the object allows it)

1 Objects and ClassesStefan Kluth 1.6Object Construction/Destruction Construction: Create object at run-time Initialise variables Allocate resources Constructor member functions Destruction: Destroy object at run-time Deallocate (free) resources Destructor member function

1 Objects and ClassesStefan Kluth 1.6Object Lifetime A* myA= new A();... delete myA; { A myA();... } Allocation creates new instance "on the heap" constructor called Must free resources by hand destructor called Declaration creates new instance "on the stack" constructor called Object will be deleted automatically when scope is left destructor called Or the language provides garbage collection (Java, Perl, Python)

1 Objects and ClassesStefan Kluth 1.6Objects Summary ● Object has interface, behaviour, identity, state ● Objects collaborate – send messages to each other – use each other to obtain results – provide data and "value-added services" ● Objects control access to their data – data private, state hidden – access through interface ● Objects have lifetime

1 Objects and ClassesStefan Kluth 1.7Objects and Classes ● Objects are described by classes – blueprint for construction of objects – OO program code resides in classes ● Objects have type specified by their class ● Classes can inherit from each other – implies special relation between corresponding objects ● Object interfaces can be separated from object behaviour and state

1 Objects and ClassesStefan Kluth 1.7Classes describe Objects ● Class code completely specifies an object – interface (member function signature) – behaviour (member function code) – inheritance and friendship relations ● Object creation and state changes happen at run- time ● In OO programs most code resides in the class member functions (methods) – objects collaborate to perform a task

1 Objects and ClassesStefan Kluth 1.7Classes = Types ● Class is a new programmer-defined data type ● Objects have type – extension of bool, int, float, etc – e.g. type complex didn't exist in C/C++, but can construct in C++ data type complex using a class ● ThreeVector is a new data type – combines 3 floats/doubles with interface and behaviour – can define operators +, ❐, *, / etc.

1 Objects and ClassesStefan Kluth 1.7Class Inheritance ● Objects are described by classes, i.e. code ● Classes can build upon other classes – reuse (include) an already existing class to define a new class – the new class can add new member functions and member data – the new class can replace (overload) inherited member functions – interface of new class must be compatible – new class has own type and type(s) of parent(s)

1 Objects and ClassesStefan Kluth 1.7Classes Summary ● Classes are blueprints for construction of objects ● Class = programmer defined data type of corresponding objects ● Classes can inherit (build upon) other classes

1 Objects and ClassesStefan Kluth 1.8Separation of Interfaces ● Interface described by class A with no (or little) behaviour – member function signatures – perhaps not possible to create objects of type A ● Now different (sub-) classes (B, C, D) can inherit from A and provide different behaviour – can create objects of type B, C or D with identical interfaces but different behaviour – code written using class A can use objects of type B, C or D

1 Objects and ClassesStefan Kluth 1.8Dynamic Object Polymorphism Objects of type A are actually of type B, C or D Objects of type A can take many forms, they are polymorph Code written in terms of A will not notice the difference but will produce different results Can separate generic algorithms from specialisations Avoids explicit decisions in algorithms (if/then/else or case)

1 Objects and ClassesStefan Kluth 1.8Dynamic Object Polymorphism int main () { vector va; va.push_back( new B() ); va.push_back( new C() ); va.push_back( new D() ); for( int i=0; i<va.size(); i++ ){ va[i]->doSomething(); }

1 Objects and ClassesStefan Kluth 1.8Interface Abstraction ● The common interface of a group of objects is an abstraction (abstract class, interface class) – find commonality between related objects – express commonality formally using interfaces ● Clients (other objects) depend on the abstract interface, not details of individual objects – Polymorphic objects can be substituted ● Need abstract arguments and return values – or clients depend on details again

1 Objects and ClassesStefan Kluth 1.8Mechanics of Dynamic Polymorphism Virtual function table with function pointers in strongly typed languages, e.g. C++, Java ABC 1doSomething0x3BA50x8BF1 2display0x0BF30x2CD5 3cleanup0x64370x7883 B::doSomethingC::doSomething B:: display C::display B::cleanupC::cleanup Lookup by name in hash-tables in weak+dynamically typed languages (Perl, Python, Smalltalk) Fast and efficient!

1 Objects and ClassesStefan Kluth 1.8Static Polymorphism (Templates) Template class U { public: void execute() { T t; t.init(); t.run(); t.finsish(); } #include “B.hh” #include “U.hh” int main { U ub; ub.execute(); } Class B { public: void init(); void run(); void finish(); } Template class U contains generic algorithm Class B implements No direct dependence between U and B, but interface must match for U to compile Can't change types at run-time Using typed collections difficult → don't use static polymorphism unless proven need

1 Objects and ClassesStefan Kluth 1.8Separated Interfaces Summary ● Interface can be separated from object – Abstract (interface) classes ● Express commonality between related objects – Abstraction is the key ● Clients depend on abstractions (interfaces), not on specific object details ● Dynamic polymorphism: simple, fast and efficient – Sometimes static polymorphism (templates) is used ● Polymorphic objects replace code branches

1 Objects and ClassesStefan Kluth 1.8Inheritance SA/SD vs OO SA/SD (procedural): Inherit for functionality We need some function, it exists in class A inherit from A in B and add some more functionality OO: Inherit for interface There are some common properties between several objects define a common interface and make the objects inherit from this interface

1 Objects and ClassesStefan Kluth 1.8Tools for OOAD ● A (graphical) modelling language – allows to describe systems in terms of classes, objects and their interactions before coding ● A programming language – classes (data+functions) and data hiding – class inheritance and object polymorphism ● Not required for OOAD (but useful) – templates, lots of convenient operators

1 Objects and ClassesStefan Kluth 1.9Summary ● Software can be a complex system – object and class views – hierarchy and abstractions ● Object model – abstract, encapsulation, modularity, hierarchy, type – objects have interface, behaviour, state, identity ● Class inheritance and object polymorphism – build hierarchies of abstractions