Object Orientation “Thinking” Object Oriented, Further Constraints, and Documentation.

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.
C Programming - Lecture 7 This lecture we will learn: –How to write documentation. Internal docs. External docs. User docs. (But not much about this).
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Software Testing and Quality Assurance
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
Fall 2007CS 2251 Software Engineering Intro. Fall 2007CS 2252 Topics Software challenge Life-cycle models Design Issues Documentation Abstraction.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
CS 201 Functions Debzani Deb.
Program Commenting CS-212 Dick Steflik. Commentary Commentary are pieces of information included in a program’s source files to provide additional information.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Modules, Hierarchy Charts, and Documentation
Guide To UNIX Using Linux Third Edition
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
Introduction to Methods
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Object Oriented Software Development
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Array.
Chapter 8 More Object Concepts
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
4.1 Instance Variables, Constructors, and Methods.
Interviewing the Internalized Other Part 2: applied in couple work Workshop for OAMFT in Toronto 2 November 2012 by Karl Tomm MD.
Documentation and Comments. What’s a comment? A comment is a simple form of documentation. Documentation is text that you the programmer write to explain.
PART 3. You have been using static methods in your programs before and methods are the building blocks of procedural programming. In this part of the.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
GEMVC. The Setup Folders Views Value Objects (VOs) Custom Events Service CFCs Controller Model Application Main MXML.
SE: CHAPTER 7 Writing The Program
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Object Orientation “Thinking” Object Oriented, Further Constraints, and Documentation.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Use Cases Use Cases are employed to describe the functionality or behavior of a system. Each use case describes a different capability that the system.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
The Software Development Process
Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
6. 1 Multiplication with Exponents and Scientific Notation 6
1 CS161 Introduction to Computer Science Topic #9.
Use Cases CS 6961 – Lecture 4 Nathan Dykman. Neumont UniversityCS Lecture 102 Administration Homework 1 is due –Still reviewing the proposal, but.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Software Engineering Requirements + Specifications.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Object-Oriented Design Concepts University of Sunderland.
Copyright © 2004, Keith D Swenson, All Rights Reserved. OASIS Asynchronous Service Access Protocol (ASAP) Tutorial Overview, OASIS ASAP TC May 4, 2004.
Copyright © Curt Hill Flow of Control A Quick Overview.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
The Object-Oriented Thought Process Chapter 03
Algorithms and Problem Solving
User-Written Functions
Concepts of Object Oriented Programming
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Functions CIS 40 – Introduction to Programming in Python
C Basics.
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Objects First with Java
Compound Statements A Quick Overview
Algorithms and Problem Solving
Java Programming Language
Presentation transcript:

Object Orientation “Thinking” Object Oriented, Further Constraints, and Documentation

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.

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.

Programming Paradigms Just as there are families of real- world spoken languages, there are multiple families – or paradigms – of programming languages.

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.

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.

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.

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.

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.

A Fraction Object class Fraction { private: int numerator; int denominator; public: Fraction add(Fraction &f); }

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); }

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() ?

f1.add(f2); //Both are Fractions This line is basically saying “Call the “ Fraction.add() ” method from the perspective of f1. Coding in OO

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.”

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.

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.

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 ?

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 “. ”.

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

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.

Questions?

For the rest of this lecture, we’ll switch gears to examining documentation and a bit more in regard to analysis.

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.

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.

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. */

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.

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.

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.

Benefits Documentation helps other programmers 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.