Title: COM 262 Object Development
1COM 262Object Development
2class Order to contain list of shapes
3One list for all..
To find total surface area of order
for (int n0nltshapeNosn) sur_area
shapesn.compute_area( )
shapes
4Polymorphism
5An object, pointer or reference will be
implicitly converted from a derived class to a
public base class.
6(No Transcript)
7class Shape protected float
per_unit_cost int quantity public
// constructor shape(float c)
per_unit_cost c virtual float
compute_area(void) return 0.0 // non
virtual function float cost(void)
return compute_area( )per_unit_cost
class Circle public Shape private float
radius public // constructor
circle (float rad, float cost)
Shape(cost) radius rad
// member function float
compute_area (void) return piradrad
8(No Transcript)
9- for non-virtual functions, the declaration of the
pointer variable determines which function will
be used. - for virtual functions, the class of the object
pointed to determines which function definition
will be used
10class Mammal public Mammal()itsAge(1)
virtual Mammal() void Move() const
cout ltlt "Mammal move one step\n" virtual void
Speak() const cout ltlt "Mammal speak!\n"
protected int itsAge class Dog public
Mammal public Dog() virtual Dog()
void Move()const cout ltlt "Dog moves 5
steps...\n" void Speak()const cout ltlt
"Woof!\n" void WagTail() cout ltlt "Wagging
Tail...\n"
11int main() Mammal pDog new Dog // Mammal
pointer pDog-gtMove() // invoke
MammalMove() pDog-gtSpeak() // invoke
DogSpeak() !! // pDog-gtWagTail() // cant
call this.. // pointer to Mammal delete
pDog pDog 0 return 0
12class Mammal public Mammal() //
constructor 1 Mammal(int age) // constructor
2 Mammal() // destructor //accessors
//GetAge(), SetAge(int ), GetWeight(),
SetWeight(int ) // other functions
protected int itsAge int itsWeight
13// constructor 1 MammalMammal()
itsAge(1), itsWeight(5) // itsAge 1
itsWeight 5 // constructor
2 MammalMammal(int age) itsAge(age), itsWeight
(5) // itsAge age itsWeight 5
equivalent to..
INITIALISATION LIST
equivalent to..
14class Dog public Mammal public Dog()
// constructor 1 Dog(int age) //
constructor 2 Dog(int age, int weight) //
constructor 3 Dog(int age, BREED breed) //
constructor 4 Dog(int age, int weight, BREED
breed) // constructor 5 Dog() //
destructor private BREED itsBreed
15DogDog() // Dog constructor
1 Mammal(), // Mammal constructor
1 itsBreed(GOLDEN) DogDog(int age)
// Dog constructor 2 Mammal(age), //
Mammal constructor 2 itsBreed(GOLDEN)
DogDog(int age, int weight) // Dog
constructor 3 Mammal(age), // Mammal
constructor 2 itsBreed(GOLDEN) itsWeight
weight DogDog(int age, int weight, BREED
breed) // Dog constructor 4 Mammal(age),
// Mammal constructor 2 itsBreed(breed)
itsWeight weight DogDog(int age, BREED
breed) // Dog constructor 5 Mammal(age),
// Mammal constructor 2 itsBreed(breed)
16If any functions are virtual, then make the
destructor virtual also..
class Mammal Mammal( ) class Dog public
Mammal Dog( ) int main () Mammal pDog
new Dog // take that puppy for a walk.. delete
pDog // Mammal killed, Dog lives on .. pDog
0 return 0
class Mammal virtual Mammal( )
17VIRTUAL FUNCTIONS
Order system that stores and retrieves its data
include ltiostream.hgt include ltfstream.hgt void
OrderStore( ofstream stream ) stream.write(
name, size ) stream.write( (char ) length,
sizeof( int ) ) void OrderRestore(
ifstream stream ) stream.read( name, size
) stream.read( (char ) length, sizeof( int )
)
(see notes for Lecture 7)
18VIRTUAL FUNCTIONS
- How to do this exactly?
- There are different amounts of data members in
each object (of our Order list)
- How to READ BACK IN (restore)?
- Which type of object is it?
- How many of them?
?
150 circle-read-code 17 square-read-code
.
?
.
?
circle square
square
19Shape shapeArraym
1 2 3
0 1 n n1 m-1
delete shapeArrayn
for (int i n i i lt m-1 )
shapeArrayi shapeArrayi1
This leads nicely into Linked Lists
20TotalOrders
main() TotalOrders.Print()
Order1 Order2 Order3
for i 1 to MaxOrders Orderi.Print()
for i 1 to MaxShapes Shapei-gtPrint()
21NEW STUFF
- Static data and functions
- Percolating up
- Casting down (RTTI)
- Multiple Inheritance
- Capability classes Mixin classes
22Static data and functions
class Cat public Cat(int age)itsAge(age)How
ManyCats virtual Cat() HowManyCats--
virtual int GetAge() return itsAge
virtual void SetAge(int age) itsAge age
static int GetHowMany() return hmc
private static int HowManyCats int
itsAge
23Static data and functions
int CatHowManyCats 0 int main() const
int MaxCats 5 Cat CatHouseMaxCats for
(int i 0 iltMaxCats i) CatHousei new
Cat(i) cout ltlt CatHowManyCats
24Percolating up
Percolate shared functionality up the hierarchy,
without migrating the interface of each
class DO e.g. Horse and Bird both share Animal,
and have a function in common (e.g. eat), move
this up to base class and create a virtual
function DONT Percolate up an interface, e.g.
Fly, where it doesnt belong, just so you can
call that function only on some derived classes
25DO e.g. Horse and Bird both share Animal, and
have a function in common (e.g. Eat), move this
up to base class and create a virtual function
virtual Eat()
Animal
Eat()
Horse
Eat()
Bird
26DONT Percolate up an interface, e.g. Fly, where
it doesnt belong, just so you can call that
function only on some derived classes
Base class in danger of becoming a global
namespace for all the functions that might be
used by any of the derived classes
Horse pPeg new Horse pPeg-gtRun() pPeg new
Pegasus pPeg-gtFly()
void Run().. ?? virtual Fly() cout ltlt
Horses cant fly..
Horse
Fly() cout ltlt I can fly..
Pegasus
27Casting down Run Time Type Identification (RTTI)
int main() Horse RanchNumberHorses
// randomly assign to Horse object or Pegasus
object for (i0 iltNumberHorses i)
Pegasus pPeg dynamic_castlt Pegasus gt
(Ranchi) if (pPeg) // Base pointer
examined at runtime pPeg-gtFly() else cout
ltlt "Just a horse\n" return 0
May be indication of poor design. Consider using
virtual functions, templates, or multiple
inheritance instead.
28DO move functionality up the inheritance
hierarchy DONT move interface up the inheritance
hierarchy DO avoid switching on the runtime type
of the object use virtual methods, templates,
and multiple inheritance DONT cast pointers to
base objects down to derived objects
29Multiple Inheritance
Animal
Horse
class Animal class Horse public Animal class
Bird public Animal class Pegasus public
Horse, public Bird public private
Animal
Bird
Pegasus
30Inheriting from shared base class
GetAge( )
GetAge( )
Animal
Animal
Horse
Bird
Ambiguity Resolution Pegasus pPeg pPeg-gtGetAge(
)
Pegasus
31Ambiguity Resolution Pegasus pPeg pPeg-gtGetAge(
)
class Pegasus public Horse, public
Bird public Pegasus(COLOR, HANDS, bool, long,
int) virtual Pegasus() virtual int
GetAge() const return HorseGetAge()
private long itsNumberBelievers
32Virtual inheritance
Animal is a virtual base class of both Horse and
Bird
Animal
Horse
Bird
class Bird virtual public Animal class
Horse virtual public Animal
Pegasus
33Capability classes Mixin classes
Middle ground between multiple and single
inheritance e.g. Horse derives from Animal and
from Displayable. Displayable would just add a
few methods for displaying any object on
screen A mixin, or capability class, is a class
that adds functionality without adding much or
any data
M
34Problems with Multiple Inheritance
Many compilers dont support it yet It makes
debugging harder (some debuggers have a hard
time with it) Nearly everything that can be done
with multiple inheritance can be done without
it (some designs are made unnecessarily complex
by using it when it is not needed)
35DO use multiple inheritance when a new class
needs functions and features from more than one
base class DO use virtual inheritance when the
most derived classes must have only one instance
of the shared base class DO initialise the shared
base class from the most derived class when using
virtual base classes DONT use multiple
inheritance when single inheritance will do
36- CLASSROOM
- Download programs from website
Examine, compile and run - Especially
- static1,2,3,4.cpp
- chapman8-11,12,13.cpp
- multiple1,2,3,4,5,6,7.cpp
- READ ABOUT multiple inheritance
TUTORIAL