Fast Intro toObject Oriented Programming
C++ BMP Library
ORT Braude Engineering College, Course: Image Processing 31650,Lecturer: Dr. Samuel Kosolapov [email protected]
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}
Example of Pointer Arithmetic
3
Price
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)
Usage of structures
5
All data relevant to image are stored together
as structure
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?)
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.
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.
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!!!
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
class instead of structure+ code inside the class
11
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
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
Add class to the project
14
Add class to the project
15
While typing name of the class, Visual Studio
updates names of .h and .cpp files
3 Files Created + Constructor & Destructor declared and “implemented”
16
17
Special names:ConstructorDestructor
Image is going “out of scope”Destructor is called here
Image is Created hereConstructor is called here
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!!!
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
Exemplary usage of public and protected
20
Example of code management
21
Example of code management
22
More C++ features
23
More C++ : Error handling with“try-catch-throw”
24
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