Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. //  demo.cpp
  2.  
  3. //  Compile with: $ g++ -Wall -std=c++11 -o demo demo.cpp
  4. //  Run with: $ ./demo
  5.  
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. class Student {
  14.  
  15.     vector<string> _Courses;
  16.     string _name;
  17.  
  18. public:
  19.  
  20.     Student(string name) : _name(name) {}
  21.  
  22.  
  23.     void addCourse(string name) {
  24.         _Courses.push_back(name);
  25.     }
  26.    
  27.    
  28.     int getNumberOfCourses() {
  29.         return _Courses.size();
  30.     }
  31.    
  32.    
  33.     string getName() {
  34.         return _name;
  35.     }
  36.    
  37.    
  38.     void printTranscript() {
  39.         cout << _name << "'s Transcript" << endl;
  40.         cout << "Number of courses: " << _Courses.size() << endl;
  41.         cout << "Courses: ";
  42.        
  43.         for (string &courseName : _Courses) {
  44.             cout << courseName << " ";
  45.         }
  46.        
  47.         cout << endl << endl;
  48.     }
  49. };
  50.  
  51.  
  52.  
  53. class University {
  54.  
  55.     vector<Student> _Students;
  56.  
  57. public:
  58.  
  59.     Student & addStudent(string name) {
  60.    
  61.         // Add the student to the end of the vector.
  62.         _Students.push_back(Student(name));
  63.        
  64.         // Return the Student object just added to the vector as a reference.
  65.         return _Students.back();
  66.     }
  67.    
  68.    
  69.     void printRegistrar() {
  70.    
  71.         cout << "University Registrar" << endl;
  72.         cout << left << setw(10) << "Name" << setw(10) << "Courses" << endl;
  73.    
  74.         for (Student & student : _Students) {
  75.             cout << setw(10) << student.getName() << setw(10) << student.getNumberOfCourses() << endl;
  76.         }  
  77.     }
  78. };
  79.  
  80.  
  81.  
  82. int main(int argc, char** argv) {
  83.  
  84.     // Create a new university called UHD.
  85.     University UHD;
  86.    
  87.     // Add 4 students to UHD.
  88.     UHD.addStudent("Jim");
  89.     UHD.addStudent("Bill");
  90.     Student Bob = UHD.addStudent("Bob");
  91.     UHD.addStudent("Greg");
  92.    
  93.     // Add 3 classes for Bob
  94.     Bob.addCourse("WRTG140");
  95.     Bob.addCourse("MATH009");
  96.     Bob.addCourse("BIOL101");      
  97.    
  98.    
  99.     // Shows Bob correctly has 3 classes.
  100.     Bob.printTranscript();
  101.    
  102.     // BUG: Shows Bob incorrectly has 0 classes.
  103.     UHD.printRegistrar();
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement