Mega Code Archive

 
Categories / C++ / Data Structure
 

Define class and store in a list

#include <iostream> #include <list> #include <cstring> using namespace std; class Project { public:   char name[40];   int duration;   Project() {      strcpy(name, "");     duration = 0;   }   Project(char *n, int d) {     strcpy(name, n);     duration = d;   }   void add_days(int i) {     duration += i;   }   void sub_days(int i) {     duration -= i;   }   bool completed() {       return !duration;    }     void report() {     cout << name << ": ";     cout << duration;     cout << " days left.\n";   } }; bool operator<(const Project &a, const Project &b) {   return a.duration < b.duration; } bool operator>(const Project &a, const Project &b) {   return a.duration > b.duration; } bool operator==(const Project &a, const Project &b) {   return a.duration == b.duration; } bool operator!=(const Project &a, const Project &b) {   return a.duration != b.duration; } int main() {   list<Project> proj;   proj.push_back(Project("Project 1", 35));   proj.push_back(Project("Project 2", 190));   proj.push_back(Project("Project 3", 1000));   list<Project>::iterator p = proj.begin();   while(p != proj.end()) {     p->report();     p++;   }   p = proj.begin();   p->add_days(10);   do {     p->sub_days(5);     p->report();   } while (!p->completed());   return 0; }