Created
October 30, 2014 20:02
-
-
Save iluxonchik/afc0d25cbff1c4caf7c5 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
#ifndef __BENEFICIARY_H__ | |
#define __BENEFICIARY_H__ | |
// Abstarct class | |
class Beneficiary { | |
public: | |
Beneficiary() { } | |
virtual int totalHelpRecieved() = 0; | |
virtual void addHelpRecieved(int value) = 0; | |
}; | |
#endif |
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
#ifndef __COMMUNITY_H__ | |
#define __COMMUNITY_H__ | |
#include "Beneficiary.h" | |
#include "Individual.h" | |
#include <stdlib.h> | |
class Community : public Beneficiary { | |
public: | |
Community(int numIndividuals) : _numIndividuals(numIndividuals) { | |
_individuals = new Individual[numIndividuals]; | |
} | |
Community() : _numIndividuals(0) {} | |
~Community() { if (_individuals) delete[] _individuals; } | |
void addHelpRecieved(int value) { | |
int valuePerEntitiy; | |
if (_numIndividuals > 0) | |
// Avoid division by 0 | |
valuePerEntitiy = value / _numIndividuals; | |
else | |
valuePerEntitiy = 0; | |
for (int i = 0; i < _numIndividuals; i++) | |
_individuals[i].addHelpRecieved(valuePerEntitiy); | |
} | |
Individual* individual(int index) { | |
if (index < _numIndividuals) | |
return &(_individuals[index]); | |
return nullptr; | |
} | |
int numIndividuals() { | |
return _numIndividuals; | |
} | |
int totalHelpRecieved() { | |
int _totalHelp = 0; | |
if (_numIndividuals == 0) | |
std::cout << "No Individuals" << std::endl; | |
for (int i = 0; i < _numIndividuals; i++) | |
_totalHelp += _individuals[i].totalHelpRecieved(); | |
return _totalHelp; | |
} | |
private: | |
Individual *_individuals; | |
int _numIndividuals; | |
}; | |
#endif |
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
#ifndef __INDIVIDUAL_H__ | |
#define __INDIVIDUAL_H__ | |
#include "Beneficiary.h" | |
class Individual : public Beneficiary { | |
public: | |
Individual() : _totalHelpRecieved(0) { } | |
void addHelpRecieved(int value) { | |
_totalHelpRecieved += value; | |
} | |
int totalHelpRecieved() { | |
return _totalHelpRecieved; | |
} | |
private: | |
int _totalHelpRecieved; | |
}; | |
#endif |
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 <iostream> | |
#include "Region.h" | |
#include "Beneficiary.h" | |
#include "Individual.h" | |
#include "Community.h" | |
int main(void) { | |
Region r1(2, 5, 0); | |
Region r2(2, 5, 1); | |
Community c1(40); | |
Community* tempCom; | |
Individual i1; | |
i1.addHelpRecieved(20); | |
std::cout << i1.totalHelpRecieved() << std::endl; | |
c1.addHelpRecieved(80); | |
std::cout << c1.totalHelpRecieved() << std::endl; | |
std::cout << c1.individual(c1.numIndividuals() - 1)->totalHelpRecieved() << std::endl; | |
r1.addHelpRecieved(100); | |
r2.addHelpRecieved(100); | |
std::cout << r1.totalHelpRecieved() << std::endl; // Output: 100 | |
std::cout << r1.communitiy(1)->totalHelpRecieved() << std::endl; | |
std::cout << r1.communitiy(1)->individual(1)->totalHelpRecieved() << std::endl; | |
tempCom = r1.communitiy(r1.numCommunites() - 1); | |
// Add help to an individual from r1's community | |
tempCom->individual(tempCom->numIndividuals() -1)->addHelpRecieved(100); | |
// Print the "totalHelpRecieved" by r1 value (now it's 200) | |
std::cout << r1.totalHelpRecieved() << std::endl; // Output: 200 | |
std::cout << r2.communitiy(1)->totalHelpRecieved() << std::endl; | |
std::cout << r2.communitiy(1)->individual(1)->totalHelpRecieved() << std::endl; | |
std::cout << r2.individual(0)->totalHelpRecieved() << std::endl; | |
return 0; | |
} |
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
#ifndef __REGION_H__ | |
#define __REGION_H__ | |
#include "Beneficiary.h" | |
#include "Community.h" | |
#include "Individual.h" | |
class Region : public Beneficiary { | |
public: | |
Region(int numCommunities, int numIndividualsPerCom, int numIndividuals) : | |
_numCommunities(numCommunities), _numIndividuals(numIndividuals), _totalEntities(numCommunities + numIndividuals), | |
_numIndividualsPerCom(numIndividualsPerCom) | |
{ | |
// Allocation of Communitie(s) | |
_communities = (Community**) malloc(sizeof(Community**)*numCommunities); | |
for (int i = 0; i < numCommunities; i++) { | |
_communities[i] = new Community(numIndividualsPerCom); | |
} | |
// Allocation of Individuals | |
_individuals = new Individual[numIndividuals]; | |
} | |
~Region() { | |
// Clear the memory allocated for Communitie(s) | |
if ((*_communities) != nullptr){ | |
for (int i = 0; i < _numCommunities; i++){ | |
// Delete all of the "Community" objects in the array | |
delete _communities[i]; | |
} | |
// Delete the allocated memory for the pointers to "Community" pointers | |
delete _communities; | |
} | |
// Clear the memory associated to Individual(s) | |
delete[] _individuals; | |
} | |
void addHelpRecieved(int value) { | |
// C# Properties much ? :P | |
int valuePerEntitiy; | |
// _helpRecieved += value; | |
if (_totalEntities > 0) | |
// Avoid division by 0 | |
valuePerEntitiy = value / (_totalEntities); | |
else | |
valuePerEntitiy = 0; | |
// Add help to every Community | |
for (int i = 0; i < _numCommunities; i++) { | |
_communities[i]->addHelpRecieved(valuePerEntitiy); | |
} | |
// Add help to every Individual | |
for (int i = 0; i < _numIndividuals; i++) { | |
_individuals[i].addHelpRecieved(valuePerEntitiy); | |
} | |
} | |
int totalHelpRecieved() { | |
int _totalHelp = 0; | |
// Get total help from every community in the region | |
for (int i = 0; i < _numCommunities; i++) { | |
_totalHelp += _communities[i]->totalHelpRecieved(); | |
} | |
// Get total help from every individual in the region | |
for (int i = 0; i < _numIndividuals; i++) | |
_totalHelp += _individuals[i].totalHelpRecieved(); | |
return _totalHelp; | |
} | |
Community* communitiy(int index) { | |
if (index < _numCommunities) | |
return _communities[index]; | |
return nullptr; | |
} | |
Individual* individual(int index) { | |
if (index < _numIndividuals) | |
return &(_individuals[index]); | |
return nullptr; | |
} | |
int numCommunites() { | |
return _numCommunities; | |
} | |
int numIndividuals() { | |
return _numIndividuals; | |
} | |
private: | |
Community **_communities; | |
Individual *_individuals; | |
int _totalEntities, _numIndividuals, _numCommunities, _numIndividualsPerCom; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment