Top Banner
Object Oriented Programming Elhanan Borenstein [email protected] copyrights © Elhanan Borenstein
21

Object Oriented Programming Elhanan Borenstein [email protected] copyrights © Elhanan Borenstein.

Jan 01, 2016

Download

Documents

Jonas Jackson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il copyrights © Elhanan Borenstein.

Object OrientedProgramming

Elhanan Borenstein

[email protected]

copyrights © Elhanan Borenstein

Page 2: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 3: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il copyrights © Elhanan Borenstein.

Administration

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

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

Page 4: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 5: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 6: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 7: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 8: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il copyrights © Elhanan Borenstein.

Before Classes…

copyrights © Elhanan Borenstein

Page 9: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 10: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 11: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 12: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 13: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 14: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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 <iostream.h>) We will use the operators “<<“ and “>>” Thanks to function overloading, there is no need to define the type

of the arguments.

I/O in C++

copyrights © Elhanan Borenstein

Page 15: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 16: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 17: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 18: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 19: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 20: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il 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

Page 21: Object Oriented Programming Elhanan Borenstein borens@tau.ac.il copyrights © Elhanan Borenstein.

Questions?

copyrights © Elhanan Borenstein