Title: COM 262 Object Development
1COM 262Object Development
2Implementation of Class shape
class Shape protected float per_unit_cost
public // constructors
Shape(float c) per_unit_cost c // default
constructor Shape( ) // virtual
function virtual float compute_area(void)
return 0.0 // non virtual function
float compute_cost(void) return
compute_area( ) per_unit_cost // end
class shape
Shape is an abstract class because it is limited
in functionality
3Implementation of derived Class rectangle
class Rectangle public Shape private float
length float breadth public //
constructors Rectangle(float len, float bth,
float cost) Shape(cost)
length len breadth bth
Rectangle( ) // member
function //override float compute_area
(void) return lengthbreadth
//end class
4Initialisation list
- Initialisation list - because an instance of a
derived - class contains variables from both the base and
- derived class (in this case rectangle - length,
breath - and shape - per_unit_cost), the constructor for
both - can/must be used to initialise the instance
Rectangle(float len, float bth, float cost)
Shape(cost) length len breadth
bth
call to constructor for base class
5Method Overriding
virtual float compute_area(void) return 0.0
Shape
float compute_area (void) //override
return lengthbreadth
Rectangle
float compute_area (void) //override
return piradrad
Circle
Polymorphism
6OverridingPure Virtual Functions
class Animal protected // The attributes
common to all animals int no_legs String
name int speed // methods public
Animal(void) String GetName(void) void
Feed( int n ) virtual void Threaten(void) 0
// Threaten routine MUST be overridden by
derived classes
virtual void Run() // Run routine
MAY be inherited and overridden, // but the
generic version will do for most animals
7OverridingPure Virtual Functions
class Dog public Animal public void
Threaten(void) // had to implement this void
GoWalkies(int distance) // extra Dog thing //
generic version of Run() good enough for this
Dog
8Child or Parent class pointer?
class 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"
9Child or Parent class pointer?
int 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
10Initialisation list and multiple constructors
class 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
11Initialisation list and multiple constructors
// constructor 1 MammalMammal()
itsAge(1), itsWeight(5) // itsAge 1
itsWeight 5
equivalent to..
// constructor 2 MammalMammal(int age)
itsAge(age), itsWeight(5) // itsAge age
itsWeight 5
equivalent to..
12Initialisation list and multiple constructors
class 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
13Initialisation list and multiple constructors
DogDog() // 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)
14Virtual destructor
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
If any functions are virtual, then make the
destructor virtual also..
class Mammal virtual Mammal( )
15Extra facts
- Static data and functions
- Percolating up
- Casting down (RTTI)
- Multiple Inheritance
- Capability classes Mixin classes
16Static data and functions
class Cat public Cat(int age)itsAge(age)HowM
anyCats 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
17Static 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
18Percolating 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
19Percolating up
- 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
virtual Eat()
Animal
Eat()
Horse
Eat()
Bird
20Percolating up
- DONT
- 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
void Run().. ?? virtual Fly() cout ltlt
Horses cant fly..
Horse
Horse pPeg new Horse pPeg-gtRun() pPeg new
Pegasus pPeg-gtFly()
Fly() cout ltlt I can fly..
Pegasus
21Casting 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.
22Dos and Donts
- DO 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
23Multiple Inheritance
Animal
class Animal class Horse public Animal class
Bird public Animal class Pegasus public
Horse, public Bird public private
Horse
Animal
Bird
Pegasus
24Inheriting from shared base class
GetAge( )
GetAge( )
Animal
Animal
Horse
Bird
Ambiguity Resolution Pegasus pPeg pPeg?GetAge(
)
Pegasus
25Inheriting from shared base class
Ambiguity Resolution Pegasus pPeg pPeg?GetAge(
)
class Pegasus public Horse, public
Bird public Pegasus(COLOR, HANDS, bool, long,
int) virtual Pegasus() virtual int
GetAge() const return HorseGetAge()
private long itsNumberBelievers
26Virtual inheritance
Animal
Animal is a virtual base class of both Horse and
Bird
Horse
Bird
class Bird virtual public Animal
class Horse virtual public Animal
Pegasus
27Capability 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
28Problems 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) - Not in Java..
29Dos and Donts
- DO 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