Lecture Notes 2 – Software engineering, C++ classes

CSS 501 – Data Structures and Object-Oriented Programming

 

Reading for this lecture:       Carrano, Chapter 1

 

To be covered in this lecture:

 

Software process

Let’s start by discussing some of the fundamental principles that are useful in building complex computer programs and that form the basis for software engineering. Software engineering is a branch of computer science that provides techniques to facilitate the development of computer programs (one textbook definition). It defines the process by which programs are designed, coded, tested, and maintained (among other steps). Rather than just starting coding at some arbitrary point, a problem solving approach is used for the developing programs to accomplish some goal.  One version of describing software life cycle:

 

·         Specification: input/output/error/special cases

·         Design: not implementation, break into smaller problems, algorithms and abstract data types (ADT) to solve the problem, pseudo code.

·         Risk Analysis: cost and schedule

·         Verification: Correctness of solution

·         Coding: implementation

·         Testing:

·         Refining:

·         Production:

·         Maintenance

 

I am not sure how valid is the above cycle. It is one version, important point is, all steps are valid (may not be well defined step-by-step). We are going to talk about: Specification, and Design this week. This class, focuses on good design and good coding.

 

Today

OO Concepts and better solution:

 

·         Slides: Here is the Powerpoint slides (should cover through ADT)

·         Source code Listings from the book.

 

 

Note that in all cases, documentation connects the different steps in software development, since it is a key aspect of all phases of the software life cycle. This is one of the most important aspects of software. For large programs, different people are often responsible for different parts of the life cycle and documentation is crucial to understanding the phases that other people have worked on. Even if you are the only person who will ever use a program (unlikely, except for trivial programs), when you come back to it a week, a month, or a year later, will you remember the specifications, the design details, or other steps? Document each phase so that you don’t need to!

 

Each function should have a set of pre-conditions specifying the conditions that must exist at the beginning of the function, and a set of post-conditions that specify the conditions after the function has completed. 

 

Example:

                                // sort:  function to sort an array of integers

                                // preconditions:  n is the number of values to sort and is no less than zero.

                                //                             array has been allocated and holds at least n integers.

                                // postconditions: the first n values of array are sorted into non-decreasing order.

                                //                             specifically array[i] <= array[i+1] for 0 <= i < n-1

                                void sort(int n, int array[])

 

This is the kind of documentation that I want to see in this class. Each of the function parameters (including return values) should be mentioned in documentation. Note that the preconditions and postconditions say nothing about how the sorting is achieved. For a complex function, you should also say something about how the function works and include comments inside the function for each code block.

 

 

C++ concepts

See the C++ notes linked on the course web site:

http://courses.washington.edu/css342/zander/css332/

In particular, the following pages:

·         C++ I/O

o    Learn cin/cout.

·         C++ functions and pass by value vs. pass by reference

o    Notice the "&"!!

o    Variables stored on the stack vs. the heap

o    Look back at Program0.cpp, the invertPixel() function

·         The Rational class header file

o    Syntax, case MATTERS!

o    include: <what> vs "what"?!

§  why the .h, do we need it?

o    public/private: can appear multiple times, keyword changes the scope

o    Pre-processor directive (all them #)

§  #ifndef/#define/#endif (#pragma once): to avoid cyclic inclusion

o    Secret? You can include code in .h file!

§  Rule of thumb: simple one-liner

·         The Rational class .cpp

o    Syntax: Constructor,

o    The scoping operator "::"

o    Let's draw a memory diagram for the Rational::add() function!

o    Did we talk about "const"?

·         Using Rational objects, sample driver

o    How objects are constructed

o    The lack of "new"!! Let's draw a memory diagram again!

o    Constructors: notice the variable w