Laboratory 1 Solutions
Solution to student1.cc code:
/*******************************************************************
* Name: Brenda Student
* Date: 1/21/14
* Course: CSCI 132
* Assignment: Lab1
* Instructor: Royden
* Implementation: student1.cc
* Implementation File: This file contains the implementation for
* all the member functions for the Student class, storing each
* course as an element in an array.
********************************************************************/
#include "student.h"
#include <iostream>
#include <cstring>
using namespace std;
Student::Student( ) {
//Precondition:
// None.
//Postcondition:
// Initializes the Student's number of courses to 0,
// Initializes the Student's graduation date to 0,
// Initializes the Student's name to "John Doe"
numCourses = 0;
gradDate = 0;
strcpy(studentName, "John Doe");
} //end Student()
Student::Student(/*in*/ char name[], /*in*/ int year) {
//Precondition:
// name[ ] is an array of characters, less than 30 long,
// containing the student's name.
// year is an integer representing the student's graduation date.
//Postcondition:
// Initializes the Student's number of courses to 0,
// Initializes the Student's graduation date to year,
// Initializes the Student's name to the string contained in name.
numCourses = 0;
gradDate = year;
strcpy(studentName, name);
} //end Student(char, int)
void Student::AddCourse( /*in*/ string9 courseName){
//Precondition:
// courseName is a string of less than 10 characters
// There are less than 6 courses in the course list already.
//Postcondition:
// The string, courseName, is added to the courselist.
strcpy(courses[numCourses], courseName);
numCourses += 1;
} //end AddCourse()
void Student::ListInfo(void) const {
//Precondition:
// A Student object has been initialized.
//Postcondition:
// The function prints out the student's name and
// graduation date, one each line.
// The student's name is printed in the form:
// "Name: Jane Doe"
// The gradduation date is printed in the form:
// "Graduation Date: 2006"
// One blank line is printed after the information list.
cout << "Name: " << studentName << endl;
cout << "Graduation date: " << gradDate << endl;
cout << endl;
} //end ListInfo()
void Student::ListCourses(void) const {
//Precondition:
// A Student object has been initialized.
//Postcondition:
// The courses in the course list are printed to the monitor.
// Courses are printed in the form: "Course 1: csci262"
// One course is on each line.
// One blank line is printed after the list of courses.
for (int i = 0; i < numCourses; i++) {
cout << "Course " << i+1 << ": " << courses[i] << endl;
}
cout << endl;
} //end ListCourses()
void Student::SetGradDate(/*in*/ int year) {
//Precondition:
// year is an integer representing a date.
//Postcondition:
// The graduation date for the student object is set to year.
gradDate = year;
} //end SetGradDate()
Home | | Syllabus | | Assignments | | Lecture Notes