software engineering
TRANSCRIPT
![Page 1: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/1.jpg)
Software EngineeringSoftware Engineering
Dr Ian Reid
B4, 4 lectures, Hilary Term
http://www.robots.ox.ac.uk/~ian/Teaching/SoftEng
![Page 2: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/2.jpg)
Software Engineering vs structured programmingSoftware Engineering vs structured programming
• Not really a course about software engineering…
1. Software engineering– Mostly about concepts,
2. Structured programming– Revision, coding in C and Matlab, functions
3. Data structures– structures, classes
4. Object oriented programming– objects, object-oriented concepts like inheritance, polymorphism,
patterns and the standard template library
![Page 3: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/3.jpg)
Learning OutcomesLearning Outcomes
• The course will aim to give a good understanding of basic design methods, and emphasize the need to produce well-structured maintainable computer software. The course will concentrate on principles, but these will be reinforced with examples in Matlab and C/C++ programming languages. Specifically, by the end of the course students should:– understand concepts of basic program design techniques that can
be applied to a variety of programming languages, in particular Matlab and C/C++
– understand the need for structured programming in software projects
– be able to recognise and to produce and/or maintain well structured programs
– have a basic understanding of the role of and advantages of object oriented design
![Page 4: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/4.jpg)
TextsTexts
• Sommerville, Software Engineering, Addison-Wesley (8th edition), 2007.
• Wirth, Algorithms + Data Structures = Programs, Prentice-Hall, 1975
• Leveson, Safeware: System Safety and Computers, Addison-Wesley, 1995.
• Lipmann and Lajoie, C++ Primer, Addison-Wesley, 2005.• Goodrich et al., Data structures and algorithms in C++,
Wiley, 2004
![Page 5: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/5.jpg)
The Role of Computing in EngineeringThe Role of Computing in Engineering
• Computing is ubiquitous in engineering. Why?
• Awesome speed of modern, everyday computers a makes complicated analysis and simulation possible across all domains.
• Applications in design and modelling. Far beyond the reach of the mortal human engineer. Indeed many modelling problems are utterly infeasible without modern computers and software.
• In embedded systems, computers can provide a level of power, speed, flexibility and control not otherwise possible (eg mobile phone)
• Computing is “cheap” (but exercise this argument with care)
• Software is the key…
some examples…
![Page 6: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/6.jpg)
Example: mobile phoneExample: mobile phone
• Even simple mobile phones rely on software
• Typical phone has a microcontroller (SIM card) with a small program– Drive GUI– Control devices (keypad,
microphone, a/d, dsp, decoder)
![Page 7: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/7.jpg)
Example: Sizewell BExample: Sizewell B
• Nuclear power station (PWR), onstream in 1995• Software used extensively in the design• Software for control!
– first UK reactor to use software in its Primary Protection System)
![Page 8: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/8.jpg)
Example: A380Example: A380
• A380• 1400 separate
programs• There is a
software project just to manage all the software!
• Clearly safety-critical features of the software
![Page 9: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/9.jpg)
Example: NPfITExample: NPfIT
• NHS National Plan for IT• Plan to provide electronic care records for patients• Connect 30000 GPs and 300 hospitals• Provide secure access to records for healthcare
professionals• Provide access for patients to their own records via
“Healthspace”
![Page 10: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/10.jpg)
Software engineering versus programmingSoftware engineering versus programming
• Software engineering is about more than just programming/coding
• It is about design principles and methodologies that yield programs that are– Robust – Manageable– Reusable
![Page 11: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/11.jpg)
Software vs “other” engineeringSoftware vs “other” engineering
• How is software engineering similar to other engineering?
• Abstraction and Modularity– Consider free-body diagram– Thevenin/Norton– Low output impedance / High input impedance– Digital computer
• We return to these concepts later…
![Page 12: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/12.jpg)
Abstraction: free-body diagramAbstraction: free-body diagram
![Page 13: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/13.jpg)
Modularity: Op-amp bufferModularity: Op-amp buffer
• Unity gain buffer• Vout = Vin• Very high input impedance, very low output impedance
+
-
InOut
![Page 14: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/14.jpg)
Software vs “other” engineeringSoftware vs “other” engineering
• How is software different to other engineering?• Pure, weightless, flexible• Capacity to incorporate massive complexity • No manufacturing defects, corrosion, aging
![Page 15: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/15.jpg)
Intrinsic difficulties with softwareIntrinsic difficulties with software
• Analogue versus discrete state systems• The “curse” of flexibility
– Can encourage unnecessary complexity– Redefinition of tasks late in development – shifting goal-post
• Complexity and invisible interfaces– Standard way of dealing with complexity is via modularity– But this alone is not enough because interfaces can be subtle
and invisible, and here too there is a need to control complexity
• Historical usage information– Unlike physical systems, there is a limited amount of experience
about standard designs
![Page 16: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/16.jpg)
When software projects go wrongWhen software projects go wrong
• A320, Habsheim and Strasbourg
![Page 17: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/17.jpg)
When software projects go wrongWhen software projects go wrong
• London Ambulance Service– 1992, computerised ambulance despatch system fails
• Therac-25– 2 people died and several others exposed to dangerous levels of
radiation because of software flaws in radiotherapy device
• OSIRIS– £5M University financial package– Expenditure to date more like £20-25M
• NPfIT?– NHS £12 billion IT project
• comp.risks is a great source of others...
![Page 18: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/18.jpg)
NHS National programme for IT: NPfITNHS National programme for IT: NPfIT
• Plan to provide electronic care records for patients• Connect 30000 GPs and 300 hospitals• Provide secure access to records for healthcare
professionals• Provide access for patients to their own records via
“Healthspace”
• Laudable?• Realistic?
– Software Engineering specialists have their doubts– Ross Anderson (Prof of Security Engineering, Cambridge
Computing Laboratory) wrtes in his blog “I fear the whole project will just continue on its slow slide towards becoming the biggest IT disaster ever”.
![Page 19: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/19.jpg)
Software life-cycleSoftware life-cycle
• Software development stages– Specification– Design– Implementation– Integration– Validation– Operation/Maintenance/Evolution
• Different types of system organise these generic activities in different ways
• Waterfall approach treats them as distinct stages to be signed off chronologically
• In practice usually an iteration of various steps
![Page 20: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/20.jpg)
RequirementsRequirements
• Vague initial goals• Iterative refinement• Leading to more precise specification• Example
– Calculate the n-bounce trajectory of a lossy bouncing ball.– Refine this to consider
• What does the statement actually mean?• Physics• Initial conditions• Air-resistance?• Stopping criterion (criteria)?
– Now, think about how to design/implement
![Page 21: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/21.jpg)
Validation/VerificationValidation/Verification
• Verification: does the system confirm to spec?• Validation: does it actually do what it was supposed to?• Top-down vs bottom-up testing• Black-box vs white-box testing• Impossibility of exhaustive testing
![Page 22: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/22.jpg)
Extreme programming (XP)Extreme programming (XP)
• Proposed in the late 90s as a reaction to problems with “traditional” development processes
• Takes extreme position compared with waterfall approach
• Appropriate for small-medium sized projects– Teams of pairs of programmer, programming together– Incremental development, frequent system releases– Code constantly refined, improved, made as simple as possible– Do not design for change; instead change reactively
![Page 23: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/23.jpg)
Top down designTop down design
• Here want to keep in mind the general principles– Abstraction– Modularity
• Architectural design: identifying the building blocks• Abstract specification: describe the data/functions and
their constraints• Interfaces: define how the modules fit together• Component design: recursively design each block
![Page 24: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/24.jpg)
Modular designModular design
• Procedural programming: focus on algorithms• Object-oriented programming: focus on data structures
Algorithms Data structures
Programs
![Page 25: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/25.jpg)
Structured programmingStructured programming
• Top-down vs bottom-up• Both are useful as a means to understand the relations
between high-level and low-level views of a program• Top-down
– Code high level parts using “stubs” with assumed functionality for low-level dependencies
– Iteratively descend to lower-level modules
• Bottom-up– Code and test each low-level component– Need “test harness” so that low-level can be tested in its correct
context– Integrate components
• Not hard-fast rules; combination often best
![Page 26: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/26.jpg)
Simple design toolsSimple design tools
• Flow chart • Pseudo-code
– Wait for alarm– Count = 1– While (not ready to get up
and count <= 3)• Hit snooze button• Increment count
– Climb out of bed
![Page 27: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/27.jpg)
Data flowsData flows
• Data flow diagram• Simple example, VTOL simulator
Controller
Simulator
Display
state
state thrust
![Page 28: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/28.jpg)
Simple design toolsSimple design tools
• State diagram
![Page 29: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/29.jpg)
Basic coding techniquesBasic coding techniques
• Pretty much any program can be specified using:– Sequences of instructions
• { Do A; Do B; Do C }– Conditional instructions
• If (condition) Do A– Repetitions (loops)
• While (condition) Do A
• These semantic concepts are implemented in different high-level programming languages using different syntax
![Page 30: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/30.jpg)
Implementation in Matlab and CImplementation in Matlab and C
N= 10;tot = 0;totsq = 0;
for i=1:N tot = tot+i; totsq = totsq+i^2;
end
tottotsq
int i;int tot = 0;int totsq = 0;
for (i=1; i<N; i++) { tot += i; totsq += i*i;}
cout << tot << endl;cout << totsq << endl;
![Page 31: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/31.jpg)
Notes on coding styleNotes on coding style
• Use meaningful variable names• Use comments to supplement the meaning• Indent code for each block/loop• Encapsulate groups of statements sensibly in functions• Encapsulate related data sensibly in data structures• Design top down• Code bottom-up or top-down, or a combination
![Page 32: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/32.jpg)
Matlab vs CMatlab vs C
• Matlab and C are both procedural languages• Matlab is an interpreted language
– each statement decoded and executed in turn
• C is a compiled language– each module (.c file) is converted into assembly language– The interfaces between the modules are
• Shared global data• Function calls from one module to another
– This is resolved at link time when the modules are linked together into an executable
![Page 33: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/33.jpg)
Procedural programmingProcedural programming
• Aim is to break program down into functional units– procedures or functions– Set of inputs, set of outputs
• In Matlab and C this procedural building block is the function
• Understanding functions…
![Page 34: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/34.jpg)
Organisation of Matlab programsOrganisation of Matlab programs
• A Matlab “program” may be a script or function– i.e. a sequence of instructions
• This script or function will typically call a bunch of other functions
• Functions are stored in .m files • Multiple functions can be stored in one .m file, but only
first is visible outside– The others are local functions – Part of the recursive subdivision of the problem
![Page 35: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/35.jpg)
Matlab file organisationMatlab file organisation
FUNC foo bar
FUNC.m foo.m bar.m
![Page 36: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/36.jpg)
Organisation of C programsOrganisation of C programs
Source code
.c .cc
Object file
.o
compilation
Source code
.c .cc
Object file
.o
compilation
…..
…..
linking
executable
![Page 37: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/37.jpg)
FunctionsFunctions
• Function definition• Function call• Function prototype• Scope (local versus global data)• Parameters and return value(s)• Function call• Low-level implementation of function calls• Recursion
![Page 38: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/38.jpg)
Function definitionFunction definition
% compute factorial
function z = fact(n)
% function body
z = 1;
for i=1:n
z = z*i;
end
// compute factorial
int fact(int n)
{
int i, val = 1;
for (i=1; i<=n; i++) {
val *= i;
}
return val;
}
![Page 39: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/39.jpg)
Function callFunction call
• Distinguish between– The function definition
• Defines the set of operations that will be executed when the function is called
• The inputs• The outputs
– And the function call• i.e. actually using the function
• Formal vs Actual parameters
• Return value(s)– The value of a function evaluation is the return value
fact(10)
a = 6;z = fact(a);
[V,D] = eig(A);
![Page 40: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/40.jpg)
Function prototypeFunction prototype• The function prototype provides enough information to
the compiler so that it can check that it is being called correctly
• Defines the interface– Input (parameter), output (return value)
myexp.c file
float myexp(float x){ const float precision = 1.0e-6; float term=1.0, res=0.0; int i=0; while (fabs(term)>precision) { res += term; i++; term = pow(x,i)/fact(i); } return res;}
myexp.h file
float myexp(float x);
![Page 41: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/41.jpg)
Scope: local variablesScope: local variables
• Variables which are declared inside a function are local variables
• They cannot be “seen” outside the function (block) in which they are declared
• A local variable exists only for the duration of the current function execution
• It is declared as a new variable every time the function is called
• It ceases to exist when the function returns• It does not “remember” its value between calls
![Page 42: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/42.jpg)
Scope: global variablesScope: global variables
• Global variables exist outside all functions• A global variable is visible inside functions• If there exist two variables, one local, one global, with
the same name, then the local one takes precedence within its local scope
• C and Matlab behave differently– C will use a global if no local exists– Matlab only uses a global if the programmer explicitly requests it
• Globals should be used with caution because their use inside a function compromises its encapsulation
![Page 43: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/43.jpg)
EncapsulationEncapsulation
• Want the function to behave in the same way for the same inputs– encapsulate particular functional relationship
• But if the function depends on a global it could behave differently for the same inputs
• Live example using myexp
![Page 44: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/44.jpg)
Function encapsulationFunction encapsulation
Input parameters Output values
Hidden input
Input parameters Output values
Hidden output
![Page 45: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/45.jpg)
Side-effectsSide-effects
• Could set value of a global variable in a function• Again this compromises the function’s encapsulation
– Causes a side-effect– An implicit output, not captured by the interface
• Makes it difficult to re-use code with confidence• c.f. C and Matlab function libraries
– Set of re-usable routines with well defined interfaces
• In small projects maybe not a big problem• Hugely problematic in bigger projects, especially when
multiple programmers working as a team• Complicates interfaces between components, possibly in
unintended ways
![Page 46: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/46.jpg)
Low-level implementation of function callLow-level implementation of function call
Memory
CODE
DATA
machine code
global variables
STACK
local variable m
local variable 1return location
return value n
return value 1
parameter x
parameter 1
……
…Activation record
![Page 47: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/47.jpg)
Pass by value/referencePass by value/reference
int i=5, j=10;swap(i,j);cout << i << “ “ << j << endl;
Pass by value Pass by reference
void swap(int a, int b){
int temp = a;a = b;b = temp;return;
}
void swap(int& a, int& b){
int temp = a;a = b;b = temp;return;
}
![Page 48: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/48.jpg)
RecursionRecursion
• Recursion is the programming analogue of induction:
– If p(0) and p(n) implies p(n+1)– Then p(n) for all n
• Define a function in terms of– Itself– Boundary conditions
• For example– Factorial: n! = n * (n-1)!, 0! = 1
![Page 49: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/49.jpg)
Recursion example: factorialRecursion example: factorial
• Live demo
![Page 50: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/50.jpg)
Data types and data structuresData types and data structures
• C/C++ predefine a set of atomic types– bool, char, int, float, double
• C/C++ provides machanism for building compound data structures– struct (class)– Array
• Matlab supports arrays/matrices (of course)• Matlab also supports structures
![Page 51: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/51.jpg)
• A class (struct in C) is a compound data type which encapsulates related data into a single entity
class Complex {public:
double re, im;};
• Defines how a variable of this type will look
int i;Complex z;
C/C++: struct and classC/C++: struct and class
Class definition
Create a variable (an instance) of this type
![Page 52: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/52.jpg)
Example: VTOL stateExample: VTOL state
• Represent current state as, say, a triple of numbers and a bool, (position, velocity, mass, landed)
• Single variable represents all numbers– Better abstraction!
class State {double pos, vel, mass;bool landed;
};
State s;
Controller
Simulator
Display
state
state thrust
![Page 53: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/53.jpg)
Accessing class membersAccessing class members
State s;
s.pos = 1.0;s.vel = -20.0;s.mass = 1000.0;s.landed = false;
s.pos = s.pos + s.vel*deltat;
Thrust = ComputeThrust(s);
• In Matlab introduce structure fields without declaration
s.pos = 1.0;s.vel = -20.0;…
Thrust = ComputeThrust(s);
![Page 54: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/54.jpg)
Output parametersOutput parameters
Image ReadImage(const string filename, bool& flag);
bool ReadImage(const string filename, Image& im);
• Input: filename (type string)• Output:
– im (type Image) – boolean flag indicating success/failure
function [Image, errflag] = ReadImage(filename)
• Basically the same, but cleaner in Matlab!
![Page 55: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/55.jpg)
ArraysArrays
• An array is a data structure containing a numbered (indexed) collection of items of a single data type
int a[10];res = a[0] + a[1] + a[2];
Complex z[20];State s[100];
for (t=1; t<100; t++) {s[t].pos = s[t-1].pos + s[t-1].vel + 0.5*g;s[t].vel = s[t-1].vel + g – GetThrust(s[t-1], burnrate)/s[t-1].mass;
s[t].mass = s[t-1].mass – burnrate*escapevel;}
![Page 56: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/56.jpg)
Multi-dimensional arraysMulti-dimensional arrays
double d[10][5];
has elements:
d[0][0] d[0][1] … d[0][4]...d[9][0] d[9][1] … d[9][4]
![Page 57: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/57.jpg)
MethodsMethods
• In C++ a class encapsulates related data and functions
• A class has both data fields and functions that operate on the data
• A class member function is called a method in the object-oriented programming literature
![Page 58: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/58.jpg)
ExampleExample
class Complex {public:
double re, im;
double Mag() {return sqrt(re*re + im*im);
}
double Phase() {return atan2(im, re);
}};
Complex z;cout << “Magnitude=“ << z.Mag() << endl;
![Page 59: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/59.jpg)
ConstructorConstructor
• Whenever a variable is created (declared), memory space is allocated for it
• It might be initialised– int i;– int i=10;– int i(10);
• In general this is the work of a constructor
• The constructor is a special function with the same name as the class and no return type
– Complex(double x, double y) {{ re = x; im = y; }
![Page 60: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/60.jpg)
Information hiding / encapsulationInformation hiding / encapsulation
• Principle of encapsulation is that software components hide the internal details of their implementation
• In procedural programming, treat a function as black boxes with a well-defined interface – Need to avoid side-effects– Use these functions as building blocks to create programs
• In object-oriented programming, a class defines a black box data structure, which has – Public interface– Private data
• Other software components in the program can only access class through well-defined interface, minimising side-effects
![Page 61: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/61.jpg)
ExampleExample
class Complex {public:
Complex(double x, double y) { re=x; im=y; }double Re() { return re; }double Im() { return im; }double Mag() { return sqrt(re*re + im*im);}double Phase() { return atan2(im, re);
}
private:double re, im;
};
Complex z(10.0,8.0);cout << “Magnitude=“ << z.Mag() << endl;cout << “Real part=“ << z.Re() << endl;
![Page 62: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/62.jpg)
ExampleExampleclass Complex {
public:Complex(double x, double y) {
r = sqrt(x*x + y*y);theta = atan2(y,x);
}double Re() { return r*cos(theta); }double Im() { return r*sin(theta); }double Mag() { return r;}double Phase() { return theta; }
}
private:double r, theta;
};
Complex z(10.0,8.0);cout << “Magnitude=“ << z.Mag() << endl;cout << “Real part=“ << z.Re() << endl;
![Page 63: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/63.jpg)
C++ program organisationC++ program organisation
• Complex.h
class Complex {public:
Complex(double x, double y); double Re();
double Im(); double Mag();
double Phase();
private:double re, im;
};
![Page 64: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/64.jpg)
C++ program organisationC++ program organisation
• Complex.cpp
#include “Complex.h”
Complex::Complex(double x, double y) {re = x; im = y;
} double Complex::Re() { return re; }double Complex::Im() { return im; }double Complex::Mag() {
return sqrt(re*re+im*im); }double Complex::Phase() { return atan2(im,re); }
![Page 65: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/65.jpg)
Object-oriented programmingObject-oriented programming
• An object in a programming context is an instance of a class
• Object-oriented programming concerns itself primarily with the design of classes and the interfaces between these classes
• The design stage breaks the problem down into classes and their interfaces
• OOP also includes two important ideas concerned with hierarchies of objects– Inheritance– polymorphism
![Page 66: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/66.jpg)
InheritanceInheritance
• Hierarchical relationships often arise between classes• Object-oriented design supports this through inheritance
• An derived class is one that has the functionality of its “parent” class but with some extra data or methods
• In C++class A : public B {…};
![Page 67: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/67.jpg)
ExampleExample
class Window
Data: width, height posx, posy
Methods: raise(), hide() select(), iconify()
class TextWindow
Data: cursor_x, cursor_y
Methods: redraw(), clear() backspace(), delete()
class GraphicsWindow
Data: background_colour Methods: redraw(), clear() fill()
class InteractiveGraphicsWindow
Data: Methods: MouseClick(), MouseDrag()
![Page 68: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/68.jpg)
PolymorphismPolymorphism
• Polymorphism, Greek for “many forms”• One of the most powerful object-oriented concepts• Ability to hide alternative implementations behind a
common interface• Ability of objects of different types to respond in different
ways to a similar event• Example
– TextWindow and GraphicsWindow, redraw()
![Page 69: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/69.jpg)
ImplementationImplementation
• In C++ run-time polymorphism implemented via virtual functions
class Window {
…virtual void redraw();
};
![Page 70: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/70.jpg)
ExampleExample
• Class A is base class, B and C both inherit from A• If the object is of type A then call A’s func()• If the object is of type B then call B’s func()• If the object is of type C then call C’s func()
• If class A defines func() asvirtual void func() = 0;then A has no implementation of func()
• class A is then an abstract base class – It is not possible to create an instance of class A, only instances derived
classes, B and C– class A defines an interface to which all derived classes must conform
• Use this idea in designing program components– Specify interface, then have a guarantee of compatibility of all derived
objects
![Page 71: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/71.jpg)
Another exampleAnother example
• Consider a vector graphics drawing package• Consider base class “Drawable”
– A graphics object that knows how to draw itself on the screen– Class hierarchy may comprise lines, curves, points, images, etc
• Program keeps a list of objects that have been created and on redraw, displays them one by one
• This is implemented easily by a loop for (int i=0; i<N; i++) {
obj[i]->Draw();}
![Page 72: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/72.jpg)
TemplatesTemplates
• Templating is a mechanism in C++ to create classes in which one or more types are parameterised
• Example of compile-time polymnorphism
class BoundedArray {public:
float GetElement(int i) { if (i<0 || i>=10) {
cerr << “Access out of bounds\n”;return 0.0;
} else {return a[i];
}}
private:float a[10];
};
![Page 73: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/73.jpg)
TemplatesTemplates
template <class Type>class BoundedArray {
public:Type GetElement(int i) {
if (i<0 || i>=10) {cerr << “Access out of bounds\n”;return Type(0);
} else {return a[i];
}}
private:Type a[10];
};
BoundedArray<int> x;BoundedArray<Complex> z;
![Page 74: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/74.jpg)
Design patternsDesign patterns
• Programs regularly employ similar design solutions• Idea is to standardise the way these are implemented
– Code re-use– Increased reliability– Fewer errors, shorter development time
• An array is special case of a container type– Way of storing a collection of possibly ordered elements.– List, stack, queue, double-ended list, etc
• Templates in C++ offer a way of providing libraries to implement these standard containers
![Page 75: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/75.jpg)
Standard Template LibraryStandard Template Library
• C++ provides a set of container classes– Standard way of representing and manipulating container types
– eg, methods insert(), append(), size(), etc
• STL supports– Stack (FILO structure)– List (efficient insertion and deletion, ordered but not indexed)– Vector (extendible array)– others
![Page 76: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/76.jpg)
STL exampleSTL example
• std::vector<Type> is an extendible array• It can increase its size as the program needs it to• It can be accessed like an ordinary array (eg v[2])• It can report its current size
– v.size()
• You can add an item to the end without needing to know how big it is– v.push_back(x) #include<vector>
int main() {
std::vector<int> v;
for (int i=0; i<20; i++) v.push_back(i);
for (int i=0; i<v.size(); i++)
std::cout << v[i] << std::endl;
}
![Page 77: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/77.jpg)
STL, continuedSTL, continued
• To create a new STL vector of a size specified at run-time
int size;std::vector<Complex> z;
std::cin >> size;z.resize(size);
z[5] = Complex(2.0,3.0);
![Page 78: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/78.jpg)
STL, continuedSTL, continued
• To create a two dimensional array at run-time
int width, height;std::vector< std::vector<int> > x;
x.resisze(height);for (int i=0; i<height; i++)x[i].resize(width);
x[2][3] = 10;…
![Page 79: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/79.jpg)
IteratorsIterators• A standard thing to want to do with a collection of data
elements is to iterate over each– for (int i=0; i<v.size(); i++)
• Not all container types support indexing– A linked list has order, but only relative order
• An iterator is a class that supports the standard programming pattern of iterating over a container type
std::vector<int> v;std::vector<int>::iterator i;for (it=v.begin(); it!=v.end(); it++) …
• An iterator encapsulates the internal structure of how the iteration occurs
![Page 80: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/80.jpg)
Complete exampleComplete example
• Design a program to compute a maze– User-specified size – Print it out at the end
• Algorithm– Mark all cells unvisited– Choose a start cell– While current cell has unvisited neighbours
• Choose one at random• Break wall between it and current cell• Recursively enter the chosen cell
![Page 81: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/81.jpg)
Design data structuresDesign data structures
• Maze class– Compute method– Print method– Two dimensional array of Cells
• Cell class– Accessor methods– Break wall methods– Wall flags– Visited flag
![Page 82: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/82.jpg)
Cell class interfaceCell class interface
class Cell { public: Cell();
bool Visited(); void MarkVisited(); bool BottomWall(); bool RightWall(); void BreakBottom(); void BreakRight();
private: bool bottomwall; bool rightwall; bool visited;
};
![Page 83: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/83.jpg)
Maze class interfaceMaze class interface
class Maze {public:
Maze(int width, int height);void Compute(int x, int y);void Print();
private:
int Rand(int n);int H, W;std::vector< std::vector<Cell> > cells;
};
![Page 84: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/84.jpg)
Main programMain program
int main(int argc, char* argv[]){
int width, height;
cerr << "Enter maze width: ";cin >> width;cerr << "Enter maze height: ";cin >> height;
Maze m(width, height);m.Compute(height-1,0);m.Print();
return 0;}
![Page 85: Software engineering](https://reader033.vdocuments.mx/reader033/viewer/2022060108/5550f426b4c905417d8b55e8/html5/thumbnails/85.jpg)
Concept summaryConcept summary
• Top-down design– Abstraction– Encapsulation / information hiding– Modularity
• Functions• Classes / objects• Inheritance• Polymorphism• Templates• Patterns
• Exam questions? See tute sheet.