home *** CD-ROM | disk | FTP | other *** search
/ C++ for Dummies (3rd Edition) / C_FD.iso / CHAP12 / STUDENT.H < prev   
C/C++ Source or Header  |  1996-09-15  |  2KB  |  86 lines

  1. // student.h
  2. #include <iostream.h>
  3. #include <string.h>
  4.  
  5. class Student
  6. {
  7.   public:
  8.    Student()
  9.    {
  10.       cout << "constructing student no name\n";
  11.       semesterHours = 0;
  12.       gpa = 0.0;
  13.       name[0] = '\0';
  14.    }
  15.    Student(char *pName)
  16.    {
  17.       cout << "constructing student " << pName << "\n";
  18.       strncpy(name, pName, sizeof(name));
  19.       name[sizeof(name) - 1]  = '\0';
  20.  
  21.       semesterHours = 0;
  22.       gpa = 0.0;
  23.    }
  24.    Student(char *pName, int xfrHours, float xfrGPA)
  25.    {
  26.       cout << "constructing student " << pName << "\n";
  27.       strncpy(name, pName, sizeof(name));
  28.       name[sizeof(name) - 1]  = '\0';
  29.       semesterHours = xfrHours;
  30.       gpa = xfrGPA;
  31.    }
  32.  
  33. /*
  34.    // the following constructor uses default values to
  35.    // serve the function of all three constructors above
  36.    Student(char *pName  = "no name",
  37.            int xfrHours = 0,
  38.            float xfrGPA = 0.0)
  39.    {
  40.       cout << "constructing student " << pName << "\n";
  41.       strncpy(name, pName, sizeof(name));
  42.       name[sizeof(name) - 1]  = æ\0Æ;
  43.       semesterHours = xfrHours;
  44.       gpa = xfrGPA;
  45.    }
  46. */
  47.  
  48.   ~Student()
  49.    {
  50.       // whatever assets are returned here
  51.    }
  52.  
  53.    //grade - return the current grade point average
  54.    float grade()
  55.    {
  56.       return gpa;
  57.    }
  58.    //hours - return the number of semester hours
  59.    int hours() 
  60.    {
  61.       return semesterHours;
  62.    }
  63.  
  64.    //addCourse - add another course to the studentÆs record
  65.    float addCourse(int hours, float grade);
  66.  
  67.    //here we allow the grade to be changed
  68.    float grade(float newGPA)
  69.    {
  70.       float oldGPA = gpa;
  71.       //only if the new value is valid
  72.       if (newGPA > 0 && newGPA <= 4.0)
  73.  
  74.       {
  75.          gpa = newGPA;
  76.       }
  77.       return oldGPA;
  78.    }
  79.  
  80.   //the following members are off-limits to others
  81.   protected:
  82.    char  name[40];
  83.    int   semesterHours;  //hours earned towards graduation
  84.    float gpa;            //grade point average
  85. };
  86.