Created
May 25, 2013 08:23
-
-
Save azmfaridee/5648354 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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