lecture 15: object-oriented programming - computer scienceolivier/comp524/lecture15.pdfthe...

23
The University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP 524 Programming Language Concepts Stephen Olivier March 23, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Upload: others

Post on 01-Jan-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Lecture 15: Object-Oriented ProgrammingCOMP 524 Programming Language Concepts Stephen OlivierMarch 23, 2009

Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Page 2: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Object Oriented Programming Benefits

•Reduces conceptual load

•Fault containment

•Independence between components

• Makes interface design very important

2

Page 3: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Brief History

•Simula (60’s) : Objects, classes, etc.

•Smalltalk (70’s) : OO as we know it

•C++ (80’s) : OO meets C, first widespread OO language

•Java (90’s) : C/C++ syntax but OO-centric design

•and more...

3

Page 4: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Class-as-type

•A class can be viewed as a “class-as-type,” when it contains multiple variables and/or functions to manipulate these variables.

4

Page 5: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Public Variables

•Generally, people consider it bad form to put variables in the public space.

•Why?

5

Page 6: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Class-as-manager

•A class can be viewed as a “class-as-manager” when it is not directly used to contain variables but rather to “manage” other objects.

6

Page 7: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Public, Private, and Protected (in C++)

•Public fields are available to everyone

•Private fields are available only to the class

•Protected are available only to descendants.

7

Page 8: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Deriving Classes

•Classes can be derived from other class, and inherit their functions.

•This is a KEY component of object-oriented programming.

•Also opens possibility to have subtype polymorphism

8

Page 9: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Inheritance

9

class foo{ public int x; private int y; protected int z;}

class bar : foo { public int xy() {return x+y;} //invalid public int xz() {return x+z;} //valid}...bar bbb;bbb.x = 1; //validbbb.y = 1; //invalidbbb.z = 1; //invalid

Page 10: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Scoping

•In order to refer to a function for a particular class, use the scope resolution operator ::

10

class foo{public int bar();

}

int foo::bar(){ return 0;}

Page 11: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

General Purpose Base Class

•Its possible to define a general purpose base class as the highest level of abstraction

• for example Object in Java

11

Page 12: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Modifying Base Class Methods

•Its possible to redefine base methods in derived classes

12

class foo{public int bar();

}...class qud : foo{public int bar(){ return 1;};

}

Page 13: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Modifying Base Class Methods

•Sometimes better just to use base if possible and try to return errors

13

class foo{public int bar();

}...class qud : foo{public int bar(){ try {return foo::bar();}...};

}

Page 14: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Protection Rules (in C++)

•Any class can limit visibility of its member

•A derived class can restrict the visibility of a base class, but never increase it

•A derived class that limits visibility of members of a base class as protected or private can restore the visibility of individual members by inserting a “using” declaration in protected or public portion of the derived class.

14

Page 15: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Java vs C++

•Under Java derived classes cannot change the visibility from the base class

• But members can be redefined.

•Protected has a slightly different meaning.

• Under Java, protected means that it is visible by everything in the package and derived classes

• Without protected, it is visible by everything in the package, but not derived classes in other packages

15

Page 16: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Three Issues Regarding Constructers

•Choosing a constructor

•References and Values

•Execution Order

16

Page 17: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Choosing

•Two common ways:

• 1. Named constructors (Smalltalk, Eiffel)

• 2. Overloading (C++, Java, C#)

•Some languages (Ada95, Modula-3) don’t initialize objects at elaboration time

17

Page 18: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Value and References

•Variables can refer to objects (Smalltalk, Java, Python, Ruby)

•or have values which are objects (C++)

18

Page 19: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

References

•More elegant, but can cause additional overhead.

• Space required on heap

• Indirection to access heap

• Garbage collection

19

Page 20: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Objects as Values (C++)

•When variables are used as values in C++, a constructor can be implicitly called.

20

foo b; //Calls foo::foo()foo b(10, ‘x’); //calls foo:foo(int, char)

Page 21: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Costs of the Value Model

21

foo a = b+c;

foo t;t = b.operator+(c);foo a = t;

foo a = b; a += c; // avoids need for t

Page 22: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Execution order

•When an object employs inheritance it constructs the base classes first

22

foo::foo( foo_params ) : bar (bar_params) { ...}

foo ( foo_params ){super (bar_parms) ... }

Page 23: Lecture 15: Object-Oriented Programming - Computer Scienceolivier/comp524/Lecture15.pdfThe University of North Carolina at Chapel Hill Lecture 15: Object-Oriented Programming COMP

The University of North Carolina at Chapel Hill

Destructors

•Needed in OO languages with explicit heap management, e.g. C++

23

class foo { char * list; foo() { list = new char[100];} ~foo() { delete list };}