


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















Discussion slides for w
The University
week of October 1, 2007 y^ y^ of Michigany^
g
1
Agenda^ z
Inheritance / Polymorphism z Access: public
private
pro
z^
Access: public, private, pro z^
Circular dependencies and z^
Tree, Binary Trees, Binary S
m
review otected
friends
otected, friendsforward declarations
h
Search Trees
2
l f
ti^
lit
z^
eneral functionality ca applied to
any
derived
z^
Derived classes can haspecialization by addingspecialization by addingimplementing virtual fun
b^
itt
d
an be written once andd classave selectiveg members org members ornctions
Th
t^
thi
th
z^
Th
ere are two things th −^
A particular
interface
−^
A particular
implementa
b^
i h
it d
hat can be inherited:^ a
tion
Addi
k^
f
z^
Addi
ng
makes a fun
−^
No implementation is gi −^
Must
be implemented b
z^
ny class with at least abstract
and cannot b
abstract
, and cannot b
Oft
d t
d fi
z^
Oft
en used to define pu with no functionality
ti^
i t
l
nction
pure
virtual
ivenby (concrete) derived classes1 pure virtual member isbe instantiatedbe instantiated
“i t
f^
l
ure “interface” classes
class Base {
cla
public:
void print() {
pu
void print() {
cout << “Base”; } }^
ass Derived: public Base {
p^
ublic:void print() {void print() {
cout << “Derived”; }
Virtual functions are int z^
Virtual
functions are int
−^
All derived types will hafunction can be called ofunction can be called o −^
Implementations are on“P re”
irt al f nctions a
−^
“P
ure” virtual functions a
z^
Non-virtual functions shmandatory implemenlclasses^ −
Don’t override these, or
interfaces
are only interfaceshould be consideredtations for derivedr you get unintuitive behavior
class TodoList {public:public:virtual int isempty() { return(1); }virtual void add(T elt) { cerr << "virtual void add(T elt) { cerr << "virtual T next() { cerr << "TodoLi
i t
l i t t
() {
<< "T d L
virtual int type() { cerr << "TodoL};
t d fi
d\ " }
List::type: not defined\n"; };
Do you have types/classes z^
Do
you have types/classes
z^
Are you tempted to implemfunctionality in several placfunctionality in several plac z^
Can you define a natural hrelationships?relationships?Using templates vs virtual z^
Using
templates vs. virtual
z^
Does the type T change th^ −
If no, use a template − If yes, use virtual functions
s that share functionality?s that share functionality?ment identical/similarces?ces?hierarchy using “isa”l functionsl functionshe behavior of the class?
i^
l^
d^
t
z^
se sparingly and caut z^
The “deadly diamond”:
y
classint
f()f()
class Bc ass
f()
class
l
tiously^ s^ A i;
class Cclass C
f()
s D
P i
t
z^
rivate −^ Access only within the c z^
Public
Access from anywhere −^
Access
from anywhere
z^
Protected^ −
Access within this class
Why do we make
All
t^
i^
t
z^
All
ows access to privat by-case basis class myClass {class
myClass { int myPrivateVar;friend void myFriendfriend class myFrienfriend class myFrien };
b
e members on a case- dFunction();dClass;dClass;
Wh t
th
diff
z^
Wh
at are the difference structs in c++?^ −
Classes default to priva − Structs default to public z^
What about default type
b t
l^
d
es between classes andate accessc access`e of inheritance?
h^
i^
t
z^
an you have private c destructors?
t^
t^
d
constructors and