fast intro to object oriented programming c++ bmp librarybrd4.ort.org.il › ~ksamuel ›...

25
Fast Intro to Object Oriented Programming C++ BMP Library ORT Braude Engineering College, Course: Image Processing 31650, Lecturer: Dr. Samuel Kosolapov [email protected]

Upload: others

Post on 25-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Fast Intro toObject Oriented Programming

C++ BMP Library

ORT Braude Engineering College, Course: Image Processing 31650,Lecturer: Dr. Samuel Kosolapov [email protected]

Page 2: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

What are the problems with Plain C

2

Static 2D Array allocation: Image sizes can not be changed after compilation:

Solution: Usage of dynamic allocationC (alloc, malloc, calloc, etc )

C++ new and delete

Addres of Pixel {0,0}

Page 3: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Example of Pointer Arithmetic

3

Price

Page 4: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

What is wrong NOW?

4

Number of arguments in the functions starts to grow.Think about the following problem:

For each pixel"Pixel Value of Image C" = Maximum

of "Pixel Value of Image A" and "Pixel Value of Image B"

Function declaration will looks like: void Max( byte* A, int rA, int cA, byte *B, int rB, int cB, byte *C, int rC, int cC)

Are you sure that at 24:00 you will not put cC instead of rB ???

The problem here is not with the C -language at 24:00,the problem is with the programmer at 24;00

C-Language solution: structures. (And latter OOP Classes)

Page 5: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Usage of structures

5

All data relevant to image are stored together

as structure

Page 6: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

What is wrong now?

6

It’s easy to FORGET to call delete.Think about the following situation:

ImageA and ImageB were created. ImageC and ImageD still not.Then depending on values of central pixel s of Image A and B

ImageC or ImageD must be created and filledNow suppose that some problem happens

on entering algorithm parameters from keyboardQuestion: which images must be deleted and which not?

Not calling a delete leads to MEMORY LEAK– source of OS instability

(Each image grabs ~ 1MB of memory. Suppose that each 10 min relevant delete was not calledQ: When the computer will hang up?)

Page 7: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

What else is wrong ?

7

Security:Suppose some “smart programmer” writes something like this:

(mind -> syntax)

…or write this line in MAIN (by naive intention to increase size of the image)This is LEGAL but WRONG. Problem is with the language: classic C ENABLES this.

Page 8: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

What else is wrong ?

8

Security:In classical C (but not in C++, which we really use)

FUNCTION can be called with the WRONG argument(s):

Again, problem is with the language: classic C ENABLES this.

Page 9: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Radical Solution

9

Solution to THIS and many OTHER problems of the classic C:

Object Oriented Language(s): C++, C#, Java, OOP Pascal (Borland: Delphi)

Modern programmer has choice: All modern software is written by using Object Oriented Programming

(Even software for some microprocessors!!! (Parallax: Java, "Propeller") )

Disclaimer: C is still in use in some outdated software which must be supported for years:

Modern technology progress is too fast for academy and for industry!!!

Page 10: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

OBJECT ORIENTED PROGRAMMING

10

Main Goal: To help programmer to write RELIABLE (no crashes, no memory leaks, no non-closed files…)

software in a simple (more native syntax)

and reusable manner(use already written code in a simple way)

Idea #1: Code and data together

Page 11: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

class instead of structure+ code inside the class

11

Page 12: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

New Terminology

12

Now we have an OBJECT (instance of the CLASS)

CLASS OBJECT (Instance of the class) cImage Image;

Always compare with:TYPE (“dream”) Instance

“Really exists in the memory of the computer when executable is called”int A;Object has:

“properties/fields” (variables NumberOfRows, NumberOfColumns…) and “behavior” (functions – “members”, “member functions”)

Reusability:We intend to create classes/objects that are so “well designed”,

that they can be used in the many projects(Something like library)

To do this we must isolate class description and implementationfrom object declaration and usage. multi-file project

Page 13: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

3 Files

13

c2dByteGrayImage.hContains Description of the class

(fields and methods)

c2dByteGrayImage.cppContains Implementation of the

class functions (members)

ImProc.cpp

Contains reference (“include”, “using”) to c2dByteGrayImage.hContains main function (main, WinMain, …)Contains declaration of the INSTANCE of the classContains usage of the class fields and members

Visual Studio “knows” what we need:Main Menu : Project | Add class

Page 14: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Add class to the project

14

Page 15: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Add class to the project

15

While typing name of the class, Visual Studio

updates names of .h and .cpp files

Page 16: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

3 Files Created + Constructor & Destructor declared and “implemented”

16

Page 17: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

17

Special names:ConstructorDestructor

Image is going “out of scope”Destructor is called here

Image is Created hereConstructor is called here

Page 18: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Next idea: concept of constructor and destructor

18

Why are destructors and constructor useful? Answer: Dynamic memory (and other resources) allocation

Constructor is ideal place for new

Destructor is ideal place for delete - no chance to forget delete, - because destructor is called automatically!!!

Page 19: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Next idea: Code Management + Protection against low-qualified coder

19

ENABLE TO QUALIFIED PROGRAMMER (writer of the class) EVERYTHING

FORBID TO NON-QUALIFIED PROGRAMMER (user of the class) SOME OPERATIONS C++ OOP : Usage of words public - field or method is accessible by anywhere (that is by anyone) private- field or method is accessible only by writer of the class

(for example: only in c2dByteGrayImage.cpp and partly in h) protected - field or method is accessible only by writer of original class

AND by writer of NEXT version of the class (inheritance, later)

Newer use private without serious reason (explanations later) User of the class: NO DIRECT ACCESS FOR FIELDS - strange, but reliable. User can access Fields ONLY by calling METHODS (functions) Those METHODS can “check” user‟s input and “correct it” in a smart way

Reliability

Page 20: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Exemplary usage of public and protected

20

Page 21: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Example of code management

21

Page 22: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

Example of code management

22

Page 23: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

More C++ features

23

Page 24: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

More C++ : Error handling with“try-catch-throw”

24

Page 25: Fast Intro to Object Oriented Programming C++ BMP Librarybrd4.ort.org.il › ~ksamuel › ImProc.31651 › Laboratory.For Students › … · Fast Intro to Object Oriented Programming

More ideas

• Create a number of constructors. (But only ONE destructor)

• Delayed initialization (enable to change object after creation)

• Protect against bad values by using smart member functions

25

To Do: Explore CPP BMP Library