Skip to content

Instantly share code, notes, and snippets.

@azmfaridee
Created May 25, 2013 08:23
Show Gist options
  • Save azmfaridee/5648354 to your computer and use it in GitHub Desktop.
Save azmfaridee/5648354 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
#include <vector>
#define NUM_TREES 5
using namespace std;
// base tree
class AbstractDecisionTree {
public:
AbstractDecisionTree () {
cout << "AbstractDecisionTree CTor" << endl;
}
virtual ~AbstractDecisionTree () {
cout << "AbstractDecisionTree DTor" << endl;
}
};
// derived tree
class DecisionTree : public AbstractDecisionTree {
public:
DecisionTree () {
cout << "DecisionTree CTor" << endl;
}
virtual ~DecisionTree () {
cout << "DecisionRree DTor" << endl;
}
};
// base forest class
class Forest {
public:
vector<AbstractDecisionTree*> absDTrees;
Forest () {
cout << "Forest CTor" << endl;
}
virtual ~Forest () {
cout << "Forest DTor" << endl;
}
};
// derived forest class
class RandomForest : public Forest {
public:
RandomForest () {
cout << "RandomForest CTor" << endl;
for (int i = 0; i < NUM_TREES; i++) {
cout << "Creating " << i << "(th) decision tree" << endl;
DecisionTree* dtre = new DecisionTree();
absDTrees.push_back(dtre);
}
}
virtual ~RandomForest () {
cout << "RandomForest DTor" << endl;
for (int i = 0; i < NUM_TREES; i++) {
cout << "Deleting " << i << "(th) decision tree" << endl;
DecisionTree* dtre = dynamic_cast<DecisionTree*>(absDTrees[i]);
delete dtre;
}
}
};
int main(int argc, char** argv) {
cout << "Starting main program" << endl;
RandomForest rf;
cout << "End of main program" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment