Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // demo.cpp
- // Compile with: $ g++ -Wall -std=c++11 -o demo demo.cpp
- // Run with: $ ./demo
- #include <iostream>
- #include <iomanip>
- #include <vector>
- using namespace std;
- class Student {
- vector<string> _Courses;
- string _name;
- public:
- Student(string name) : _name(name) {}
- void addCourse(string name) {
- _Courses.push_back(name);
- }
- int getNumberOfCourses() {
- return _Courses.size();
- }
- string getName() {
- return _name;
- }
- void printTranscript() {
- cout << _name << "'s Transcript" << endl;
- cout << "Number of courses: " << _Courses.size() << endl;
- cout << "Courses: ";
- for (string &courseName : _Courses) {
- cout << courseName << " ";
- }
- cout << endl << endl;
- }
- };
- class University {
- vector<Student> _Students;
- public:
- Student & addStudent(string name) {
- // Add the student to the end of the vector.
- _Students.push_back(Student(name));
- // Return the Student object just added to the vector as a reference.
- return _Students.back();
- }
- void printRegistrar() {
- cout << "University Registrar" << endl;
- cout << left << setw(10) << "Name" << setw(10) << "Courses" << endl;
- for (Student & student : _Students) {
- cout << setw(10) << student.getName() << setw(10) << student.getNumberOfCourses() << endl;
- }
- }
- };
- int main(int argc, char** argv) {
- // Create a new university called UHD.
- University UHD;
- // Add 4 students to UHD.
- UHD.addStudent("Jim");
- UHD.addStudent("Bill");
- Student Bob = UHD.addStudent("Bob");
- UHD.addStudent("Greg");
- // Add 3 classes for Bob
- Bob.addCourse("WRTG140");
- Bob.addCourse("MATH009");
- Bob.addCourse("BIOL101");
- // Shows Bob correctly has 3 classes.
- Bob.printTranscript();
- // BUG: Shows Bob incorrectly has 0 classes.
- UHD.printRegistrar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement