36
Welcome to OOP course! 203.1120

Welcome to OOP course!

  • Upload
    faraji

  • View
    45

  • Download
    1

Embed Size (px)

DESCRIPTION

203.1120. Welcome to OOP course!. Course details. Teaching team Li-Tal Mashiach and Moran Lefler Grading: 30% - Project in 3 parts 70% - Final exam Contact me: E-mail: [email protected] (Subject: OOP) ‏ Office hours: Jacobs 409, after the lecture - PowerPoint PPT Presentation

Citation preview

Page 1: Welcome to OOP course!

Welcome to OOP course!

203.1120

Page 2: Welcome to OOP course!

Course detailsCourse details• Teaching team

– Li-Tal Mashiach and Moran Lefler• Grading:

– 30% - Project in 3 parts– 70% - Final exam

• Contact me:– E-mail: [email protected] (Subject: OOP)– Office hours: Jacobs 409, after the lecture– Lecture slides will be available on the course site

http://cs.haifa.ac.il/courses/prog_tech/course_info.html

Page 3: Welcome to OOP course!

BibliographyBibliography

• B. Stroustrup, The C++ Programming Language

• S. Lippman and J. LaJoie, C++ Primer• S. Meyers, Effective C++• B. Eckel, Thinking in C++

Page 4: Welcome to OOP course!

1. Introduction to OOP and C++

Page 5: Welcome to OOP course!

Why OOP?Why OOP?• “Software Crisis”:

– Too many modules…– Too many functions…– Too many variables…

• Better organization of the code• Smaller code• Reuse of code• Easier design, analysis and implementation• User vs. Programmer

Page 6: Welcome to OOP course!

Why C++?Why C++?

• Object-oriented extension of C– Any C program is also valid in C++– Remains of non-OOP characteristics (global variables

and functions, main functions…)• C++ main elements:

