27
SUMMATION Michael Heron

2CPP19 - Summation

Embed Size (px)

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.

Citation preview

Page 1: 2CPP19 - Summation

SUMMATIONMichael Heron

Page 2: 2CPP19 - Summation

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!

Page 3: 2CPP19 - Summation

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.

Page 4: 2CPP19 - Summation

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.

Page 5: 2CPP19 - Summation

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.

Page 6: 2CPP19 - Summation

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

Page 7: 2CPP19 - Summation

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.

Page 8: 2CPP19 - Summation

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.

Page 9: 2CPP19 - Summation

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.

Page 10: 2CPP19 - Summation

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.

Page 11: 2CPP19 - Summation

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.

Page 12: 2CPP19 - Summation

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.

Page 13: 2CPP19 - Summation

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.

Page 14: 2CPP19 - Summation

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

Page 15: 2CPP19 - Summation

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.

Page 16: 2CPP19 - Summation

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.

Page 17: 2CPP19 - Summation

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.

Page 18: 2CPP19 - Summation

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+

+.

Page 19: 2CPP19 - Summation

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.

Page 20: 2CPP19 - Summation

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.

Page 21: 2CPP19 - Summation

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#

Page 22: 2CPP19 - Summation

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.

Page 23: 2CPP19 - Summation

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.

Page 24: 2CPP19 - Summation

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.

Page 25: 2CPP19 - Summation

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.

Page 26: 2CPP19 - Summation

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.

Page 27: 2CPP19 - Summation

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.