Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. // Node Class Test 01.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include "stdafx.h"
  8.  
  9.  
  10. // node class
  11. class Node {
  12.     int amount;
  13. public:
  14.     std::string name;
  15.     bool usable;
  16.     Node();
  17.     Node(std::string, int);
  18.     std::vector<Node*> link;
  19.     void checkUsability(); // will test to see if the node is available
  20.     void connect(Node*);
  21. };
  22.  
  23.  
  24. // default (has no arguments)
  25. Node::Node() {
  26.     name = "this node doesn't have a name";
  27.     amount = 0;
  28.     usable = false;
  29. }
  30.  
  31.  
  32. // actual constructor
  33. Node::Node (std::string _name, int _amount) {
  34.     name = _name;
  35.     amount = _amount;
  36.     usable = false;
  37. }
  38.  
  39.  
  40. void Node::checkUsability() {
  41.     usable = true;
  42.     // add: check dependencies and see if this is actually usable
  43.  
  44.     std::cout << "checking requirements for " << name << "\n";
  45.        
  46.     // iterate over all the vector's elements
  47.  
  48.     bool result = true;
  49.     for (std::vector<Node*>::iterator i = link.begin(); i != link.end(); ++i) {
  50.         // check each
  51.         if ((*i)->amount > 0) { // it's ok
  52.             std::cout << (*i)->amount << " " << (*i)->name << " available - ok so far\n";
  53.         }
  54.         else { // if there are no available resources of this type
  55.             std::cout << "0 " << (*i)->name << " available\n";
  56.             result = false;
  57.         }
  58.     }
  59.     usable = result;
  60. }
  61.  
  62.  
  63. void Node::connect(Node* target) {
  64.     // add this new node as a thing we're dependent on
  65.     link.push_back(target);
  66. }
  67.  
  68.  
  69.  
  70. // main
  71.  
  72. using namespace std;
  73.  
  74. int main()
  75. {
  76.  
  77.     cout << "class creation test\n";
  78.  
  79.     // create a single node
  80.  
  81.  
  82.     Node* dog = new Node("dog", 1);
  83.  
  84.     cout << "class created (usable should be false)\n";
  85.  
  86.    
  87.  
  88.     // make a second node
  89.  
  90.     Node* hug = new Node("hug", 1);
  91.     // try connecting two nodes
  92.     dog->connect(hug); // hug is required for dog to be usable
  93.     dog->checkUsability();
  94.  
  95.     // test usability
  96.  
  97.     cout << dog->name << " is usable: " << dog->usable;
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement