2cpp14 - abstraction

Post on 01-Nov-2014

57 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

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

ABSTRACTIONMichael Heron

Introduction• Abstraction is a process that is important to the creation of

computer programs.• Being able to view things at a higher level of connection than the

moving parts themselves.

• In this lecture we are going to talk about the nature of abstraction with regards to object orientation.• It has both a conceptual and technical meaning.

Designing Classes• How do classes interact in an object oriented program?

• Passing messages

• How do messages flow through a class architecture?• Uh…

• Need to understand how all the parts fit together in order to understand the whole.• Can’t skip this step and succeed.

Abstraction• Process of successively filtering out low level details.

• Replace with a high level view of system interactions.

• Helped by the use of diagrams and other notations.• UML and such

• Important skill in gaining big picture understanding of how classes interact.• Vital for applying Design Patterns and other such generalizations.

Abstraction in OO• Abstraction in OO occurs in two key locations.

• In encapsulated classes• In abstract classes

• Latter important for developing good object oriented structures with minimal side effects.• Sometimes classes simply should not be instantiated.• They exist to give form and structure, but have no sensible reason

to exist as objects in themselves.

Abstract Classes• In Java and C++, we can make use of abstract classes to

provide structure with no possibility of implementation.• These classes cannot be instantiated because they are incomplete

representations.

• These classes provide the necessary contract for C++ to make use of dynamic binding in a system.• Must have a derived class which implements all the functionality.

• Known as a concrete class.

Abstract Classes• In Java, abstract classes created using special syntax for

class declaration:

abstract class Student { private String matriculationCode; private String name; private String address;

public Student (String m, String n, String a) { matriculationCode = m; name = n; address = a; } public String getName() { return name; } public String getCode() { return matriculationCode; } public String getAddress() { return address; } }

Abstract Classes• Individual methods all possible to declare as abstract:

abstract class Student { private String matriculationCode; private String name; private String address;

public Student (String m, String n, String a) { matriculationCode = m; name = n; address = a; } public String getName() { return name; } public String getCode() { return matriculationCode; } public String getAddress() { return address; } abstract public int getLoan(); abstract public String getStatus(); }

Abstract Classes• In C++, classes are made abstract by creating a pure

virtual function.• Function has no implementation.

• Every concrete class must override all pure virtual functions.• Normal virtual gives the option of overriding• Pure virtual makes overriding mandatory.

virtual void draw() const = 0;

Abstract Classes• Any class in which a pure virtual is defined is an abstract

class.• Abstract classes can also contain concrete

implementations and data members.• Normal rules of invocation and access apply here.

• Can’t instantiate an abstract class…• … but can use it as the base-line for polymorphism.

Example• Think back to our chess scenario.

• Baseline Piece class• Defines a specialisation for each kind of piece.

• Defined a valid_move function in Piece.• We should never have a Piece object

• Define it as abstract• Every object must be a specialisation.

• Had a default method for valid_move• Define it as a pure virtual

• Every object must provide its own implementation.

Intent to Override• We must explicitly state our intent to over-ride in derived

classes.• In the class definition:

• void draw() const;

• In the code:

#include "ClassOne.h"#include <iostream>

using namespace std;

void ClassOne::draw() const { cout << "Class One Implementation!";}

Interfaces• Related idea

• Interfaces• Supported syntactically in Java• Must be done somewhat awkwardly in C++

• Interfaces in Java are a way to implement a flavour of multiple inheritance.• But only a flavour

interface LibraryAccess { public boolean canAccessLibrary(); }

Interfaces

public class FullTimeStudent extends Student implements LibraryAccess {

public FullTimeStudent (String m, String n, String a) { super (m, n, a); } public int getLoan() { return 1600; } public boolean canAccessLibrary() { return true; } public String getStatus() { return "Full Time"; } }

Interfaces• Interfaces permit objects to behave in a polymorphic

manner across multiple kinds of subclasses.• Both a Student and an instance of the LibraryAccess object.• Dynamic binding used to deal with this.

• Excellent way of ensuring cross-object compatibility of method calls and parameters.

• Not present syntactically in C++

Interfaces in C++• Interfaces defined in C++ using multiple inheritance.

• This is the only time it’s good!

• Define a pure abstract class• No implementations for anything• All methods pure virtual

• Inheritance multiple interfaces to give the same effect as a Java interface.

Interfaces in C++• Interfaces in C++ are not part of the language.

• They’re a convention we adopt as part of the code.

• Requires rigor to do properly• You can make a program worse by introducing multiple inheritance

without rigor.

• Interfaces are good• Use them when they make sense.

Interfaces in C++class InterfaceClass {private:public: virtual void my_method() const = 0;};

class SecondInterface {private:

public: virtual void my_second_method() const = 0;};

Interfaces in C++

#include "Interface.h"#include "SecondInterface.h”

class ClassOne : public InterfaceClass, public SecondInterface {public: void my_method() const; void my_second_method() const;};

#include "ClassOne.h"#include <iostream>

using namespace std;

void ClassOne::my_method() const { cout << "Class One Implementation!" << endl;}

void ClassOne::my_second_method() const { cout << "Class One Implementation of second method!" << endl;}

Interfaces in C++#include <iostream>#include "ClassOne.h"

using namespace std;

void do_thing (InterfaceClass *c) { c->my_method();}

void do_another_thing (SecondInterface *s) { s->my_second_method();}

int main() { ClassOne *ob; ob = new ClassOne(); do_thing (ob); do_another_thing (ob); return 1;}

Interfaces Versus Multiple Inheritance• Interfaces give a flavour of multiple inheritance

• Tastes like chicken

• They handle multiple inheritance in the simplest way• Separation of abstraction from implementation• Resolves most of the problems with multiple inheritance.

• No need for conflicting code and management of scope• Interfaces have no code to go with them.

Interfaces versus Abstract• Is there a meaningful difference?

• Not in C++

• In Java and C# can…• Extend one class• Implement many classes

• In C++• Can extend many classes• No baseline support for interfaces

Interfaces versus Abstract• Why use them?

• No code to carry around• Enforce polymorphic structure only

• Assumption in an abstract class is that all classes will derive from a common base.• Can be hard to implement into an existing class hierarchy.

• Interfaces slot in when needed

• Worth getting used to the distinction.• OO understand more portable.

More UML Syntax

Abstract class indicated by the use of italics in attribute and method names.

Use of interface indicated by two separate syntactical elements

1. Dotted line headed with an open arrow

2. Name of class enclosed in double angle brackets

Summary• Abstraction important in designing OO programs.• Abstract classes permit classes to exist with pure virtual

methods.• Must be overloaded

• Interfaces exist in C# and Java• Not present syntactically in C++

• Concept is transferable• Requires rigor of design in C++.

top related