Homework Question in C++
Yet another homework question that I don't know how I found it interesting to answer, I guess that must be I like to kill time with coding.
It was posted here originally.
student-class.cpp
:
1 #if !defined(STUDENT_CLASS) 2 #define STUDENT_CLASS 1.0.1 3 4 #include <algorithm> 5 #include <iostream> 6 #include <string> 7 #include <list> 8 9 10 class school_class { 11 typedef std::list<std::string>::const_iterator student; 12 public: 13 14 bool empty() const throw(); 15 void add_student(const std::string &newbie) throw(); 16 void expel_student(const std::string &bad_boy) throw(); 17 bool find_student(const std::string &some_one) const throw(); 18 void list_students() const throw(); 19 20 private: 21 std::list<std::string> student_list; 22 }; 23 24 inline 25 bool school_class::empty() const throw() 26 { return student_list.empty(); } 27 28 inline 29 void school_class::add_student(const std::string &newbie) throw() 30 { student_list.push_back(newbie); } 31 32 inline 33 void school_class::expel_student(const std::string &bad_boy) throw() 34 { student_list.remove(bad_boy); } 35 36 inline 37 bool school_class::find_student(const std::string &some_one) const throw() 38 { return std::count(student_list.begin(), student_list.end(), some_one) != 0; } 39 40 inline 41 void school_class::list_students() const throw() 42 { 43 for (student some_one = student_list.begin(); 44 some_one != student_list.end(); 45 ++some_one) 46 std::cout<<*some_one<<std::endl; 47 } 48 49 #endif // STUDENT_CLASS 1.0.1 50 51 using std::cout; 52 using std::endl; 53 using std::cin; 54 using std::getline; 55 using std::string; 56 57 int main() 58 { 59 school_class my_class; 60 string name; 61 while (cout<<"enter student : ", getline(cin, name) && !name.empty()) 62 my_class.add_student(name); 63 while (!my_class.empty()) { 64 cout<<endl 65 <<string(5, '-')<<"[students in the class]"<<string(5, '-')<<endl; 66 my_class.list_students(); 67 cout<<string(8 , '-')<<"[end of the list]"<<string(8, '-')<<endl; 68 cout<<"enter the name of the student to be kicked out: "<<endl; 69 getline(cin, name); 70 if (name.empty()) break; 71 if (my_class.find_student(name)) { 72 my_class.expel_student(name); 73 cout<<name<<" is out."<<endl; 74 } else 75 cout<<endl<<"hey man, no such student!"<<endl; 76 } 77 cout<<endl<<"bye."<<endl; 78 return 0; 79 }
Here is the improved version:
student-class-2.cpp
:
1 #if !defined(STUDENT_CLASS) 2 #define STUDENT_CLASS 1.0.2 3 4 #include <algorithm> 5 #include <ostream> 6 #include <stdexcept> 7 #include <string> 8 #include <list> 9 #include <typeinfo> 10 11 namespace sc { 12 13 class school_class { 14 typedef std::list<std::string>::const_iterator student; 15 public: 16 17 virtual ~school_class() {} 18 bool empty() const throw(); 19 void add_student(const std::string &newbie) throw(); 20 void expel_student(const std::string &bad_boy) 21 throw(std::invalid_argument); 22 bool find_student(const std::string &some_one) const throw(); 23 24 operator bool() const throw(); 25 friend std::ostream& 26 operator <<(std::ostream &out, const school_class &c) throw(); 27 28 protected: 29 virtual void list_students(std::ostream &out) const throw(); 30 31 private: 32 std::list<std::string> student_list; 33 }; 34 35 inline 36 bool school_class::empty() const throw() 37 { return student_list.empty(); } 38 39 inline 40 void school_class::add_student(const std::string &newbie) throw() 41 { student_list.push_back(newbie); } 42 43 inline 44 void school_class::expel_student(const std::string &bad_boy) 45 throw(std::invalid_argument) 46 { 47 if (find_student(bad_boy)) student_list.remove(bad_boy); 48 else throw std::invalid_argument("no such student [" + bad_boy + "]"); 49 } 50 51 inline 52 bool school_class::find_student(const std::string &some_one) const throw() 53 { return std::count(student_list.begin(), student_list.end(), some_one) != 0; } 54 55 inline 56 void school_class::list_students(std::ostream &out) const throw() 57 { 58 out<<std::endl<<std::string(5, '-')<<"[students in the class]" 59 <<std::string(5, '-')<<std::endl; 60 for (student some_one = student_list.begin(); 61 some_one != student_list.end(); 62 ++some_one) 63 out<<*some_one<<std::endl; 64 out<<std::string(8 , '-')<<"[end of the list]" 65 <<std::string(8, '-')<<std::endl; 66 } 67 68 inline 69 school_class::operator bool() const throw() 70 { return !empty(); } 71 72 inline 73 std::ostream& 74 operator <<(std::ostream &out, const school_class &c) throw() 75 { c.list_students(out); return out; } 76 77 } // namespace sc 78 79 #endif // STUDENT_CLASS 1.0.2 80 81 #include <iostream> 82 using std::cout; using std::cerr; 83 using std::endl; using std::cin; 84 using std::getline; using std::string; 85 using std::exception; using std::invalid_argument; 86 using namespace sc; 87 88 int main() 89 { 90 school_class my_class; 91 string name; 92 while (cout<<"enter student : ", getline(cin, name) && !name.empty()) 93 my_class.add_student(name); 94 while (my_class) { 95 cout<<my_class 96 <<"enter the name of the student to be kicked out: "<<endl; 97 getline(cin, name); 98 if (name.empty()) break; 99 try { 100 my_class.expel_student(name); 101 cout<<name<<" is out."<<endl; 102 } 103 catch (exception &e) { 104 if (typeid(e) == typeid(invalid_argument)) 105 cerr<<"hey man, be careful!"<<endl; 106 cerr<<"exception ["<<typeid(e).name()<<"]: "<<e.what()<<endl; 107 } 108 } 109 cout<<endl<<"bye."<<endl; 110 return 0; 111 }
Edit: To accommodate new C++ Statndard, I have to add
#include <typeinfo>
in the beginning to make it compile.