CSE2305
Object-Oriented Software Engineering
Exercise 3: Animal Hospital: solution
You were asked to create a simple database to help the animal hospital using
inheritance.
Observations:
The class hierarchy is already in the question so this tutorial is in basic
terms translating UML into C++. First the base class, Animal
. Here
a couple of utility methods have been added (not in the UML diagram). Two methods
are pure virtual meaning that we can't instantiate an instance of Animal
- more detail is required in a derived class.
Animal.h |
class Animal {
public:
enum SexType { Indeterminate, Male, Female, Hermaphrodite };
typedef unsigned int Days;
Animal(const string& name,
int ageInYears,
SexType sex,
const Date& arrived = Date::Today(), const Date& departure = Date::Today());
virtual ~Animal(void);
void arrived(const Date& arrivalDate = Date::Today());
void departed(const Date& departureDate = Date::Today());
void setName(const string& name);
void setAgeInYears(int y);
void setSexType(SexType s);
Days getLengthOfStay(void) const;
const string& getName() const;
int getAgeInYears() const;
SexType getSexType() const;
virtual const string& getTypeName() const { return "Animal"; }
virtual int numberOfLegs(void) = 0; // pure virtual
virtual int feedsPerDay(void) = 0;
virtual void printDetails(ostream& s) {
s << getName() << " (" << getTypeName() << ") with " << numberOfLegs() << " legs. Arrived " <<
getArrivalDate() << ", staying " << getLengthOfStay() << " days." << endl;
s << "Requires " << feedsPerDay() << " feeds per day." << endl;
}
protected:
Date myArrivalDate;
Date myDepartureDate;
string myName;
int myAgeInYears;
SexType mySex;
};
|
Next, the Cat
class:
Cat.h |
class Cat : public Animal {
public:
Cat(const string& name,
int ageInYears,
SexType sex,
const Date& arrived = Date::Today(), const Date& departure = Date::Today());
virtual const string& getTypeName() const { return "Cat"; }
virtual int numberOfLegs(void) { return numberOfLegs - legsMissing; }
virtual int feedsPerDay(void) { return feedsPerDay; }
void setFeedsPerDay(int numberOfFeeds);
void setNumberOfLegsMissing(int n);
private:
int legsMissing;
int feedsPerDay;
const int numberOfLegs = 4;
};
|
Let's look at the Cat
constructor:
Cat.cpp |
#include "Cat.h"
Cat::Cat(const string& name, int ageInYears,
SexType sex, const Date& arrived,
const Date& departed) :
Animal(name, ageInYears, sex, arrived, departed),
legsMissing(0),
feedsPerDay(2)
{
}
.
.
.
|
The other derived classes should be easy. Some questions to consider:
• Why is feedsPerDay()
not const? Should it be?
• How much code is in common between Cat
and Dog
?
(refer tute questions).
The question also called for a simple database class (AnimalHospital
).
Here we could use the List
class (from Assignment
1) to implement the AnimalHospital
class. Something to think
about – should AnimalHospital
inherit from List
,
or should List
be a private data member of AnimalHospital
?
This material is part of the CSE2305 – Object-Oriented
Software Engineering course.
Copyright © Jon McCormack, 2005. All rights reserved.
Last modified:
September 5, 2005