
// Bunny.h
#ifndef __BUNNY_H
#define __BUNNY_H
#include <iostream>
namespace EasterBunny {
class Present {
protected:
static const double basePrice;
unsigned Quantity;
public:
Present(unsigned Q) {
Quantity = Q;
}
virtual double getPrice() const = 0;
virtual void print() const = 0;
};
class Egg: public Present {
static const double eggFactor;
public:
Egg(unsigned Q): Present(Q) {
}
double getPrice() const {
return Quantity * basePrice * eggFactor;
}
void print() const {
std::cout << "The price of the egg package is $" << getPrice() << std::endl;
}
};
class Chocolate: public Present {
static const double chocolateFactor;
public:
Chocolate(unsigned Q): Present(Q) {
}
double getPrice() const {
return Quantity * basePrice * chocolateFactor;
}
void print() const {
std::cout << "The price of the chocolate package is $" << getPrice() << std::endl;
}
};
class Candy: public Present {
static const double candyFactor;
public:
Candy(unsigned Q): Present(Q) {
}
double getPrice() const {
return Quantity * basePrice * candyFactor;
}
void print() const {
std::cout << "The price of the candy package is $" << getPrice() << std::endl;
}
};
}
#endif // __BUNNY_H
// Bunny.cpp
#include "Bunny.h"
namespace EasterBunny {
const double Present::basePrice = 50;
const double Egg::eggFactor = 0.3;
const double Chocolate::chocolateFactor = 2.4;
const double Candy::candyFactor = 1.2;
}
// Main.cpp
#include <iostream>
#include "Bunny.h"
using namespace std;
using namespace EasterBunny;
int main() {
Present *P[3];
P[0] = new Egg(5);
P[1] = new Chocolate(12);
P[2] = new Chocolate(7);
for (int i = 0; i < 3; i++) {
P[i]->print();
delete P[i];
}
return 0;
}