
// Bear.h
#ifndef __BEAR_H
#define __BEAR_H
#include <iostream>
namespace Shelter {
class Bear {
protected:
static const double basePrice;
unsigned Days;
public:
Bear(unsigned d) {
Days = d;
}
virtual double getCost() const = 0;
virtual void print() const = 0;
};
class Grizzly: public Bear {
static const double grizzlyFactor;
public:
Grizzly(unsigned d): Bear(d) {
}
double getCost() const {
return Days * basePrice * grizzlyFactor;
}
void print() const {
std::cout << "Feeding a grizzly for " << Days << " days will cost $" << getCost() << std::endl;
}
};
class Panda: public Bear {
static const double pandaFactor;
public:
Panda(unsigned d): Bear(d) {
}
double getCost() const {
return Days * basePrice * pandaFactor;
}
void print() const {
std::cout << "Feeding a panda for " << Days << " days will cost $" << getCost() << std::endl;
}
};
class PolarBear: public Bear {
static const double polarBearFactor;
public:
PolarBear(unsigned d): Bear(d) {
}
double getCost() const {
return Days * basePrice * polarBearFactor;
}
void print() const {
std::cout << "Feeding a polar bear for " << Days << " days will cost $" << getCost() << std::endl;
}
};
}
#endif // __BEAR_H
// Bear.cpp
#include "Bear.h"
namespace Shelter {
const double Bear::basePrice = 50;
const double Grizzly::grizzlyFactor = 2.4;
const double Panda::pandaFactor = 0.3;
const double PolarBear::polarBearFactor = 1.2;
}
// Main.cpp
#include <iostream>
#include "Bear.h"
using namespace std;
using namespace Shelter;
int main() {
Bear *P[3];
P[0] = new Grizzly(22);
P[1] = new Panda(51);
P[2] = new PolarBear(43);
for (int i = 0; i < 3; i++) {
P[i]->print();
delete P[i];
}
return 0;
}