Transcript OOP - MTA

Object Oriented
Programming
Elhanan Borenstein
[email protected]
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
Course Page


Web: http://www.cs.tau.ac.il/~borens/teaching/oop-03b/

Updates & Notes

Presentation & Example from class
E-Mail: [email protected]

Subject: OOP Course

E-mail in pure English only
Books

Object Oriented Programming and C++ / Amir Kirsh

The C++ Programming Lnaguage / Bjarne Stroustrup

Effective C++, More Effective C++ / Scott Meyeres
copyrights © Elhanan Borenstein
Course Overview

OOP vs. C++ (can write any C++ app in c)
Knowledge of C is required!!!
Syllabus (partial !!!)

Introduction to OOP

C++

Overloading functions & operators

Classes & Objects

Inheritance & Polymorphism

…

Templates & STL

Introduction to OOD
copyrights © Elhanan Borenstein
Introduction to OOP and C++
Why OOP?

“Software Crisis” in procedural programming:

Too many modules…

Too many functions…

Too many variables…
An expensive mess!!!

Better organization of the code

Smaller code

Reuse of code

Easier design, analysis and implementation

User vs. Programmer
copyrights © Elhanan Borenstein
Introduction to OOP and C++
The Solution - Classes

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
copyrights © Elhanan Borenstein
Introduction to OOP and C++
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)
copyrights © Elhanan Borenstein
Before Classes…
copyrights © Elhanan Borenstein
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 – who’s fault is it?

Can we solve an ambiguity problem according to the
return value? Why?
copyrights © Elhanan Borenstein
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
By Reference (ByRef) Arguments
Argument in C

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!!!
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)
I/O in C

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
<iostream.h>)

We will use the operators “<<“ and “>>”

Thanks to function overloading, there is no need to
define the type of the arguments.
copyrights © Elhanan Borenstein
Input & Output (cin, cout)
Example 1 (output)
#include <iostream.h>
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 <iostream.h>
void main( )
{
int age;
char str[100]”;
cout<<“Please enter your name”;
cin>>str;
cout<<“Please enter your age”;
cin>>age;
}
copyrights © Elhanan Borenstein
Inline Functions
Motivation

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
The solution

Inline functions. The functions are embedded within
the call.

The compiler is not bound by the inline declaration.
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[]”.
copyrights © Elhanan Borenstein
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.

Should be used wisely!!!!
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)
copyrights © Elhanan Borenstein
Questions?
copyrights © Elhanan Borenstein