– Encapsulation (כימוס(– Template (תבניות(– Inheritance (הורשה(– Polymorphism (רב צורתיות(– Exceptions (חריגות(

Page 7: Welcome to OOP course!

A bit of historyA bit of history• Time: 1962• Place: Norwegian Computing

Center, Oslo• Kristen Nygaard and Ole-Johan

Dahl work on simulation of ship movement in fjords– Many kinds of ships, each with its

own characteristics…

Page 8: Welcome to OOP course!

How it would have looked like?How it would have looked like?typedef struct {float sailArea;…} SailBoatData;typedef struct {float engineVolume;…} MotorBoatData;void move(Environment *env, Location *loc, int boatKind, void

*boatData){if (boatKind == SAIL_BOAT){

sailArea = ((SailBoatData*)boatData)->sailArea;… //compute movement using sail area, env, and loc

}else if (boatKind == MOTOR_BOAT){engineVolume = ((MotorBoatData*)boatData)->engineVolume;… //compute movement using engine volume, env, and loc

} else … //handle other kinds of boats{void turn(Direction * dir, int boatKind, void *boatData){

if (boatKind == SAIL_BOAT){… //handle sail boat case

}else if (boatKind == MOTOR_BOAT){… //handle the motor boat case

} else … //handle other kinds of boats}

//usage:int main(){ … //set up env. and boat data while(…){ move(env, loc, kind, data); }}

Page 9: Welcome to OOP course!

What are the problems?What are the problems?

• Difficult to distribute the work within team– Team member responsible for

• Kind of boats?– Must add code to every function

• Particular operation?– Must be expert in all boat kinds

• Difficult to share code between boat types• Difficult to add new boat types and operations• Difficult to maintain

Page 10: Welcome to OOP course!

A bit of history (cont.)A bit of history (cont.)

• Time: 1962• Place: Norwegian Computing Center, Oslo• Kristen Nygaard and Ole-Johan Dahl work on

simulation of ship movement in fjords– Many kinds of ships, each with its own characteristics…

• Solution: group the ships into classes– Each class of ships type has its own data and behavior

• Simula 67

Page 11: Welcome to OOP course!

How it would have looked like (2)?How it would have looked like (2)?class Boat {

private:Location loc;

public:void move (Environment *env) = 0;void turn (Direction *dir) = 0;Location getLocation(){return loc;}

}class SailBoat: public Boat {

private:float sailArea;

public: void move(Environment *env){…}void turn (Direction *dir){…}

} class MotorBoat: public Boat {

private:float engineVolume;

public: void move(Environment *env){…}void turn (Direction *dir){…}

}

//usage:int main(){ Boat boat= …; Environment * env = …; while(…){ boat.move(env); }}

Page 12: Welcome to OOP course!

How it would have looked like (2)?How it would have looked like (2)?“private” means the following elements are visible only inside the Boat class

“public” means anyone can use the following elements (like in struct)

//usage:int main(){ Boat boat= …; Environment * env = …; while(…){ boat.move(env); }}

class Boat {private:

Location loc;public:

void move (Environment *env) = 0;void turn (Direction *dir) = 0;Location getLocation(){return loc;}

}; class SailBoat: public Boat {

private:float sailArea;

public: void move(Environment *env){…}void turn (Direction *dir){…}

} class MotorBoat: public Boat {

private:float engineVolume;

public: void move(Environment *env){…}void turn (Direction *dir){…}

}

Page 13: Welcome to OOP course!

How it would have looked like (2)?How it would have looked like (2)?Here we declare that every Boat can move (i.e., has a method move)

And this is the C++ way to say that we are not yet going to specify how the boats move

//usage:int main(){ Boat boat= …; Environment * env = …; while(…){ boat.move(env); }}

class Boat {private:

Location loc;public:

void move (Environment *env) = 0;void turn (Direction *dir) = 0;Location getLocation(){return loc;}

}; class SailBoat: public Boat {

private:float sailArea;

public: void move(Environment *env){…}void turn (Direction *dir){…}

} class MotorBoat: public Boat {

private:float engineVolume;

public: void move(Environment *env){…}void turn (Direction *dir){…}

}

Page 14: Welcome to OOP course!

How it would have looked like (2)?How it would have looked like (2)?

//usage:int main(){ Boat boat= …; Environment * env = …; while(…){ boat.move(env); }}

This means that a SailBoat is a Boat and can do whatever a Boat can do

Specifically, this method describes how SailBoat movemoves

class Boat {private:

Location loc;public:

void move (Environment *env) = 0;void turn (Direction *dir) = 0;Location getLocation(){return loc;}

}; class SailBoat: public Boat {

private:float sailArea;

public: void move(Environment *env){…}void turn (Direction *dir){…}

} class MotorBoat: public Boat {

private:float engineVolume;

public: void move(Environment *env){…}void turn (Direction *dir){…}

}

Page 15: Welcome to OOP course!

How it would have looked like (2)?How it would have looked like (2)?

We know every boat can move, so we call the movemove method. At runtime, either move()move() from SailBoatSailBoat or move()move() from MotorBoatMotorBoat will be called, based on boat’s actual type

//usage:int main(){ Boat boat= …; Environment * env = …; while(…){ boat.move(env); }}

class Boat {private:

Location loc;public:

void move (Environment *env) = 0;void turn (Direction *dir) = 0;Location getLocation(){return loc;}

}; class SailBoat: public Boat {

private:float sailArea;

public: void move(Environment *env){…}void turn (Direction *dir){…}

} class MotorBoat: public Boat {

private:float engineVolume;

public: void move(Environment *env){…}void turn (Direction *dir){…}

}

Page 16: Welcome to OOP course!

How it would have looked like (2)?How it would have looked like (2)?

//usage:int main(){ Boat boat= …; Environment * env = …; while(…){ boat.move(env); }}

class Boat {private:

Location loc;public:

void move (Environment *env) = 0;void turn (Direction *dir) = 0;Location getLocation(){return loc;}

}; class SailBoat: public Boat {

private:float sailArea;

public: void move(Environment *env){…}void turn (Direction *dir){…}

} class MotorBoat: public Boat {

private:float engineVolume;

public: void move(Environment *env){…}void turn (Direction *dir){…}

}

Page 17: Welcome to OOP course!

What did we achieve?What did we achieve?• Described the problem in terms natural to the

problem– Pre-OO, we talked in terms of intints and floatfloats, like the

computer– Now we can talk in terms of boatboats and maneuvermaneuvers

• Partitioned the problem into encapsulatedencapsulated sub-problems with well-defined interfaces

• Adding new boat types is easy• And so is changing boat behavior

Page 18: Welcome to OOP course!

OOP – The Three Big IdeasOOP – The Three Big Ideas• Encapsulation

– Hide your data – then nobody can change it by mistake• private: Location loc;private: Location loc;

– Hide your algorithms – then you may change them at any time• Implementation of move(…)move(…) within each type

• Inheritance– Shared behavior implemented just once

• Boat::getLocation()Boat::getLocation()• Polymorphism

– Provide different method implementations in different types– Allows, when using objects, to keep to the right level of details

• main(){… boat.move(env);…}main(){… boat.move(env);…}

Page 19: Welcome to OOP course!

The OO view of the worldThe OO view of the world

• The world is a set of objects interacting with each other

:SailBoat :Environment

move() getLocation()

computeWind(Location)

computeCurrent(Location)

Page 20: Welcome to OOP course!

An aside: UMLAn aside: UML

• Unified Modeling Language– For spec, tutorials, etc. visit http://www.uml.org/– Generic description of classes, object interactions, etc.

:SailBoat :Environment

move() getLocation()

computeWind(Location)

computeCurrent(Location)

The interacting objects

This is an interaction diagram

This time is spent executing computeWind(Location)

Call computeCurrent(Location)

Return a value from computeCurrent(Location)

Page 21: Welcome to OOP course!

Back to the OO viewBack to the OO view

• The world is a set of objects interacting with each other

• Each object is an instanceinstance of a class– Same behaviorbehavior, different datadata

• Classes may inheritinherit data and functionality from other classes

Page 22: Welcome to OOP course!

Inheritance treeInheritance tree

Boat

SailBoat MotorBoatPaddleBoat

AircraftCarrier

Page 23: Welcome to OOP course!

UML Class diagramUML Class diagram

Boat

getLocation()move(Env)turn(Dir)

Location……

SailBoatfloat sailAreamove(Env)turn(Dir)

MotorBoatfloat engineVolumemove(Env)turn(Dir)

Data

Functionality

“Has-a” relationship

Hollow arrow means inheritance(“is-a” relationship)

Page 24: Welcome to OOP course!

2. Data Abstraction

Page 25: Welcome to OOP course!

Data typesData types• Built-in data types: int, float, …

– Have data• E.g., float has exponent, mantissa, sign bit

– Have operations defined on them• +, !=, …• How is the float ‘+’ implemented? Don’t know

• Goal: user-defined types that behave in the same way– Create, initialize, copy instances– Store state– Perform operations

Page 26: Welcome to OOP course!

Example: StringExample: StringClass declarationClass declaration

class String}char* chars;

public:char* getString();void setString(char* value);

{

String.h

int main(){String str; str.setString(“Hi everyone”);printf(“%s\n”, str.getString());

}

Page 27: Welcome to OOP course!

Example: StringExample: StringClass definitionClass definition

class String}char* chars;

public:char* getString();void setString(char* value);

{

String.cpp

char* String::getString(){return chars;}void String::setString(char* value){

int length = strlen(value);chars = new char[length+1];strcpy(value, chars);

}

String.h -- reminder

Page 28: Welcome to OOP course!

EncapsulationEncapsulation• Recall some of our objectives

– Develop classes independently– Facilitate implementation changes

• Example:– Replace float sailArea;sailArea; with float sailAreas[];sailAreas[];– Have to find everyone who uses sailAreasailArea and figure out how

to replace the usage --Potential for bugs!• But: if a field is not accessible outside the class, it

cannot be used there– Data hiding

Page 29: Welcome to OOP course!

C++: Limiting data visibilityC++: Limiting data visibility

• Visibility of class members limited in class declaration– private – accessible only within the class – protected – accessible only within the class and classes that inherit

from it– public – accessible anywhere

• private is the default access level class C{

int i; //private by defaultprivate:

int j1;int j2;

protected:int k;

public:int l;

}• This is just the basics…

class String} char* chars;public: char* getString(); void setString(char* value);

{

Page 30: Welcome to OOP course!

Example: String (cont.)Example: String (cont.)

• Let’s add string comparison!

class String}char* chars;

public:char* getString();void setString(char* value);int equals(String *other);

{

String.h int main(){String str1, str2; str1.setString(“Hi everyone”);str2.setString(“Hello”);if (str1.equals(str2) ==1)

printf(“equal!\n”); }

Page 31: Welcome to OOP course!

String comparison: 1String comparison: 1stst attempt attemptclass String}

char* chars;public:

char* getString();void setString(char* value);int equals(String *other);

{ String.h

char* String::getString(){return chars;}void String::setString(char* value){…}int String::equals(String *other){

if (strcmp(chars, other->chars) == 0)return 1;

return 0;}

String.cpp

Page 32: Welcome to OOP course!

Optimizing comparison Optimizing comparison

• Observation: – Most of the strcmpstrcmps are likely false

• Let’s improve performance by saving strcmpstrcmps– Run strcmpstrcmp only on strings of equal length– Cache string lengths

Page 33: Welcome to OOP course!

String comparison: 2String comparison: 2ndnd attempt attempt

class String}char* chars;int length;

public:char* getString();void setString(char* value);int equals(String *other);

{

String.h

Page 34: Welcome to OOP course!

String comparison: 2String comparison: 2ndnd attempt attempt

char* String::getString(){return chars;}void String::setString(char* value){

int length = strlen(value); chars = new char[length+1];strcpy(value, chars);

}int String::equals(String *other){

if (length != other->length)return 0;

if (strcmp(chars, other->chars) == 0)return 1;

return 0;}

String.cpp

Page 35: Welcome to OOP course!

String comparison: 2String comparison: 2ndnd attempt attempt

• Changes to the main() function:

Page 36: Welcome to OOP course!

String comparison: SummaryString comparison: Summary

• Defined a type and operations on it• Internal data representation was

concealed from the class clients• Changed implementation without touching

the client code