Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

C Language.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ fundamentals.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Object Oriented Programming Elhanan Borenstein Lecture #6.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Object Oriented Programming Elhanan Borenstein Lecture #4.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Object-Oriented Programming in C++
CLIP Command Line InterPreter for a subset of C++.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Object Oriented Programming Dr. Alon Schclar
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
Object Oriented Programming Elhanan Borenstein Lecture #5.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
From C to C++. What is C++ C++ is the work of Bjarne Stroustrup of AT&T Bell Labs. C++ is a mostly upward compatible extension of C that provides: A better.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Copyright 2005, The Ohio State University CSE – Introduction to C++ Name: Shirish Tatikonda Time: T 3:30 PM Room: BE 394
Object Oriented Programming Elhanan Borenstein Lecture #7.
Object-Oriented Programming (OOP) and C++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
CS SUMMER LECTURE 1 by İlker Korkmaz.
Object Oriented Programming
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
(5 - 1) Object-Oriented Programming (OOP) and C++
(5 - 1) Object-Oriented Programming (OOP) and C++
Programming Languages and Paradigms
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Presentation transcript:

Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein

Agenda Administration Course Overview Introduction to OOP and C++ Function Overloading & Default Parameters Arguments By Reference cin / cout Inline Functions Memory Allocation Additional Improvements copyrights © Elhanan Borenstein

Administration Web:  Updates & Notes  Presentation & Example from class  Subject: OOP Course  in pure English only Course Page Object Oriented Programming and C++ / Amir Kirsh The C++ Programming Lnaguage / Bjarne Stroustrup Effective C++, More Effective C++ / Scott Meyeres Books copyrights © Elhanan Borenstein

Course Overview Introduction to OOP C++  Overloading functions & operators  Classes & Objects  Inheritance & Polymorphism  … Templates & STL Introduction to OOD Syllabus (partial !!!) OOP vs. C++ (can write any C++ app in c) Knowledge of C is required!!! copyrights © Elhanan Borenstein

Introduction to OOP and C++ “Software Crisis” in procedural programming:  Too many modules…  Too many functions… An expensive mess!!!  Too many variables… Better organization of the code Smaller code Reuse of code Easier design, analysis and implementation User vs. Programmer Why OOP? copyrights © Elhanan Borenstein

Introduction to OOP and C++ The natural solution: focus on data!!! (instead of focusing on operations -functions) Define the data entities we want to use… Each entity is implemented as a class and defines:  The data we want to store.  The operations that could be applied to this data. Example:  Teachers Management Application Classes, instances and application The Solution - Classes copyrights © Elhanan Borenstein

Introduction to OOP and C++ An Object-Oriented extension of C.  Any C program is also valid in C++.  Remains of non-OOP characteristics (global variables and functions, main functions…).  Still using pointers !!!! A few notes on Java… C++ main elements:  Encapsulation  Inheritance  Polymorphism  Template (example: swap) (C++ only)  Exceptions (C++ only) C++ copyrights © Elhanan Borenstein

Before Classes… copyrights © Elhanan Borenstein

Function Overloading We would like to avoid writing / knowing / using a huge number of functions which in effect, do the same action. It is possible to define numerous functions with the same name, as long as the compiler can detect (while calling the function, according to its arguments), which function should be used. void printNice(int i); void printNice(int i, char ch); void printNice(int i, char* str); void printNice(float f); Motivation and Usage copyrights © Elhanan Borenstein

Function Overloading When the compiler cannot positively determine which function should be used, it will announce an ambiguity error. Ambiguity problem – who’s fault is it? Can we solve an ambiguity problem according to the return value? Why? void printNice(double d); void printNice(float f); The Ambiguity Problem copyrights © Elhanan Borenstein

Default Parameters It is possible to define default values for the last arguments of a function. These arguments can then be dropped when calling the functions. It is still possible to give a different value when calling the function (all previous arguments must be specified too).  Arguments order Default values are defined in the function prototype !!! (use a comment notation in the implementation…) Beware – Ambiguity!!!! void printReallyNice(char* str, int fontSize = 10, char color = 0); Usage copyrights © Elhanan Borenstein

By Reference (ByRef) Arguments In C, arguments are passed by Value. Changing the value of the arguments in the function, does not change the value of the original variables. If we wish to change the value of the original arguments, we can use pointers. Argument in C In C++, arguments are still passed by Value, but… A function can ask to get an argument by reference (ByRef). By reference arguments are in fact implemented with pointers, but hiding them from the user – a safer method!!! Argument in C++ copyrights © Elhanan Borenstein

By Reference (ByRef) Return Values A function can return a value by reference. It will in effect return a location in memory.  A by reference return value must be alive after the function terminates (global, input variables, …).  Can be used as LValue Example: Find() copyrights © Elhanan Borenstein

Input & Output (cin, cout) When using printf (or scanf), the programmer must define the type of each argument. We could write a different function for each type… I/O in C We can use the I/O objects cin and cout (defined in ) We will use the operators “ >” Thanks to function overloading, there is no need to define the type of the arguments. I/O in C++ copyrights © Elhanan Borenstein

Input & Output (cin, cout) Example 1 (output) #include void main( ) { int i = 23; char *str = “hello”; cout<<str; cout<<i<<endl; cout<<“the value of i is “<<i<<endl; cout<<(char)65; } copyrights © Elhanan Borenstein

Input & Output (cin, cout) Example 2 (input) #include void main( ) { int age; char str[100]”; cout >str; cout >age; } copyrights © Elhanan Borenstein

Inline Functions Each function call requires allocating memory on the stack. Overhead may outweighs the benefits (especially in small functions that will be called many times. Macros have other problems:  No type checking on the arguments  Readability Motivation Inline functions. The functions are embedded within the call. The compiler is not bound by the inline declaration. The solution copyrights © Elhanan Borenstein

Memory Allocation Memory allocation is implemented with the command “new”. No casting is required (unlike C). For arrays allocation we will use “new[n]”. Allocation To free allocated memory, we will use the command “delete”. For arrays, we will use “delete[]”. Freeing copyrights © Elhanan Borenstein

Additional Improvements C++ still supports the conventional notation of comments from C: /* this is a comment */ In addition, we can use a single comment line starting with // // initialization int index; // this is also a comment Comments copyrights © Elhanan Borenstein

Additional Improvements Variables can be defined at any point in the code. Should be used wisely!!!! Variable Definition When defining a struct or enum variable, no need to declare it is a struct / enum. (A reminder: in C we usually used typedef to solve this problem) Structs and enums Definition copyrights © Elhanan Borenstein

Questions? copyrights © Elhanan Borenstein