06 COMP2150 OCCF Complete
06 COMP2150 OCCF Complete
OCCF
WINTER 2024
1
Outline
• What is OCCF?
2
Simplifying object interaction
• Simple, predictable interaction is one of the hallmarks of
OO
4
Orthodox Canonical Class Form
• The four things that (in most situations) every C++
class should have are:
• a null constructor → you already know this one
• a destructor
• a copy constructor
• an assignment operator
5
Orthodox Canonical Class Form
• These four items are relatively straightforward
6
Destructors
7
Destructors/Finalizers
• You're used to using constructors: methods to
perform tasks associated with the creation of an
object
• There are also destructors - methods that perform
tasks associated with the death of an object
• Neither constructors nor destructors actually create
or destroy - they just do housekeeping when those
events happen
• Destructors are not seen as often in Java as in C++,
because?
8
Destructors/Finalizers
• In C++, we need to release dynamically allocated
space ourselves, whereas Java manages this for us
9
Destructors in C++
• Syntax: ~ClassName()
10
Destructors
• In a hierarchy, when an object goes out of scope, its
destructor is called, followed by its parent's (and so
on) – this happens automatically, you don't need to
call the parent destructor yourself
11
Destructors
Class hierarchy Destructor call order
Grandparent ~Grandparent()
Parent ~Parent()
Child ~Child()
13
Virtual destructors
• If the appropriate subclass' destructor wasn't called,
then we could potentially have memory leaks or
other problems (anything missing from cleanup) –
we need the destructor to be polymorphic
14
Virtual destructors
• C++ reminds you of this by giving you a warning
message if you write a polymorphic (virtual) method
in a hierarchy and don't have a virtual destructor
15
In Java
• We have an analogous feature in Java, finalizers:
• void finalize(); //comes from Object class
16
Pass-by-value
vs Pass-by-reference
& Copy Constructors
17
Shallow vs deep copy
• There are two ways to do assignment when we are
working with pointers
• we can assign a pointer to point to the same thing
as something else:
• "shallow copy" - it's just copying the pointer
19
Example – stack based
Person q("Ryley");
Person p = q;
Person r(p);
20
Example – stack based
Person q("Ryley");
Person p = q;
Person r(p);
• The first is the only one that does not involve taking
data from another object
• The other two are all making copies of another
object, and this is where C++ would call your copy
constructor if you wrote one!
• default will exist if you don't write one; it just might
not do what you want
21
Copy Constructors in C++
• A copy constructor looks just like a constructor, but it
takes a reference parameter rather than a value
parameter
• First, recall what these are - a value parameter is
allocated when the routine is called, and receives a
copy of the argument from the call
• this is the only way of passing parameters in Java
• Whereas a reference parameter is a pointer
managed internally - it refers/points to the argument
from the caller (and thus allows changes to that
argument)
22
Reference parameters
• In C/C++, things are passed by value unless we say
otherwise
• Recall we have a & operator to obtain the address
of something
23
Reference parameters
• C++ will supply the address, not you
24
Reference parameters
void dostuff(int& x) {….}
• The parameter x is now a reference parameter
• Much like a Java reference variable
27
Why reference parameters?
• The danger is because they are changeable too, we'll
accidentally change it when we don't intend to. But
we can also ensure this doesn't happen:
28
More on const
• C++ also has a really nice safety measure – const can
also be put on a method name
• e.g., void doIt() const;
30
Copy constructors
• Now we can look at a copy constructor - it looks like
a constructor except it takes a reference parameter
to another object of the same type
31
Copy constructors
• Now we can look at a copy constructor - it looks like
a constructor except it takes a reference parameter
to another object of the same type
32
Copy constructors
• Now we can look at a copy constructor - it looks like
a constructor except it takes a reference parameter
to another object of the same type
33
Copy constructors
Book::Book (const Book& source) {
title = source.title;
author = source.author; }
34
Default copy constructors
• Does a recursive copy of fields
35
Default copy constructors
• Remember recursive does not mean deep - if I have a
stack-based Node in an object, a copy constructor
will copy that Node (via its copy constructor)
• if I have a pointer-to-node, the pointer will simply
be copied
36
Our copy constructor
• Will be called: whenever we make an object that
involves taking data from another object
• Person p = q;
38
In Java
• Object in java has a method named clone that you
can call via super (super.clone())
39
In Java
class MyClass implements Cloneable {…
public Object clone() throws CloneNotSupportedException {
Object newThing = super.clone();
return newThing;
}
}
• This is a shallow copy; to make it deep you need to
add your own code (i.e. make a new piece of
memory, copy this over deeply and then return it
rather than just calling super.clone() )
• Most of the time, super.clone() is not what you want,
especially if you want a deep copy
40
Assignment Operator
41
Assignment operator
• Assignment is another of the four items in the OCCF;
it's a fairly obvious one - so obvious, you just use =
and don't think about it…
• However, assignments can have pointer semantics
(i.e. make 2 things point to one another) or copy
semantics (copy the value from one to the other)
• For different types of objects, we may want to define
our own special mechanisms outside of what the
language would do by default
• How do we do this?
42
Assignment in C++
• Default behaviour for = on objects (not pointers) is to
copy fields recursively (i.e. exactly what the default
copy constructor does – i.e. not a deep copy)
43
Assignment in C++
• Assignment is actually a message sent to the object
on the left of the =, with the object on the right as an
argument
Person p, q;
p = q;
→ is a message sent to p asking it to “set itself using q”
44
Assignment in C++
46
Assignment in C++
MyObject& MyObject::operator = (MyObject& right) {
data = right.getData();
//copy over whatever else we want
return (*this);
}
47
Assignment in C++
MyObject& MyObject::operator = (MyObject& right) {
data = right.getData();
//copy over whatever else we want
return (*this);
}
48
Assignment in C++
MyObject& MyObject::operator = (const MyObject& right){
data = right.getData();
//copy over whatever else we want
return (*this);
}
49
In Java? Not possible directly
• Java does NOT let us change the behaviour of
operators ourselves
50
Orthodox Canonical Class Form
• Assignment, the null constructor, the copy
constructor, and the destructor collectively support
many other higher-level operations
51
Orthodox Canonical Class Form
• Therefore, they are the ones you should consider
first, even if it doesn't look like you'll need them
52
OCCF and course work
• Complete OCCF is NOT expected on A2, but you are
expected to write destructors to ensure memory is
properly returned
53
OCCF and course work
• In future assignments, as well as tests and exams, we
will operate under the policy of "do not write these
items unless you actually need them"
54