// Class example - student record // // // #include // here we define the class. For this class we include all member // function definitions inside the class definition itself, this // doesn't have to be done this way! class StudentRecord { // make the data members "private". That means we can't get at these // variables from outside the class (only member functions can // access these variables public: char *name; // student name double hw[3]; // homework grades double test[2]; // test grades // we don't need the data member ave, we can always compute it! // double ave; // final average NOW COMPUTED! // member functions are public (so we can call them from functions // that are not part of this class) public: // here is a special function called a "constructor" // a constructor is called whenever a new variable of // type "StudentRecord" is created. A constructor is often // used to initialize an object. StudentRecord(void) { name=NULL; // no name yet // clear all grades to 0 for (int i=0;i<3;i++) hw[i] = 0.0; for (int i=0;i<2;i++) test[i] = 0.0; // some debugging stuff! cout << "Student Record created" << endl; } // =================================================== // Functions that set the value of data members // note that we "hide" the data members behind these // functions... char *SetName( char *s) { // we don't copy the string, we copy the pointer!!! // there are times when you don't want to do this, // you want to allocate memory for a new string and // make a copy. return( name = s ); } // SetHW sets a single hw grade. // hwnum is the number of the hw and grade is the grade. double SetHW(int hwnum, double grade) { // do some error checking! if ((hwnum<0) || (hwnum>2)) { cerr << "Invalid HW number!" << endl; exit(1); } if ((grade<0.0) || (grade>100.0)) { cerr << "Invalid HW grade!" << endl; exit(1); } return( hw[hwnum] = grade); } // SetTest sets a single test grade. // testnum is the number of the test and grade is the grade. double SetTest(int testnum, double grade) { // do some error checking! if ((testnum<0) || (testnum>1)) { cerr << "Invalid Test number!" << endl; exit(1); } if ((grade<0.0) || (grade>100.0)) { cerr << "Invalid Test grade!" << endl; exit(1); } return( test[testnum] = grade); } // =================================================== // functions that get the value of data members char *GetName(void) { return(name); } // GetHW returns a single homework grade double GetHW(int hwnum) { // do some error checking! if ((hwnum<0) || (hwnum>2)) { cerr << "Invalid Test number!" << endl; exit(1); } return(hw[hwnum]); } // GetTest returns a single test grade double GetTest(int testnum) { // do some error checking! if ((testnum<0) || (testnum>1)) { cerr << "Invalid Test number!" << endl; exit(1); } return(test[testnum]); } // getting the average means we need to // compute it (we don't need the "ave" data member). double GetAve(void) { double tot=0; for (int i=0;i<3;i++) tot += hw[i]; for (int i=0;i<2;i++) tot += test[i]; return(tot/5); } // =================================================== // a function that prints out the record void print() { cout << "Name: " << name << endl; for (int i=0;i<3;i++) cout << "HW #" << i << ": " << hw[i] << endl; for (int i=0;i<2;i++) cout << "TEST #" << i << ": " << test[i] << endl; cout << "Average: " << GetAve()<< endl; } }; int main(void) { // declare a variable of type StudentRecord StudentRecord stu; // initialize with some stuff stu.SetName("Joe Student"); stu.SetHW(0,90.5); stu.SetHW(1,100); stu.SetHW(2,55.25); stu.SetTest(0,82.5); stu.SetTest(1,88); stu.name = "joe"; //print the record stu.print(); return(0); }