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.
2. 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.
3. 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.
4. 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.
5. 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.
6. 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.
7. 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;
}
}
8. 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();
}
9. 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;
10. 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.
11. 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.
12. 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!";
}
13. 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();
}
14. 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";
}
}
15. 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++
16. 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.
17. 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.
18. Interfaces in C++
class InterfaceClass {
private:
public:
virtual void my_method() const = 0;
};
class SecondInterface {
private:
public:
virtual void my_second_method() const = 0;
};
19. 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;
}
20. 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;
}
21. 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.
22. 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
23. 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.
24. 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
25. 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++.