1.background on oop

31
Object-Oriented Programming 46-691 MSCF Carnegie Mellon University Background on O-O Programming

Upload: 4sainadh

Post on 14-Oct-2015

6 views

Category:

Documents


0 download

TRANSCRIPT

  • 5/24/2018 1.Background on OOP

    1/31

    Object-Oriented Programming46-691

    MSCF

    Carnegie Mellon UniversityBackground on O-O Programming

  • 5/24/2018 1.Background on OOP

    2/31

  • 5/24/2018 1.Background on OOP

    3/31

  • 5/24/2018 1.Background on OOP

    4/31

    Patch Panel Programming

  • 5/24/2018 1.Background on OOP

    5/31

    Programming Language

    Evolution Machine language

    Assembler

    3rd generation (COBOL, FORTRAN, C)

    Specialized (Lisp, Prolog, APL)

    4th generation (SQL, GAMS,spreadsheets, Mathematica)

    5th generation (example, anyone?)

  • 5/24/2018 1.Background on OOP

    6/31

    Why So Many Languages? Bring the language closer to the

    problem.

    But 4GLs are typically focused onspecialized domains (e.g., relationaldatabases).

    We want a language that is generalpurpose, yet can easily be tailored toany domain.

    An inspiration from an odd place

  • 5/24/2018 1.Background on OOP

    7/31

  • 5/24/2018 1.Background on OOP

    8/31

    Simula Designed to simulate job shops, banks, etc.

    To code, you create classes (like milling machines)and instances (machine #1, etc.).

    All milling machines have properties (how much timeto make Part #1).

    All milling machines have abilities (mill to depth n,move part to positionx).

    Job shop operations simulated by making themachines, and moving material through them.

  • 5/24/2018 1.Background on OOP

    9/31

    Whats Cool Here?Very close to the problem; domain

    experts can pitch in easily.

    Its possible to make new types ofmachines (e.g., drill press).

    Description of the shop floor is

    transparent:

    millingMachine#1. millPart (part#3, depth

    2mm)

    drillPress#2. idleUntil (Poisson(0.2))

  • 5/24/2018 1.Background on OOP

    10/31

    Programming = Simulation? For a stock market application, we

    might write:

    value = Stock1.determineValue()

    risk = Stock1.determineRisk()

    if (Stock1.f(value, risk) > 0.12) then

    Stock1.sell()

    So Simula inspired more general-purpose object-oriented languages.

  • 5/24/2018 1.Background on OOP

    11/31

    Object-Oriented Languages Smalltalk, C++, Java, etc

    You can make any kind of objects youwant

    How different from procedurallanguages?

    No different at all: Every (reasonable)language is Turing complete

    Very different: Make expression easier, less

    error-prone

  • 5/24/2018 1.Background on OOP

    12/31

    O-O Languages (Alan Kay) Everything is an object.

    A program is a bunch of objects tellingeach other what to do, by sendingmessages.

    Each object has its own memory, and is

    made up of other objects.

    Every object has a type (class).

    All objects of the same type can receivethe same messages.

  • 5/24/2018 1.Background on OOP

    13/31

    Making C++ Work It's easy as pie to write procedural

    code in C++.

    It takes some discipline (an attitude) tothink O-O.

    It's worthwhile to do it.

  • 5/24/2018 1.Background on OOP

    14/31

    ObjectsAn object has an interface (its powers

    and abilities), determined by its class.

    A class is an abstract data type, or user-defined type.

    Designing a class means defining its

    interface.

  • 5/24/2018 1.Background on OOP

    15/31

    Built-In Types Think of an int

    What is its interface?

    How do you send it messages? How do you make one?

    Where does it go when youre done with it?

    In solving a computational problem, the goalis to

    Dream up useful classes,

    Endow them with appropriate characteristics, and

    Make some and set them to work.

  • 5/24/2018 1.Background on OOP

    16/31

    Example

    Suppose Ive defined this class in C++:

    To make one, I typeBottleOfBeer myBeer; // compare to int i

    This allocates memory on the stack;myBeerrefers to its location.

    BottleOfBeer

    +open():void

    +lif t(height:int):voi

    +tilt(angle:int):void

  • 5/24/2018 1.Background on OOP

    17/31

    Example (cont.)

    If I want many beers, I might sayBottleOfBeer ninetyNineBottlesOfBeer[99];

    This creates an array of bottles, contiguouslylocated in memory on the stack

    ninetyNineBottlesOfBeeris a pointer to thebeginning of this memory

    If I want a beer opened, I saymyBeer.open();

    or

    ninetyNineBottlesOfBeer[3].open();

  • 5/24/2018 1.Background on OOP

    18/31

    Designers Design, Users Use

    The interface is the critical part, but thedetails (implementation) are important too.

    Users use the interface (the public part);the implementation is hidden by accesscontrol.

    BottleOfBe er

    -topOn:Boolean

    +open():void

    +lift(height:int):voi

    +tilt(angle:int):void

  • 5/24/2018 1.Background on OOP

    19/31

    Objects vs. ProceduralLibraries

    C libraries are like this, sort of:

    The library designer invents a useful struct.

    Then she provides some useful functions for it.

    The user creates an instance of the struct, thenapplies library functions to it.

    One big difference is that anyonecan change anypart of the struct. Booo, hsss!

    Another difference is in initialization (and well attendto this soon enough).

  • 5/24/2018 1.Background on OOP

    20/31

    Two Ways of Reusing Classes

    Composition: One class has another as a part(indicated by the diamond aggregation

    symbol).BottleOfBeer CaseOfBeer 1* Tavern1*

    Patron

    1*

  • 5/24/2018 1.Background on OOP

    21/31

    Two Ways of Reusing Classes

    Inheritance: One class is a specializedversion of another (indicated by the

    triangle inheritance symbol).BottleOfBee r

    +open():void+lift(height:int):void

    +tilt(angle:int):void

    BottleOfRollingRock

    -lowCalorie:Boolean

    +isLowCalorie() :Boolean

  • 5/24/2018 1.Background on OOP

    22/31

    Polymorphism

    Different subclasses respond to thesame message, possibly with different

    actions. Patron

    +beerPlease():Polite

    BritishPatroAmericanPatron

    +beerPlease():Rude

    GermanPatron

    +beerPlease():InGerman

  • 5/24/2018 1.Background on OOP

    23/31

    Polymorphism Using Pointers

    Patron * p1 = new Patron;

    Patron * p2 = new AmericanPatron;

    Patron * p3 = new BritishPatron;Patron * p4 = new GermanPatron;

    p1->BeerPlease() ; // polite request

    p2-> BeerPlease() ; // rude request

    p3->BeerPlease() ; // polite request

    p4->BeerPlease() ; // request in German (but polite)

    This is a bit of a trick: it requires late bindingof the function call.

  • 5/24/2018 1.Background on OOP

    24/31

    Creating Objects

    We usually assume this is free; withbuilt-in types like intor char, we just say

    int i;

    char c;

    With user-defined types (the ones we

    make), we need to be explicit aboutwhat we want: one or more constructorfunctions

    This is a very important issue!

  • 5/24/2018 1.Background on OOP

    25/31

    Destroying Objects

    If an object goes out of scope, it canno longer be used (its name is no

    longer known). In C++, we might need to write an

    explicit function to free memory

    allocated to an object. (Compare Java, which uses references

    and garbage collection.)

  • 5/24/2018 1.Background on OOP

    26/31

    Example of Object Scope

    What happens to lect (and s)?

    The LectureNotesobject is created andinitialized, but it disappears after return (its

    out of scope).

    The class designer needs to say what cleanupis needed (in something called a destructor).

    public string getTitle(int lectureNumber) {

    LectureNotes lect = syllabus.getLecture(lectureNumber);

    string s = lect.getLine(1);

    return s;

    }

  • 5/24/2018 1.Background on OOP

    27/31

    A Simple Model of Memory(An Ordinary C++ Variable)

    The variable lectis the

    memory where the

    LectureNotes object resides.

    22 C++ Versus Java\n

    The LectureNotesobject

    may in turn refer (through a

    pointer) to other memory

    When you speak of the variable lect, you are speaking of

    the actual LectureNotesobject. When lectgoes out of scope, it is

    automatically destroyed. If destruction is complicated, an

    explicit destructor must be written, or else we have a memory leak.

    memory

  • 5/24/2018 1.Background on OOP

    28/31

    Javas Model of Memory

    The variable lectrefers

    to a memory location

    0x1234 22 C++ Versus Java\n

    The LectureNotesobject

    may in turn refer (through a

    pointer) to other memory

    When you speak of the variable lect, you are speaking of

    the actual LectureNotesobject. When lectgoes out of scope, it is

    automatically destroyed, but what it refers to isnt.

    memory

  • 5/24/2018 1.Background on OOP

    29/31

    C++s Use of Memory

    Registers

    Stack

    Heap

    Static variables (both global andmembers)

    Constants

    Function code

  • 5/24/2018 1.Background on OOP

    30/31

    Creating New Types

    class MyNewType {

    // definition here

    }; Now its legal to say

    MyNewType m;

    orMyNewType * mPointer = new MyNewType;

  • 5/24/2018 1.Background on OOP

    31/31

    Class Members

    Fields (a.k.a. member variables, datamembers)

    Methods (a.k.a. member functions)class MyClass {

    int a;YourClass b;double memberFunction(int x, double d) {

    return 0;}

    }