Docsity

C++ Inheritance, Polymorphism, and Access Specifiers - UMich, Oct 1, 2007 - Prof. Sugih Ja, Study notes of Algorithms and Programming

Partial preview of the text

Download C++ Inheritance, Polymorphism, and Access Specifiers - UMich, Oct 1, 2007 - Prof. Sugih Ja and more Study notes Algorithms and Programming in PDF only on Docsity!

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

Code RCode

R

G

l f

ti^

lit

z^

G

eneral functionality ca applied to

any

derived

z^

Derived classes can haspecialization by addingspecialization by addingimplementing virtual fun

ReuseReuse

b^

itt

d

an be written once andd classave selectiveg members org members ornctions

Interfaces and IInterfaces

and I

Th

t^

thi

th

z^

Th

ere are two things th −^

A particular

interface

−^

A particular

implementa

ImplementationImplementation h t

b^

i h

it d

hat can be inherited:^ a

tion

Pure Virtual aPure

Virtual a

Addi

k^

f

z^

Addi

ng

makes a fun

−^

No implementation is gi −^

Must

be implemented b

z^

A

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

and Abstractand Abstract

ti^

i t

l

nction

pure

virtual

ivenby (concrete) derived classes1 pure virtual member isbe instantiatedbe instantiated

“i t

f^

”^

l

ure “interface” classes

Example: VirtuaExample:

Virtua

class Base {

cla

public:

void print() {

pu

void print() {

cout << “Base”; } }^

}^

l vs Non

Virtual

l^

vs. Non-Virtual

ass Derived: public Base {

p^

ublic:void print() {void print() {

cout << “Derived”; }

Virtual vs NVirtual

vs N

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

Non virtualNon-virtualterfacesterfacesave the functionality, so theon any instance of the baseon any instance of the basenly “suggestions”are onl

interfaces

are only interfaceshould be consideredtations for derivedr you get unintuitive behavior

BaseBase

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};

ClassClass ; "TodoList::add: not defined\n"; };"TodoList::add: not defined\n"; };ist::next: not defined\n"; };Li t t

t d fi

d\ " }

List::type: not defined\n"; };

When

Inheritance/P

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

to use

Polymorphism

s that share functionality?s that share functionality?ment identical/similarces?ces?hierarchy using “isa”l functionsl functionshe behavior of the class?

Multiple InMultiple

In

U

i^

l^

d^

t

z^

U

se sparingly and caut z^

The “deadly diamond”:

y

classint

f()f()

class Bc ass

f()

class

nheritancenheritance^ ti ???

l

tiously^ s^ A i;

class Cclass C

f()

s D

AccessAccess

P i

t

z^

P

rivate −^ Access only within the c z^

Public

Access from anywhere −^

Access

from anywhere

z^

Protected^ −

Access within this class

Why do we make

ControlControlclass s and any derived classesanything private?

Friends FunctioFriends

Functio

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 };

ns and Classesns

and Classes

b

e members on a case- dFunction();dClass;dClass;

Classes aClasses

a

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

nd Structsnd Structs

b t

l^

d

es between classes andate accessc access`e of inheritance?

Private ConstrucPrivate

Construc

C

h^

i^

t

z^

C

an you have private c destructors?

ctors/Destructorsctors/Destructors

t^

t^

d

constructors and