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.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Chapter 1 Principles of Programming and Software Engineering.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
© Copyright Eliyahu Brutman Programming Techniques Course מוקדש לזכרו של פרופ' לב ברוטמן ( ) חוקר בעל שם עולמי, ממייסדי החוג למדעי המחשב באוניברסיטת.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Object Oriented Software Development
Introduction to Object-oriented programming and software development Lecture 1.
Design Patterns OOD. Course topics Design Principles UML –Class Diagrams –Sequence Diagrams Design Patterns C#,.NET (all the course examples) Design Principles.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
CSC 212 Object-Oriented Programming and Java Part 1.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
SNPL1 Woochang Lim What (Variable) + How (Function) = Object Objects are the physical and conceptual things we find in the universe around us. Object-Oriented.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Salman Marvasti Sharif University of Technology Winter 2015.
Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Introduction to Object-Oriented Programming Lesson 2.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism © 2011 | PN AZRINA.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Previous lecture Introduction to OOP and C++ Data Abstraction –String example.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Unified Modeling Language (UML)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
LThe C++ programming language Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 4./0. lThe object oriented view of the world.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance ITI1121 Nour El Kadri.
Review: Two Programming Paradigms
C++ Classes & Object Oriented Programming
About the Presentations
OBJECT ORIENTED PROGRAMMING overview
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Chapter 4: Writing Classes
Introduction to Data Structure
Presentation transcript:

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: – (Subject: –Office hours: Jacobs 409, after the lecture –Lecture slides will be available on the course site

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

1.Introduction to OOP and C++

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

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

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…

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

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

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

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

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

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

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

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

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

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

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

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)‏

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

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

Inheritance tree Boat SailBoatMotorBoatPaddleBoat AircraftCarrier

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)‏

2. Data Abstraction

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

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

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

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

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

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

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

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

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

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

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

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