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

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Software Testing and Quality Assurance
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Fall 2007CS 2251 Software Engineering Intro. Fall 2007CS 2252 Topics Software challenge Life-cycle models Design Issues Documentation Abstraction.
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.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Modules, Hierarchy Charts, and Documentation
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Software.
Object Oriented Software Development
1 Shawlands Academy Higher Computing Software Development Unit.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
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.
Object Orientation “Thinking” Object Oriented, Further Constraints, and Documentation.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
SE: CHAPTER 7 Writing The Program
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
CPS120: Introduction to Computer Science Lecture 14 Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Object Orientation A Crash Course Intro. What is an Object? An object, in the context of object- oriented programming, is the association of a state with.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
The Software Development Process
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
1 CS161 Introduction to Computer Science Topic #9.
CS 11 C++ track: lecture 1 Administrivia Need a CS cluster account sysadmin/account_request.cgi Need to know UNIX (Linux)
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Object-Oriented Design Concepts University of Sunderland.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
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.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Algorithms and Problem Solving
User-Written Functions
Friend functions, operator overloading
Review What is an object? What is a class?
Functions CIS 40 – Introduction to Programming in Python
C Basics.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Building Java Programs
Coding Concepts (Basics)
Java Programming Language
Chapter 9 Introduction To Classes
Object Oriented Programming
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); }

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

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

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

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

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

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

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

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.

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

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.

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!

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!

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