#include #include #include /* for random number generation */ using namespace std; class student { protected: string name; int year; // 0=freshman, 1=sophomore, etc 4 = grad student int id; double gpa; public: student(string n) // constructor { name = n; year = rand()%5; id = 700 + rand()%3000000; gpa = (rand()%401)/100.0; } ~student() {} // destructor string getname() // accessor { return name; } void setyear(int y) { if (y>=0 && y<5) year =y; } // modifier virtual void printinfo() // virtual means overridable { cout << name + " is a "; string Y[] = {"freshman","sophomore","junior","senior","graduate student"}; cout << Y[year] << " with a GPA of " << gpa << endl; } // separate computation from IO }; void fs(student s) { s.setyear(3); } void gs(student *s) { s->setyear(3); } int main(int argc, char* argv[]) { student s1("Ian"); student* s2 = new student("Alex"); // only form allowed in Java, but no * // what's the difference? s1.setyear(1); s1.printinfo(); s2->setyear(2); s2->printinfo(); fs(s1); s1.printinfo(); gs(s2); s2->printinfo(); return 0; }//main