Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The...
Transcript of Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The...
![Page 1: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/1.jpg)
Welcome to Introduction to Object-Oriented
Programming (OOP)
COSC 2321
Dr. Mircea Agapie Office: SCIENCE 213-C [email protected]
254-968-0792
![Page 2: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/2.jpg)
Syllabus, textbook, etc.
![Page 3: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/3.jpg)
In this class, we’re going to use C++, one of the three most successful (OO) languages
(The other two are Java and Python)
![Page 4: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/4.jpg)
Source: http://blog.codeeval.com/codeevalblog/2015#.VdqgdE1759A=
![Page 5: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/5.jpg)
Source: http://spectrum.ieee.org/computing/software/the-2015-top-ten-programming-languages
![Page 6: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/6.jpg)
Source: http://redmonk.com/sogrady/2015/07/01/language-rankings-6-15/
![Page 7: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/7.jpg)
Ch. 1: Introduction to Objects
![Page 8: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/8.jpg)
The progress of abstraction
The fundamental problem of computer programming: traversing the gulf between “machine space” and “problem space”.
This is done with the help of abstraction(s).
![Page 9: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/9.jpg)
Low-level languages are “too close” to the machine
http://www.chipsetc.com/gold-value-in-computer-chips.html https://en.wikipedia.org/wiki/Robot-assisted_surgery
Pro
gram
min
gLa
ngu
age
Pros? Cons?
Examples: all machine, assembly and procedural languages
![Page 10: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/10.jpg)
Domain-specific languages are “too close” to the problem
http://www.chipsetc.com/gold-value-in-computer-chips.html https://en.wikipedia.org/wiki/Robot-assisted_surgeryP
rogr
amm
ing
Lan
guag
e
Pros? Cons?
Examples: Lisp, Prolog, VHDL, Matlab, VAL (programming language for robotics), etc.
![Page 11: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/11.jpg)
OO languages are “in the middle”
http://www.chipsetc.com/gold-value-in-computer-chips.html https://en.wikipedia.org/wiki/Robot-assisted_surgery
Pro
gram
min
gLa
ngu
age
![Page 12: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/12.jpg)
Very short history of C++
• 1970s: C is invented and becomes very popular• C is a procedural language, and, as real-life C
programs become large, it gets harder to write, maintain and debug them, esp. when they are developed by teams, rather than individual programmers. This is the problem of scalability.
• 1980s: C++ is invented and becomes very popular• C++ combines the procedural features of C with OO
features (from Simula and other languages) –classes, information hiding, inheritance, polymorphism – thus greatly improving scalability.
![Page 13: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/13.jpg)
Let’s see how much C we remember
What is the net effect of running this program?
Not in text
![Page 14: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/14.jpg)
Let’s see how much C we remember
Can you rewrite the if statement in a simpler form?
Hint: true and false are represented in C as 1 and 0, respectively.
Not in text
![Page 15: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/15.jpg)
Can you rewrite the if statement in a simpler form?
Not in text
![Page 16: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/16.jpg)
Let’s see how much C we remember
One of these programs will crash; which one, and why?
Not in text
![Page 17: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/17.jpg)
![Page 18: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/18.jpg)
Let’s see how much C we remember
Review/interview – style questions at
http://www.mycquiz.com/
Not in text
![Page 19: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/19.jpg)
Not in text
![Page 20: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/20.jpg)
Not in text
![Page 21: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/21.jpg)
Although some (most) compilers agree in simple cases, they don’t agree in all cases; this is the meaning of undefined behavior.
![Page 22: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/22.jpg)
Review/interview – style questions at
http://www.mycquiz.com/
Ongoing assignment: Answer/study/understand all 26
questions!
EOL1
![Page 23: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/23.jpg)
QUIZ
Explain very briefly why OOLs are “better” than Procedural Languages at solving real-life problems. Why are they worse?
![Page 24: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/24.jpg)
Alan Curtis Kay (born May 17, 1940) is an American computer scientist. He has been elected a Fellow of the American Academy of Arts and Sciences, the National Academy of Engineering, and the Royal Society of Arts. He is best known for his pioneering work on object-oriented programming and windowing graphical user interface design.
He was the inventor of Smalltalk, the first version of which, known as Smalltalk-71, was created by Kay in a few mornings on a bet that a programming language based on the idea of message passing inspired by Simula could be implemented in "a page of code."
[Source: Wikipedia]
![Page 25: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/25.jpg)
![Page 26: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/26.jpg)
![Page 27: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/27.jpg)
Remember:
There are no “pure” OO languages. (Even Simula and Smalltalk have loops and decisions!)
C++ is no exception to this rule, being a mixture of procedural, OO, and functional features.C++ is a superset of C.
![Page 28: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/28.jpg)
An object has an interface
In OO parlance, “class” means “type”
Simula-67 used the keyword class for the first time
The difference between built-in types and abstracttypes …
An OO programming system welcomes the new, abstract classes defined by the programmer, and gives them all the care and type-checking that it gives to built-in types.
![Page 29: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/29.jpg)
An OO programming system welcomes the new, abstract classes defined by the programmer, and gives them all the care and type-checking that it gives to built-in types.
Wait a second – what’s the big deal? C also has abstract types, they are called ______________.
![Page 30: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/30.jpg)
C also has abstract types, they are called arrays, structs and unions.
However, they are not handled the same as the simple, built-in types; for instance:• Structs and unions cannot be compared for equality.• Arrays cannot be compared for equality, nor assigned to
each other.
![Page 31: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/31.jpg)
Once a class is established, you can make as many objects of that class as you likeObjects communicate through interfacesThe interface defines what actions are possible
![Page 32: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/32.jpg)
This is an example of a black-box diagram …
… but what is inside the box?
![Page 33: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/33.jpg)
The hidden implementation
Inside the box/object there must be:
• Data that defines the state of the object
• Code that is called to satisfy the requests of the interface.
In a pure OOL, both data and code are “hidden”; they together comprise the implementation.
New in C++
![Page 34: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/34.jpg)
Why is hiding important in programming?
B/c it provides modularity, so we:
• Allow the users to work with a simpler, abstractview of the object
• Prevent the users from inadvertently messing-up the inner workings of the object
• Can later tweak/optimize the implementation w/o worrying about unwanted effects in other parts of the program
![Page 35: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/35.jpg)
Hiding is a.k.a. separating the interface from the implementation
C++ uses three explicit keywords to set the boundaries in a class: public, private, protected.
![Page 36: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/36.jpg)
Reusing the implementation
Composition
Aggregation
Association
MO
RE
GEN
ERA
L
![Page 37: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/37.jpg)
Owner classComponent
class
Of the 3 relations, only Composition is directly supported in C++
![Page 38: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/38.jpg)
Composition example (not in text)
This is similar to nested structs!
![Page 39: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/39.jpg)
Improved example (not in text)
These are similar to pointers to
structs!
In composition, when the owning
object is destroyed, so are the
component objects
The remainder of Lecture 2 is from Ch.2
![Page 40: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/40.jpg)
QUIZ
1. When/where was the keyword class first used in a computer language?
2. In OOL, the _____________ of a class must be kept separate from the _______________.
3. The above concept is called ________ing the ______________.
4. What are the benefits of doing so?5. What technique for code reuse did we cover?
![Page 41: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/41.jpg)
Owner classComponent
class
Reusing the interface: composition
![Page 42: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/42.jpg)
Reusing the interface: inheritance
Another UML diagram!
![Page 43: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/43.jpg)
This is called a type hierarchy
Another UML diagram!
![Page 44: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/44.jpg)
“Casting the solution in the same terms as the problem is tremendously beneficial because you don’t need a lot of intermediate models to get from a description of the problem to a description of the solution. “
![Page 45: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/45.jpg)
“With objects, the type hierarchy is the primary model, so you go directly from the description of the system in the real world to the description of the system in code.
Indeed, one of the difficulties people have with object-oriented design is that it’s too simple to get from the beginning to the end. A mind trained to look for complex solutions is often stumped by this simplicity at first.”
![Page 46: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/46.jpg)
“With objects, the type hierarchy is the primary model, so you go directly from the description of the system in the real world to the description of the system in code.
Critical thinking: What is the unmentioned assumption in the quote above?
Not in text
![Page 47: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/47.jpg)
“With objects, the type hierarchy is the primary model, so you go directly from the description of the system in the real world to the description of the system in code.
Critical thinking: What is the unmentioned assumption in the quote above?A: That the real-world system has a hierarchical structure in the first place!
Not in text
![Page 48: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/48.jpg)
Examples of problems that are notmodeled well with type hierarchies
(or inheritance):
• Anything that involves time: cause/effect, transactions, sequencing, correlations, etc.
• Anything that involves large numbers of identical elements: brains, ant colonies, weather simulations, finite-element models, etc.
Not in text
![Page 49: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/49.jpg)
When all you have is a hammer …
Not in text
![Page 50: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/50.jpg)
After this warning, let’s recognize that many real-life systems/problems are
hierarchical – that’s one of the reasons OOP became successful!
![Page 51: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/51.jpg)
How to differentiate the new, derived class from the original, base class?
A. Add more functions/methods
![Page 52: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/52.jpg)
How to differentiate the new, derived class from the original, base class?
B. Change the behavior of an existing base-class function
![Page 53: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/53.jpg)
We say that the base function is overriden. The base and derived functions of the same name are said to be polymorphic.
B. Change the behavior of an existing base-class function
![Page 54: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/54.jpg)
How to differentiate the new, derived class from the original, base class?
C. Remove (delete)an existing base-class function There is no standard UML
diagram for this!
![Page 55: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/55.jpg)
Creating and destroying objects
Source: Leo Ferres, Memory management in C: The heap and the stack
Staticstorage
Contains:• Local, static and constant
objects• Unitialized global objects
Contains:• Local auto objects
Originally it meant “Block Started by
Symbol”
Remember: In C (and C++) uninitialized static variables are initialized by default with 0.
Contains:• Objects allocated dynamically
![Page 56: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/56.jpg)
Speed vs. flexibility
Fast to access, but inflexible - the number of objects and their types must be known at compile-time.
Slower to access, but flexible - the number of objects and their types are decided at run-time, with malloc/free in C, and new/delete in C++.
![Page 57: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/57.jpg)
Lifetime of objectsCompiler has no knowledge of lifetime of dynamic objects, stored on heap → It’s the programmer’s responsibility to delete them.
![Page 58: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/58.jpg)
“As an alternative, the environment can provide a feature called a garbage collector that automatically discovers when an object is no longer in use and destroys it.
[…] writing programs using a garbage collector is much more convenient, but it requires that all applications must be able to tolerate the existence of the GC and its overhead. This does not meet the design requirements of the C++ language and so it was not included, although third-party garbage collectors exist for C++.”
Java has built-in GC.
![Page 59: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/59.jpg)
Exception handling: dealing with errors
In C, errors are handled through return codes from functions, e.g.:
![Page 60: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/60.jpg)
C++ exception handling
In C++, an exception is an object that is “thrown” from the site of the error and can be “caught” by an appropriate exception handler designed to handle that particular type of error.
C++ exceptions are objects dynamically allocated from the heap.
![Page 61: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/61.jpg)
Unlike C, the throwing is done automatically,
with minimal programming effort
Source: http://stackoverflow.com/questions/9670396/exception-handling-and-opening-a-file
![Page 62: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/62.jpg)
Multiple exceptions can be caught from the
same try … catch block
Source: http://stackoverflow.com/questions/10650219/try-catch-exception-handling-c
![Page 63: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/63.jpg)
Continue taking notes in your notebook!
--Analysis and design--The 5 phases of the design process--Extreme programming--Why C++ succeeds--Strategies for transition
![Page 64: Welcome to Introduction to Object-Oriented Programming … · Examples: Lisp, Prolog, VHDL, ... The difference between built-in types and abstract types ... malloc/free in C, and](https://reader030.fdocuments.in/reader030/viewer/2022021801/5b376a107f8b9a4a728c0ce5/html5/thumbnails/64.jpg)
Pair programming
• You will experiment with it in more advanced classes, like Software Engineering.
• In our class, teamwork is OK (recommended, actually!) when discussing problems, but all programming must be done individually!
EOL 3