Download - CPP19 - Revision

Transcript
Page 1: CPP19 - Revision

RevisionMichael Heron

Page 2: CPP19 - Revision

Introduction• In today’s lecture we are going to round off our discussion of

programming with a revision of topics.• The theoretical side of these at least.

• This relates to the examination portion of the module.• Important to understand the concepts for this.• Code of secondary importance.

Page 3: CPP19 - Revision

What Is A Program?• A program is a series of statements to the computer.• Computers are stupid• Humans are clever• Need to ‘talk down’ to the computer

• Process of arriving at a working program very complicated.

• Involves the interaction of many skills.

Page 4: CPP19 - Revision

Representing Information• Information in a computer program is represented by

variables.• These are stored in the computer’s memory.

• Variables stand in place of literal values.• We don’t know what they will be when we run the program.

• Used to deal with ambiguity.• Can be many different types.

Page 5: CPP19 - Revision

Flow of Execution• All programs have a flow of execution.• This determines in what order the code statements are executed.

• By default, flow of execution is sequential.• Statements are executed one after the other.

• We have access to many flow control operations to change that.• These permit us to change the order in which code is executed.

Page 6: CPP19 - Revision

Flow of Execution• Repetition structures are used to repeat sections of code.• They fall into two categories.• Unbounded loops, when we don’t know how many times to

iterate.• Bounded loops, when we do.

• For loops are bounded loops.• While loops are unbounded loops.• Also exist a do-while loop, with more situational use.

Page 7: CPP19 - Revision

Flow of Execution• Selection structures allow us to choose between different

paths of execution.• If lets us provide code that might be executed if conditions are

met.• If-else lets us provide between two mutually exclusive course of

action.• If-Else if allows for more fine-grained control.

• Switch statement exists as a syntactic nicety.• It makes code more readable.

Page 8: CPP19 - Revision

Arrays• Representing data as single variables very limited.• Many real world situations require something more

comprehensive.• Arrays exist as a collection of related data.• A list of names, a list of ages, etc

• Arrays are syntatically amenable to manipulation with other structures.• For loops in particular.

Page 9: CPP19 - Revision

Arrays• Arrays serve as the basis for more complicated data structures.• They can be 1D, 2D, or as many dimensions as we like.

• Arrays are made up of elements which are identified by indices.• The number of indices is dependant on how many dimensions

the array has.• It’s like a variable with many different compartments.

Page 10: CPP19 - Revision

Functions• Incorporating all program code into a single main function is

very limited.• Hard to write• Hard to read• Hard to maintain

• Functions allow us to split up the functionality between smaller units.• Functions, or methods

• Same thing with different names.

Page 11: CPP19 - Revision

Functions• Functions are uniquely identified by their signatures.• Their name, and the order and type of their parameters.

• Parameters get sent into functions as a way of providing information.

• Functions can return a value to their calling function.• To give information back.

Page 12: CPP19 - Revision

Functions and Variables• Functions introduce a new issue with regards to variables.• That of Scope

• In a program, variables have one of three kinds of scope.• Local• Global• Class-wide

Page 13: CPP19 - Revision

Pointers• Variables represent an abstraction.• They are not the memory addresses, but the contents of the

memory addresses.• Pointers allow us to access memory locations directly.• Useful for several reasons.

• Works through the use of two operators• *, which is the dereference operator• &, which is the reference operator.

Page 14: CPP19 - Revision

Program Correctness• Most programs are not very correct.• They crash, or misbehave.

• It’s very hard to create correct computer programs.• Beyond the ability of Mortal Ken• This a direct result of the way digital data is represented.

• We can take a structured, systematic approach to this.• By creating and following a testing strategy

Page 15: CPP19 - Revision

Testing• Testing breaks down into two key families.• Black box testing, which tests only inputs and outputs.• White box testing, which tests only the flow of execution through

the program.• Testing based on the creation of test cases.• These stress ‘high risk’ parts of the system.

• A good testing strategy is one designed to uncover flaws.

Page 16: CPP19 - Revision

Debugging• Getting a program running is the easy thing.• Getting it working is more difficult.• Debugging is a complex task requiring patience and a

particular mindset.• It involves tracking down often complex misbehavior.

• It is a process intricately linked to programming.• But a separate and distinct step.

Page 17: CPP19 - Revision

Objects• C++ is an object oriented language.• This introduces new difficulties in development.

• Object oriented programming is built on two main structures.• The class, which is a blueprint• The object, which is a specific instance of a class.

• Classes define our structural side of the program.• Objects define our dynamic side.

Page 18: CPP19 - Revision

Objects and Classes• Classes sit idle until we create objects from them.• This process is called instantiation.

• The class defines the structure.• The attributes• The methods

• The object defines the state.• The value each of the attributes has.

Page 19: CPP19 - Revision

Encapsulation• Good object design is very difficult.• It takes years and years of practise and making mistakes.

• Some principles exist to aid in design.• Encapsulation is the principle of tying data and the methods

that act on that data together.• We can protect the delicate innards of an object using visibility

modifiers on the data.• Private, Public, Protected

• The set of public methods exposed defines the object’s interface.

Page 20: CPP19 - Revision

Inheritance• Inheritance is the technique of allowing one class to

incorporate methods and attributes defined in another.• The child class inherits the methods and attributes of the parent.

• Useful for many reasons.• Maintenance• Reusability• Cohesion of interface

Page 21: CPP19 - Revision

Object Design• Hard to assess a particular object hierarchy.• Some metrics exist

• Cohesion• Coupling• Impact of Change

• Important to create objects in the right way.• Black box design• Incorporate placeholders• Compile early and often

Page 22: CPP19 - Revision

File Handling• Input and Output in C++ is handled via streams for the most

part.• cout and cin are examples of streams.

• File I/O in C++ is handled as an extension of this idea.• Create an appropriate object• Manipulate it using << and >>• Close it when you’re done

Page 23: CPP19 - Revision

Stream I/O• Streams in C++ are very versatile.• They can be manipulated using stream manipulators.

• Techniques are shared between keyboard / monitor I/O and file I/O• What works for one will work for the other.

• This is powered by inheritance.• They all inherit from the same basic structure.

Page 24: CPP19 - Revision

Parsing• Most of the data you pull into a system will not be in a format

suitable for processing.• Necessary to parse data into a suitable format.

• Various parsing routines exist.• Tokenization• Object representation• Data conversion

• Usually necessary to ‘roll your own’• Data representation is too important to leave to ‘off the shelf’

solutions.

Page 25: CPP19 - Revision

Summary• Summarising a summary of the module is a crazy thing to do• So instead I will put some jokes.• Two fish are in a tank. One turns to the other and says ‘Do you

know how to drive this thing?’• The other says ‘My word! A talking fish!’

• Have you heard about the new pirate movie? It’s rated AaaaAaaaaAaarrrrRrr!


Top Related