Download presentation
Presentation is loading. Please wait.
Published byJonas Jackson Modified over 9 years ago
1
Object Oriented Programming Elhanan Borenstein borens@tau.ac.il copyrights © Elhanan Borenstein
2
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
3
Administration Web: http://www.cs.tau.ac.il/~borens/teaching/oop-03b/http://www.cs.tau.ac.il/~borens/teaching/oop-03b/ Updates & Notes Presentation & Example from class E-Mail: borens@tau.ac.ilborens@tau.ac.il Subject: OOP Course E-mail 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
4
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
5
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
6
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
7
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
8
Before Classes… copyrights © Elhanan Borenstein
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
Input & Output (cin, cout) Example 2 (input) #include void main( ) { int age; char str[100]”; cout >str; cout >age; } copyrights © Elhanan Borenstein
17
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
18
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
19
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
20
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
21
Questions? copyrights © Elhanan Borenstein
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.