#ifndef CHART_H
#define CHART_H
#include <iostream>
#include <string>
class Chart
{
public:
virtual void display() = 0;
};
class HistogramChart : public Chart
{
public:
HistogramChart() {
std::cout << "Historgram constructor" << std::endl;
}
void display() {
std::cout << "show Histogram" << std::endl;
}
};
class PieChart: public Chart
{
public:
PieChart() {
std::cout << "Piechart constructor" << std::endl;
}
void display() {
std::cout << "show Piechart" << std::endl;
}
};
class LineChart: public Chart
{
public:
LineChart() {
std::cout << "Linechart constructor" << std::endl;
}
void display() {
std::cout << "show Linechart" << std::endl;
}
};
class ChartFactory
{
public:
static Chart* getChart(const std::string& strName) {
Chart* p = NULL;
if (strName == "histogramchart") {
p = new HistogramChart;
} else if (strName == "piechart") {
p = new PieChart;
} else if (strName == "linechart") {
p = new LineChart;
}
return p;
}
};
#endif
#include "Chart.h"
void test()
{
Chart* p = ChartFactory::getChart(std::string("histogramchart"));
p->display();
p = ChartFactory::getChart(std::string("piechart"));
p->display();
p = ChartFactory::getChart(std::string("linechart"));
p->display();
}
int main()
{
test();
}
