Upload
dangnhu
View
242
Download
0
Embed Size (px)
Citation preview
Welcome to Introduction to Object-Oriented
Programming (OOP)
COSC 2321
Dr. Mircea Agapie Office: SCIENCE 213-C [email protected]
254-968-0792
Syllabus, textbook, etc.
In this class, we’re going to use C++, one of the three most successful (OO) languages
(The other two are Java and Python)
Source: http://blog.codeeval.com/codeevalblog/2015#.VdqgdE1759A=
Source: http://spectrum.ieee.org/computing/software/the-2015-top-ten-programming-languages
Source: http://redmonk.com/sogrady/2015/07/01/language-rankings-6-15/
Ch. 1: Introduction to Objects
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).
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
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.
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
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.
Let’s see how much C we remember
What is the net effect of running this program?
Not in text
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
Can you rewrite the if statement in a simpler form?
Not in text
Let’s see how much C we remember
One of these programs will crash; which one, and why?
Not in text
Let’s see how much C we remember
Review/interview – style questions at
http://www.mycquiz.com/
Not in text
Not in text
Not in text
Although some (most) compilers agree in simple cases, they don’t agree in all cases; this is the meaning of undefined behavior.
Review/interview – style questions at
http://www.mycquiz.com/
Ongoing assignment: Answer/study/understand all 26
questions!
EOL1
QUIZ
Explain very briefly why OOLs are “better” than Procedural Languages at solving real-life problems. Why are they worse?
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]
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.
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.
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 ______________.
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.
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
This is an example of a black-box diagram …
… but what is inside the box?
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++
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
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.
Reusing the implementation
Composition
Aggregation
Association
MO
RE
GEN
ERA
L
Owner classComponent
class
Of the 3 relations, only Composition is directly supported in C++
Composition example (not in text)
This is similar to nested structs!
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
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?
Owner classComponent
class
Reusing the interface: composition
Reusing the interface: inheritance
Another UML diagram!
This is called a type hierarchy
Another UML diagram!
“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. “
“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.”
“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
“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
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
When all you have is a hammer …
Not in text
After this warning, let’s recognize that many real-life systems/problems are
hierarchical – that’s one of the reasons OOP became successful!
How to differentiate the new, derived class from the original, base class?
A. Add more functions/methods
How to differentiate the new, derived class from the original, base class?
B. Change the behavior of an existing base-class function
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
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!
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
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++.
Lifetime of objectsCompiler has no knowledge of lifetime of dynamic objects, stored on heap → It’s the programmer’s responsibility to delete them.
“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.
Exception handling: dealing with errors
In C, errors are handled through return codes from functions, e.g.:
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.
Unlike C, the throwing is done automatically,
with minimal programming effort
Source: http://stackoverflow.com/questions/9670396/exception-handling-and-opening-a-file
Multiple exceptions can be caught from the
same try … catch block
Source: http://stackoverflow.com/questions/10650219/try-catch-exception-handling-c
Continue taking notes in your notebook!
--Analysis and design--The 5 phases of the design process--Extreme programming--Why C++ succeeds--Strategies for transition
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