Beruflich Dokumente
Kultur Dokumente
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
Course Page
Web: http://www.cs.tau.ac.il/~borens/teaching/oop-03b/
Updates & Notes Presentations & Examples from class
E-Mail: borens@tau.ac.il
Subject: OOP Course E-mail in pure English only
Books
Object Oriented Programming and C++ / Amir Kirsh The C++ Programming Language / Bjarne Stroustrup Effective C++, More Effective C++ / Scott Meyers
copyrights Elhanan Borenstein
Course Overview
OOP vs. C++ (can write any C++ app in c)
Knowledge of C is required!!!
Better organization of the code Smaller code Reuse of code Easier design, analysis and implementation User vs. Programmer
Example:
Teachers Management Application
Before Classes
Function Overloading
Motivation and Usage
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);
copyrights Elhanan Borenstein
Function Overloading
The Ambiguity Problem
When the compiler cannot positively determine which function should be used, it will announce an ambiguity error. void printNice(double d); void printNice(float f); Ambiguity problem whose fault is it? Can we solve an ambiguity problem according to the return value? Why?
Default Parameters
Usage
It is possible to define default values for the last arguments of a function. These arguments can then be dropped when calling the functions. void printReallyNice(char* str, int fontSize = 10, char color = 0); 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!!!!
copyrights Elhanan Borenstein
Arguments 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!!!
copyrights Elhanan Borenstein
Example: Find()
I/O in C++
We can use the I/O objects cin and cout (defined in <iostream.h>) We will use the operators << and >> Thanks to function overloading, there is no need to define the type of the arguments.
Inline Functions
Motivation
Each function call requires allocating memory on the stack. Overhead may outweigh the benefits (especially in small functions that will be called many times.
double SQR(double num) { return num*num; }
Inline Functions
The solution
Inline functions. The functions are embedded within the call.
inline double SQR(double num) { return num*num; }
Comments:
The compiler is not bound by the inline declaration. The functions implementation should be included in the same file !!! Bigger executables (though, more efficient)
copyrights Elhanan Borenstein
Memory Allocation
Allocation
Memory allocation is implemented with the command new. No casting is required (unlike C). For arrays allocation we will use new[n].
Freeing
To free allocated memory, we will use the command delete. For arrays, we will use delete[ ].
Memory Allocation
... int *pInt = new int; cout<<Please insert an integer:; cin>>*pInt; int n; cout<<Please insert size of array:; cin>>n; float *floatArray = new float[n]; delete pInt; delete []floatArray;
Additional Improvements
Comments
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
copyrights Elhanan Borenstein
Additional Improvements
Variable Definition
Variables can be defined at any point in the code. ... int sum; for (int I = 0; I < 10 ; i++) { int item = GetItem(i)*2; sum += item; } Should be used wisely!!!!
copyrights Elhanan Borenstein
Additional Improvements
Structs and enums 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)
in C struct student { char *name; int grade; }; struct student Rafi; -- or typedef struct student st; st Rafi; in C++ student Rafi;
copyrights Elhanan Borenstein
Questions?