2CPP19 - Summation

Post on 01-Nov-2014

56 views 0 download

Tags:

description

This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.

Transcript of 2CPP19 - Summation

SUMMATIONMichael Heron

Introduction• We have reached the end of our journey through object

orientation in C++• Huzzah, hooray, etc, etc

• As the last lecture on the topic, we are going to recap on all the significant theoretical concepts.• Not rehash, just recap.

• This knowledge will Guide You Well when it comes to the exam.• These are things you are expected to know!

The Principles of OO• OO is based primarily on three key principles.

• Inheritance• Encapsulation• Polymorphism.

• Together they form a mighty triad of techniques for building genuinely powerful programs.• These three together is why it is a fundamentally different style

from structured programming.

The Principles of OO• Object oriented structures are not inherently scalable.

• You need to be careful with what you do.

• Metrics such as coupling and cohesion give a view of the objective quality of a class hierarchy.

• Methods such as impact of change give a view of the maintainability of a system.

Why OO?• Why do we code using objects?

• Easier for humans to understand?• No.

• More efficient?• No

• More maintainable• Yes

• More expandable• Yes

• Reusable• Kinda

• Powerful?• Very much so.

Inheritance• The process of inheritance is to assume the methods and

attributes of another class.• Modified by the visibility of those methods and parameters.

• Java permits single inheritance only.• As does C#

• C++ permits multiple inheritance.• There be dragons. • Only one genuinely good thing comes from multiple inheritance in

C++

Encapsulation• Encapsulation is the process of bundling the attributes of

a class along with the methods that act upon those attributes.• Goes hand in hand with the topic of information hiding.

• Should ensure a separation of abstraction and implementation.• Access to functionality available only through predefined interfaces.

Polymorphism• The most abstract of the three.

• It’s the technique of treating a specialised class as an instance of a more general class.

• Significant differences between C++ and Java here.• All methods in java are inherently virtual.• In C++, method virtuality must be declared as part of the class

definition.

• Only virtual methods will be called as the most specialised version.

Overloading and Overriding• Object orientation is about providing a consistent interface

to an object.• There are various techniques that allow us to do this.

• Three key ways in which this is done in C++• Method overloading• Method overriding• Operator overloading

• The last is not available in Java• Which is a good thing.

Method Overloading• Process used to permit multiple interfaces to a single

method.• Don’t need to learn two sets of methods• One method with two sets of parameters.

• Reduces the cognitive burden on using an object.• Ensures consistency across an interface.• Can be overdone.

Method Overriding• Method Overriding is the process of providing a

specialised implementation of a single method.• Incorporated strongly into polymorphism and inheritance.

• Works through the use of virtual methods in C++• In Java, all methods are implicitly virtual.

Operator Overloading• C++ permits for operators to be overloaded.

• Change the way the basic + and – operators work on objects.• Leads easily to code obfuscation.

• Need to understand not only the objects, but how and where they can be applied to base operators.

• Handled using overridden methods in Java.• A more elegant approach for a more civilized age.

Abstraction• Abstraction is a key element in programming.

• It’s the process of getting rid of the low level details to focus on the high level interactions.

• Is both conceptual and technical as a term.• Abstraction is a general process• Abstract classes are a specific kind of abstraction in object

oriented programs.

• Understanding the flow of execution through a class hierarchy requires understanding of abstraction.

Abstract Classes• Abstract classes cannot be instantiated.

• They can only serve as the basis for other derived classes.• They can enforce a polymorphic contract with the compiler.

• A class in Java is made abstract via a special keyword.• A class in C++ is made abstract by the inclusion of a pure

virtual method

Pure Virtual Methods• A virtual method in C++ may be over-riden if the

developer desires.• A pure virtual method must be over-ridden.• Classes which incorporate no code and only pure virtual

methods can be used as interfaces.• Java has a special keyword for this too.

• Only good use of multiple inheritance.

Templates• Abstraction as a concept leads into the concrete

implementation of templates.• Boilerplate code• Code is generated by the compiler based on typing information.

• Templates are a powerful tool• Used to good effect in the Standard Template Library.

• A library of C++ classes for everyone to use.

STL• The Standard Template Library contains implementations

of:• Sequential containers• Adapter containers• Associative containers

• Worthwhile exercise to write these structures from scratch.• Understanding gained by doing this.

• Worth using the STL structures for ‘live’ code.

Stream Based I/O• I/O in C++ based primarily on streams.

• Polymorphism allows for the same basic operators to work on file and keyboard/monitor I/O

• I/O operations quite flexible.• You can modify the presentation quite a bit.

• However, object representation in files remains complex.• Serialization is the process used, and not natively supported in C+

+.

Static• Static methods in object oriented languages are class

methods.• They belong to a class, not to an instance.

• Static attributes in object oriented languages are class attributes.• All objects share the same data field for this.

• Static methods are limited.• Can only call on other static methods or attributes.

Const• The const modifier in C++ is used to specify different

behaviour depending on where it is used.• Can specify a constant value• Can specific an unchangeable value• Can specify a method that cannot change instance attributes.

• Indiscriminate use of const usually a sign of bad design.

Moving On• Where do you go… from here?

• Anywhere you like.

• The knowledge of C++ you have gained during this module is transferable.• You’ll find related concepts in any real OO language.

• We have spoken quite a bit about how the concepts relate to Java.• They relate just as well in C#

But… why?• All of these concepts are complicated by the nature of

pointers.• Pointers are the secret engine behind C++

• C++ is a complicated language because it layers pointer troubles on top of conceptual troubles.

• Why do this module in C++?• Several reasons.

C++ in Industry• C++ remains one of the most popular languages in

industry.• It’s not the most popular, but you’ll encounter it often in Real Life.

• People who can code in C++ or C are a dying breed.• ‘Too complicated’

• However, you learn things in C++ you don’t in other languages.

C++ The Language• Even if you never create another pointer, simply

understanding how they work opens up a world that other languages hide.• C++ more than any other language requires you to understand the

implications of what you are doing.

• This is an important mental skill.• It’s not just about the code.

• It’s about the concepts.

Memory Management• C++ has explicit memory management.

• No inherent, automated • garbage collection as in Java

• We must manually handler pointers and dynamic memory ourselves.• This has implications for our design.

• Copy constructors• Overloaded assignment operators• Destructors

• Learning to do this is a good mental exercise.• Albeit frustrating.

Next Week• Next week is your consolidation week.

• Use the time wisely, young padawans. Padawen? Padawii?

• The lab will be staffed as always.• Only two contact hours for the scheduled lecture time.

• Lab prep on Monday.• Q&A about your current assessment.• Bring questions about the assessment if you have any.

• Drop in tutorial on Thursday• Come along if you have any questions relating to OO concepts.

• No planned content otherwise.

Summary• Our discussion of OO in C++ is at an end.

• Alas, alas

• Next week is the consolidation week.• Finish up what you’re working with.

• The week after you’ll be learning about data structures with Manuel.• These build on the concepts we have discussed over the past few

weeks.

• Have fun!• Snausages.