Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Node Class Test 01.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- #include <vector>
- #include <string>
- #include "stdafx.h"
- // node class
- class Node {
- int amount;
- public:
- std::string name;
- bool usable;
- Node();
- Node(std::string, int);
- std::vector<Node*> link;
- void checkUsability(); // will test to see if the node is available
- void connect(Node*);
- };
- // default (has no arguments)
- Node::Node() {
- name = "this node doesn't have a name";
- amount = 0;
- usable = false;
- }
- // actual constructor
- Node::Node (std::string _name, int _amount) {
- name = _name;
- amount = _amount;
- usable = false;
- }
- void Node::checkUsability() {
- usable = true;
- // add: check dependencies and see if this is actually usable
- std::cout << "checking requirements for " << name << "\n";
- // iterate over all the vector's elements
- bool result = true;
- for (std::vector<Node*>::iterator i = link.begin(); i != link.end(); ++i) {
- // check each
- if ((*i)->amount > 0) { // it's ok
- std::cout << (*i)->amount << " " << (*i)->name << " available - ok so far\n";
- }
- else { // if there are no available resources of this type
- std::cout << "0 " << (*i)->name << " available\n";
- result = false;
- }
- }
- usable = result;
- }
- void Node::connect(Node* target) {
- // add this new node as a thing we're dependent on
- link.push_back(target);
- }
- // main
- using namespace std;
- int main()
- {
- cout << "class creation test\n";
- // create a single node
- Node* dog = new Node("dog", 1);
- cout << "class created (usable should be false)\n";
- // make a second node
- Node* hug = new Node("hug", 1);
- // try connecting two nodes
- dog->connect(hug); // hug is required for dog to be usable
- dog->checkUsability();
- // test usability
- cout << dog->name << " is usable: " << dog->usable;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement