dis3

dis3 - CS32
Discussion
 Sec.on
1B
 Week
4
...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS32
Discussion
 Sec.on
1B
 Week
4
 TA:
Brian
Choi
 Inheritance
 •  The
process
of
deriving
a
new
class
using
another
 class
as
a
base.
 •  Our
example:
 Dog
 Features
of
a
dog
 Cat
 Features
of
a
cat
 •  But
there
might
be
some
common
features
in
the
 two…
 Inheritance
 •  The
process
of
deriving
a
new
class
using
another
 class
as
a
base.
 •  Our
example:
 Animal
 Implement
common
features
 of
all
animals
here
 derive Dog
 Implement
features
specific
to
 dogs
here
 derive Cat
 Implement
features
specific
to
 cats
here
 Deriving
a
class
from
another
 class Animal { public: Animal(); ~Animal(); int getAge() const; void setAge(int age); void speak() const; private: int m_age; }; base class •  Dog
inherits
Animal.
 class Dog : public Animal { public: Dog(); ~Dog(); string getName() const; void setName(string name); private: string m_name; }; derived class Deriving
a
class
from
another
 Dog d1; d1.setName(“puppy”); d1.setAge(1); Animal a1; a1.setAge(2); a1.setName(“abc”); Deriving
a
class
from
another
 •  What’s
inherited:
 –  all
member
func.ons
except
the
overloaded
assignment
 operator
(operator=),
constructors,
and
the
destructor
 –  all
member
variables
 •  However,
the
derived
class
cannot
access
the
private
 members
of
the
base
class
directly
(e.g.
Dog
cannot
 access
m_age).
 •  class D : public B

“a
D
object
is
a
kind
of
B”
 Construc.on
 •  So,
a
Dog
is
an
Animal.
What
happens
when
we
 construct
a
Dog?
 •  1.
The
base
part
of
the
class
is
constructed.
 m_age

 Animal
 0
 Construc.on
 •  So,
a
Dog
is
an
Animal.
What
happens
when
we
 construct
a
Dog?
 •  2.
The
member
variables
are
created. 
 m_age

 m_name

 Animal
 0
 “”
 Construc.on
 •  So,
a
Dog
is
an
Animal.
What
happens
when
we
 construct
a
Dog?
 •  3.
The
body
of
constructor
is
executed. 
 m_age

 m_name

 Animal
 0
 Ini.alized
 Destruc.on
 •  •  •  •  Just
reverse
the
order
of
construc.on.
 1.
The
body
of
destructor
is
executed.
 2.
The
member
variables
are
removed.
 3.
The
base
part
of
the
class
is
destructed. 
 Overriding
member
func.ons
 •  Assume
speak()
is
implemented
as
follows.
 Void Animal::speak() const { cout << “...” << endl; } •  Dog
inherits
this
func.on.
 •  But
we
want
our
Dog
to
really
say
something
when
 ordered
to
speak!
 
 Overriding
member
func.ons
 class Dog : public Animal { public: Dog(); ~Dog(); string getName() const; void setName(string name); void speak() const; private: string m_name; }; void Dog::speak() const { cout << “Woof!” << endl; } Animal a1; a1.speak(); •  Output:
 
…
 
Dog d1; d1.speak(); •  Output:
 
Woof! Overriding
member
func.ons
 class Dog : public Animal { public: Dog(); ~Dog(); string getName() const; void setName(string name); void speak() const; private: string m_name; }; void Dog::speak() const { cout << “Woof!” << endl; } •  Why
do
we
call
this
 overriding,
not
 overloading?
 Overriding
member
func.ons
 class Dog : public Animal { public: Dog(); ~Dog(); string getName() const; void setName(string name); void speak() const; private: string m_name; }; void Dog::speak() const { cout << “Woof!” << endl; } •  Why
do
we
call
this
 overriding,
not
 overloading?
 •  Overload
–
same
func.on
 name,
but
different
return
 type
and/or
different
set
of
 arguments
 •  Override
–
same
func.on
 name,
same
return
type,
 same
everything,
except
 defined
“again”
in
the
 derived
class.
 Overriding
member
func.ons
 •  Can
I
s.ll
call
the
base
class’s
speak()
with
a
Dog
 object? 
 •  Yes,
just
do:
 Dog d1; d1.Animal::speak(); •  You
probably
won’t
need
this
in
this
class
though.
 Virtual
func.ons:
Mo.va.on
 •  Back
to
this
diagram:
 Animal
 Implement
common
features
 of
all
animals
here
 derive Dog
 Implement
features
specific
to
 dogs
here
 derive Cat
 Implement
features
specific
to
 cats
here
 •  Suppose
we
have
speak()
overriden
in
Cat,
 where
it
goes
“Meow!”;
 Virtual
func.ons:
Mo.va.on
 •  C++
allows
a
pointer
to
the
 base
class
to
point
to
a
 derived
class.
 •  What
do
you
think
 
pAni->speak();

 
will
do?
 
