Presentation is loading. Please wait.

Presentation is loading. Please wait.

Welcome to OOP course! 203.1120. Course details Teaching team –Li-Tal Mashiach and Moran Lefler Grading: –30% - Project in 3 parts –70% - Final exam Contact.

Similar presentations


Presentation on theme: "Welcome to OOP course! 203.1120. Course details Teaching team –Li-Tal Mashiach and Moran Lefler Grading: –30% - Project in 3 parts –70% - Final exam Contact."— Presentation transcript:

1 Welcome to OOP course! 203.1120

2 Course details Teaching team –Li-Tal Mashiach and Moran Lefler Grading: –30% - Project in 3 parts –70% - Final exam Contact me: –E-mail: litalma@cs.technion.ac.il (Subject: OOP)‏litalma@cs.technion.ac.il –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

3 Bibliography B. Stroustrup, The C++ Programming Language S. Lippman and J. LaJoie, C++ Primer S. Meyers, Effective C++ B. Eckel, Thinking in C++

4 1.Introduction to OOP and C++

5 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

6 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 (חריגות(

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

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

9 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

10 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

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

12 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){…} }

13 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){…} }

14 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 move Specifically, this method describes how SailBoat move s 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){…} }

15 How it would have looked like (2)? move move()SailBoat move()MotorBoat We know every boat can move, so we call the move method. At runtime, either move() from SailBoat or move() from MotorBoat 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){…} }

16 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){…} }

17 What did we achieve? Described the problem in terms natural to the problem intfloat –Pre-OO, we talked in terms of ints and floats, like the computer boatmaneuver –Now we can talk in terms of boats and maneuvers encapsulatedPartitioned the problem into encapsulated sub- problems with well-defined interfaces Adding new boat types is easy And so is changing boat behavior

18 OOP – 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 move(…)Implementation of 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);…}

19 The OO view of the world The world is a set of objects interacting with each other :SailBoat:Environment move()‏ getLocation()‏ computeWind(Location)‏ computeCurrent(Location)‏

20 An aside: UML Unified Modeling Language –For spec, tutorials, etc. visit http://www.uml.org/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)‏

21 Back to the OO view The world is a set of objects interacting with each other instanceEach object is an instance of a class behaviordata –Same behavior, different data inheritClasses may inherit data and functionality from other classes

22 Inheritance tree Boat SailBoatMotorBoatPaddleBoat AircraftCarrier

23 UML Class diagram Boat getLocation()‏ move(Env)‏ turn(Dir)‏ Location … SailBoat float sailArea move(Env)‏ turn(Dir)‏ MotorBoat float engineVolume move(Env)‏ turn(Dir)‏ Data Functionality “Has-a” relationship Hollow arrow means inheritance (“is-a” relationship)‏

24 2. Data Abstraction

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

26 Example: String Class 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()); }

27 Example: String Class 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

28 Encapsulation Recall some of our objectives –Develop classes independently –Facilitate implementation changes Example: sailArea;sailAreas[]; –Replace float sailArea; with float sailAreas[]; sailArea –Have to find everyone who uses sailArea 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

29 C++: 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 default private: 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); }

30 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”); }

31 String comparison: 1 st attempt class 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

32 Optimizing comparison Observation: strcmp –Most of the strcmp s are likely false strcmpLet’s improve performance by saving strcmp s strcmp –Run strcmp only on strings of equal length –Cache string lengths

33 String comparison: 2 nd attempt class String{ char* chars; int length; public: char* getString(); void setString(char* value); int equals(String *other); } String.h

34 String comparison: 2 nd 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

35 String comparison: 2 nd attempt Changes to the main() function:

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


Download ppt "Welcome to OOP course! 203.1120. Course details Teaching team –Li-Tal Mashiach and Moran Lefler Grading: –30% - Project in 3 parts –70% - Final exam Contact."

Similar presentations


Ads by Google