Object Orientation
description
Transcript of Object Orientation
![Page 1: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/1.jpg)
Object Orientation
“Thinking” Object Oriented, Further Constraints, and
Documentation
![Page 2: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/2.jpg)
An Aside• To some of you, I imagine that some
of C++’s syntax and structure may be pretty foreign, to say the least.– In particular, some people have never
worked (heavily) with OO before.– This is because there’s a whole different
way of thinking about programming tasks in OO.
![Page 3: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/3.jpg)
Programming Paradigms• In the world, there are over 6900
different spoken languages.• Some of these languages are more
similar to each other than others.– Consider Spanish, French, Italian…– Consider Hindi, Urdu… or perhaps
Mandarin Chinese.
![Page 4: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/4.jpg)
Programming Paradigms• Just as there are families of real-
world spoken languages, there are multiple families – or paradigms – of programming languages.
![Page 5: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/5.jpg)
Imperative• Chances are, most of your
programming experience is in the imperative paradigm.– Think C, for example.– Code is executed one line after another,
step by step.– The focus is on the order of code
execution.
![Page 6: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/6.jpg)
Imperative• Imperative-style programs make little
attempt to enforce good data organization habits.– This must be handled by the
programmer.– Data is often in a global scope,
accessible anywhere, at any time, by anyone.• As are functions.
![Page 7: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/7.jpg)
Object-Orientation• Object-orientation is quite different.– As we’ve seen already, part of its design
is to enforce the organization of data into logical, conceptual units within the system.
– Each object keeps its data private (ideally) and seeks to enforce constraints to keep itself in a proper form.
![Page 8: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/8.jpg)
Object-Orientation• Object-orientation is quite different.–Work gets done by objects interacting
with other objects.• As such, the exact flow of execution in the
program may not be easy to track.– Object orientation aims to avoid making
anything truly global.• Java doesn’t even allow “truly” global
variables… though it’s easily possible to compensate for this. C++ allows them.
![Page 9: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/9.jpg)
Coding in OO• So far, we’ve seen a few examples of
OO code, but I haven’t really broken down what’s going on behind the scenes to make everything work.
• Let’s look at an example and talk through it.
![Page 10: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/10.jpg)
A Fraction Objectclass Fraction{
private:int numerator;int denominator;
public:Fraction add(Fraction &f);
}
![Page 11: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/11.jpg)
Exercise 1• Code up the Fraction object• Include constructor with two ints• Include no-arg constructor• Include accessor and mutator
methods• Make a main() that instantiates a
fraction, sets its member variables, and prints it out
![Page 12: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/12.jpg)
A Fraction Objectpublic Fraction* Fraction::add(Fraction &f){
int num = numerator * f.denominator;num += f.numerator * denominator;int dnm = f.denominator * denominator;
return new Fraction(num, dnm);}
![Page 13: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/13.jpg)
Exercise 2• Now add the Fraction::add() method• Add to main() another fraction
instantiation, add the two fractions, and print out the result
• Compile and test
![Page 14: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/14.jpg)
Ways to Create Objects• Can instantiate f1 by initialization– Fraction f1(i,j);
• Can instantiate f1 by new– Fraction *f1 = new Fraction(i,j);– But f1 is not a Fraction…–… it is a pointer to a Fraction object
![Page 15: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/15.jpg)
Ways to Reference Objects
• When f1 instantiated by initialization– Fraction f1(i,j);– Access members by “.”
• When f1 instantiated by new– Fraction *f1 = new Fraction(i,j);– Access members by “->”– Use object by “*” dereference
![Page 16: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/16.jpg)
Coding in OO• First, let’s examine this line of code.
f1.add(f2); //Both are Fractions
• What is this setting up and modeling?
• Secondly, what is going on in add()?
![Page 17: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/17.jpg)
f1.add(f2); //Both are Fractions
• This line is basically saying “Call the “Fraction.add()” method from the perspective of f1.
Coding in OO
![Page 18: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/18.jpg)
A Fraction Object
public Fraction* Fraction::add(Fraction &f){
int num = numerator * f.denominator;num += f.numerator * denominator;int dnm = f.denominator * denominator;
return new Fraction(num, dnm);}
So, that line of code has an implied reference to what was previously called “f1.”
![Page 19: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/19.jpg)
A Fraction Object
public Fraction* Fraction::add(Fraction &f){
int num = numerator * f.denominator;num += f.numerator * denominator;int dnm = f.denominator * denominator;
return new Fraction(num, dnm);}
This “implied reference” is known as this within C++. It’s understood to be implied on any
“unqualified” field names in the method below.
![Page 20: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/20.jpg)
A Fraction Object
public Fraction* Fraction::add(Fraction &f){
int num = numerator * f.denominator;num += f.numerator * denominator;int dnm = f.denominator * denominator;
return new Fraction(num, dnm);}
The use of “numerator” and “denominator”, when not preceded by “f.” here, are with
respect to this.
![Page 21: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/21.jpg)
A Fraction Object
public Fraction* Fraction::add(Fraction &f){
int num = numerator * f.denominator;num += f.numerator * denominator;int dnm = f.denominator * denominator;
return new Fraction(num, dnm);}
What about when we do have “f.” preceding numerator and denominator?
![Page 22: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/22.jpg)
A Fraction Object
public Fraction* Fraction::add(Fraction &f){
int num = numerator * f.denominator;num += f.numerator * denominator;int dnm = f.denominator * denominator;
return new Fraction(num, dnm);}
In such cases, the perspective shifts to that of the object f, from which it then operates for the
field or method after the “.”.
![Page 23: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/23.jpg)
f1.add(f2); //Both are Fractions
• Even though the add() method is operating with two different Fraction class instances, the code is able to keep track of which is this and which is the parameter f.
Coding in OO
![Page 24: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/24.jpg)
An Aside• There also exist other programming
paradigms.–We’ll visit some others later on, but I’d
rather keep things here for now and avoid confusion.
![Page 25: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/25.jpg)
Questions?
![Page 26: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/26.jpg)
• For the rest of this lecture, we’ll switch gears to examining documentation and a bit more in regard to analysis.
![Page 27: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/27.jpg)
Documentation• Documentation is the “plain” English
text accompanying code that seeks to explain its structure and use.– Some of this documentation is typically
in comments, directly in the code.– Other documentation may be in external
documents.
![Page 28: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/28.jpg)
Documentation Types• I expect four kinds of documentation
– Source comments – enough to follow code– README file – how to install and configure, info on
version #, etc.– User documentation – man page on how to use– Project report – description of purpose, theory,
design, analysis, verification, and validation• In addition, source code should include
– Header files(s)– Implementation file(s)– Application (driver) file(s)– Makefile
![Page 29: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/29.jpg)
Project Documentation• Project report – should give the
purpose, the overall structure of all the system, as well as the theory of operation, analysis of efficiency, proofs (if any), test plan and results, and known bugs– Comments in code necessarily follow the
actual layout of the code, which may not be the best for understanding it
![Page 30: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/30.jpg)
Code Documentation• For complex code, it can be very
helpful to place inline comments on a “paragraph” level, explaining what purpose that block of code is accomplishing.– A line-by-line commentary may clarify what the code is doing, but rarely indicates why.• Note the purpose of your code – its goal.
![Page 31: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/31.jpg)
Code Documentation• We’ve already noted two different
ways to comment within C++:
// This is a one-line comment.
/* This is a block comment, spanning multiple lines. */
![Page 32: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/32.jpg)
Code Documentation• In producing documentation for a
method, it is wise to place some form of the “relationships” criterion within the description.– Generally, the conceptual purpose which
a method, field, or class serves.
![Page 33: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/33.jpg)
Code Documentation• One should also include an
explanation of the method’s preconditions, if it has any– Preconditions: the limitations a
particular method imposes on its inputs– If a method is called with arguments
that do not match its preconditions, its behavior is considered to be undefined
– These are very useful for proofs also!
![Page 34: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/34.jpg)
Documentation• As there exists a notion of preconditions, there also exist postconditions– Postconditions: the effect a method has
on its inputs (any unaffected/unlisted input should remain untouched), any generated exceptions, information about the return value, and effects on object state
– These are also very helpful for proofs!
![Page 35: Object Orientation](https://reader036.fdocuments.net/reader036/viewer/2022062520/56815e6a550346895dcce5df/html5/thumbnails/35.jpg)
Benefits• Documentation helps other programmers
(or yourself at some later time) to understand the role of each accessible field and method for a given class
• Inside of code, documentation provides great reference material for future maintenance efforts
• It can serve as a starting point for project report, but should not have everything a project report has