should
do?
 Animal *pAni; int x; cin >> x; switch (x) { case 1: pAni = new Dog; break; case 2: pAni = new Cat; break; default: pAni = new Animal; break; } Virtual
func.ons:
Mo.va.on
 •  What
it
will
do:
 
“…”,
no
macer
what
x
is.
 •  What
it
should
do:
 
“Woof!”
if
x
==
1,
 
“Meow!”
if
x
==
2,
 
“…”
otherwise
 •  We
want
the
overriden
 func.on
to
be
called!
 Animal *pAni; int x; cin >> x; switch (x) { case 1: pAni = new Dog; break; case 2: pAni = new Cat; break; default: pAni = new Animal; break; } Virtual
func.ons
 class Animal { public: Animal(); virtual ~Animal(); int getAge() const; void setAge(int age); virtual void speak() const; private: int m_age; }; base class •  pAni‐>speak();
 class Dog : public Animal { public: Dog(); ~Dog(); string getName() const; void setName(string name); void speak() const; private: string m_name; }; derived class Polymorphism
 •  Late
binding
/
dynamic
 binding
 –  The
appropriate
version
is
 selected
during
run.me!
 •  Polymorphism
 –  pAni
can
take
mul.ple

 forms.
 Animal *pAni; int x; cin >> x; switch (x) { case 1: pAni = new Dog; break; case 2: pAni = new Cat; break; default: pAni = new Animal; break; } Polymorphism:
a
realis.c
example
 En.ty
 NonEnemyNPC
 fns:
damage(),
heal(),
move()
 vars:
energy,
posi.on
 Player
 fns:
speak()

 vars:
stuffToSay

 Enemy
 fns:
shoot()

 vars:
equipments,
items,
exp

 fns:
ac.on()

 vars:
itemsToDrop
 Zombie
 Robot
 fns:
ac.on()




 fns:
ac.on()





 Player List of Enemy Pointers P
 Z
 R
 R
 R
 List of Entity Pointers Virtual
func.ons
 class Animal { public: Animal(); virtual ~Animal(); int getAge() const; void setAge(int age); virtual void speak() const; private: int m_age; }; base class class Dog : public Animal { public: Dog(); ~Dog(); string getName() const; void setName(string name); void speak() const; private: string m_name; }; derived class •  Wait,
what’s
that
“virtual”
doing
before
the
 destructor
of
Animal?
 Note
for
Java
programmers
 •  In
Java,
every
func.on
is
virtual.
 Animal
speaks?
 •  “speak”
is
a
common
feature
among
all
(or
many)
 animals.
 •  But
it
really
means
something
only
if
we
know
what
 this
animal
is.
 •  Op.on
1:
 –  Get
rid
of
speak()
func.on
in
Animal,
and
implement
it
in
 all
the
derived
classes.
 •  Then
we
can’t
do
pAni‐>speak()…
 •  Op.on
2:
 –  Make
it
a
pure
virtual
func.on. Pure
virtual
func.ons
 •  You
declare
it
in
the
 base
class,
but
don’t
 define
it,
and
add
“=
0”
 in
the
declara.on.
 •  It
is
a
dummy
func.on.
 •  The
derived
class
must
 implement
all
the
pure
 virtual
func.ons
of
its
 base
class.

 class Animal { public: Animal(); virtual ~Animal(); int getAge() const; void setAge(int age); virtual void speak() const = 0; private: int m_age; }; Abstract
base
class
 •  If
a
class
has
at
least
one
pure
virtual
func.on,
it
is
called
an
 abstract
base
class.
 
Animal a1; // won’t compile Animal *pAni = new Animal; // won’t compile •  Animal
is
like
a
“common”
interface
without
complete
 implementa.on.
Or,
one
can
think
of
it
as
a
“framework.”
 Preview:
Recursion
 •  Suppose
I
have
the
following
func.on:
 A function calling itself! int factorial(int n) { if (n == 1) return 1; return n * factorial(n - 1); } int y = factorial(4); Preview:
Recursion
 int factorial(int n) n = 4 { if (n == 1) return 1; return n * factorial(n-1); factorial(4) } int factorial(int n) n = 2 { if (n == 1) return 1; return n * factorial(n-1); factorial(2) } int factorial(int n) n = 3 { if (n == 1) return 1; return n * factorial(n-1); factorial(3) } int factorial(int n) n = 1 { if (n == 1) return 1; return n * factorial(n-1); Factorial(1) } Preview:
Recursion
 •  The
technique
of
using
a
func.on
that
calls
itself
is
called
 recursion.
 •  This
is
like
the
domino
effect!
 factorial(1)
 factorial(2)
 factorial(3)
 factorial(4)
 •  It
helps
you
reduce
a
big
problem
into
a
smaller
one,
thus
 makes
the
implementa.on
a
lot
simpler.
 ...
View Full Document

This note was uploaded on 02/09/2012 for the course CS 32 taught by Professor Davidsmallberg during the Spring '08 term at UCLA.

Ask a homework question - tutors are online