c++ List class as Student type -
i trying use class student , declare list type. can pushback without changing list.h or node.h how can print data in list2? given print() function within list..h not work :(
node.h
#ifndef node_h #define node_h #include <string> #include <iostream> using namespace std; template <typename t> class node { private: t data; node<t>* next; public: node(t); virtual ~node(); // base class destructor must virtual template <typename u> friend class list; }; template <typename t> node<t>::node(t d) { data = d; next = null; } template <typename t> node<t>::~node() { } #endif /* strnode_h */
list.h
#ifndef list_h #define list_h #include "node.h" // singly linked list template <typename t> class list { private: node<t>* head; // pointer first node node<t>* tail; // pointer last node int count; // number of nodes in list public: class outofrangeexception{ }; // empty inner class exception handling list(); virtual ~list(); void push_back(t item); void insert(int index, t item); void remove(int index); int indexof(t item); t get(int position); // outofrangeexception generated bool isempty(); int size(); void print(); }; template <typename t> list<t>::list() { head = tail = null; count = 0; } template <typename t> list<t>::~list() { node<t>* discard; while (head != 0) { discard = head; head = head->next; delete discard; } } // append item @ end of strlist template <typename t> void list<t>::push_back(t item) { try { node<t>* newnode = new node<t>(item); if (head == 0) { head = tail = newnode; } else { tail->next = newnode; tail = newnode; } ++count; } catch (bad_alloc &e) { cout << "memory allocation exception: " << e.what() << endl; exit(1); } } // insert item @ specified index template <typename t> void list<t>::insert(int index, t item) { try { if (index < 0 || index > count) // push_back() if index == count throw outofrangeexception(); node<t>* newnode = new node<t>(item); if (head == 0) { // empty head = tail = newnode; } else if (index == 0) { // @ start newnode->next = head; head = newnode; } else if (index == count) { // @ end tail->next = newnode; tail = newnode; } else { // insert in middle node<t>* prevnode; node<t>* currnode = head; (int = 0; < index; i++) { prevnode = currnode; currnode = currnode->next; } // insert between 'prevnode' , 'currnode' prevnode->next = newnode; newnode->next = currnode; } ++count; } catch (bad_alloc &e) { cout << "memory allocation exception: " << e.what() << endl; exit(1); } } // strlist empty? template <typename t> bool list<t>::isempty() { return count == 0; } // remove item @ specified index template <typename t> void list<t>::remove(int index) { if (index < 0 || index >= count) throw outofrangeexception(); if (index == 0) { // @ start node<t>* discard = head; head = head->next; delete discard; } else { node<t>* prevnode; node<t>* currnode = head; (int = 0; < index; i++) { prevnode = currnode; currnode = currnode->next; } // remove 'currnode' prevnode->next = currnode->next; // bypass delete currnode; if (index == count - 1) // last node removed. update 'tail' tail = prevnode; } --count; if (count == 0) tail = null; } // retrieve item @ given position of strlist. position starts 0. // throws outofrangeexception if invalid position value given. template <typename t> t list<t>::get(int position) { if (position < 0 || position >= count) throw outofrangeexception(); int loc = 0; node<t>* curr = head; while (loc < position) { ++loc; curr = curr->next; } return curr->data; } // requirement: // != operator of <class t> used template <typename t> int list<t>::indexof(t item) { if (head == 0) { return -1; // not found } else { int index = 0; node<t>* currnode = head; while (currnode->data != item && currnode != null) { currnode = currnode->next; ++index; } if (currnode == null) // not found thru end return -1; else return index; } } // number of nodes in strlist template <typename t> int list<t>::size() { return count; } // requirement: // << operator <class t> used. template <typename t> void list<t>::print() { cout << "*** strlist contents ***" << endl; (int = 0; < count; i++) { cout << << ": " << get(i) << endl; } } #endif
student.h
#include "list.h" class student { private: string name; int id; public: student(); student(string a); virtual ~student(); friend ostream& operator<<(ostream &os, const student& p); bool operator!=(const student &p) const; bool operator==(const student &p) const; }; student::student() { } student::student(string a) { name = a; } student::~student() { } ostream& operator<<(ostream &os, const student& p) { return os << p.name; } bool student::operator==(const student &p) const { // compare values, , return bool result. if (name == p.name) return true; else return false; } bool student::operator!=(const student &p) const { return !(*this == p); }
main.cpp
#include <iostream> using namespace std; #include "student.h" int main() { cout << "\n*** strlist test ***" << endl; list<string> list; list.push_back("zero"); list.push_back("one"); list.push_back("two"); list.push_back("three"); list.push_back("four"); list.push_back("five"); list.print(); list.insert(1, "inserted @ position 1"); list.insert(0, "inserted @ position 0"); list.insert(4, "inserted @ position 4"); list.print(); cout << "removing @ indexes 3, 0" << endl; list.remove(3); list.remove(0); list.print(); list.insert(2, "inserted @ position 2"); list.print(); cout << "five @ index " << list.indexof("five") << endl; cout << "two @ index " << list.indexof("two") << endl; //test student class implementation // student<string> st1; //create new student ryan martin id of 1 list<student> list2; student stu("ryan martin"); list2.push_back(stu); //list2.print(); //list2.push_back("ryan"); //list2.printstudents(); //test student class stored , can access return 0; }
the << operator must defined student class. quote list.h:
// requirement: // << operator <class t> used. template <typename t> void list<t>::print() { cout << "*** strlist contents ***" << endl; (int = 0; < count; i++) { cout << << ": " << get(i) << endl; } }
so in student class, need implement operator<<(ostream &out);
do friend (friends fun!):
friend std::ostream& operator<< (std::ostream &out, const student &stu) { return out << stu.name << " id: " << stu.id << std::endl; }
here reference: http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
Comments
Post a Comment