#include #include using namespace std; //Class Demo: create a student class. class student { //Can only be accessed by methods inside of the class private: int exam1; int exam2; int exam3; //Anybody can access this stuff public: string name; int id; char grade; //Default constructor student() { name = "nobody"; exam1 = 0; exam2 = 0; exam3 = 0; grade = 'F'; } student(string givenName) { name = givenName; exam1 = 15; exam2 = 0; exam3 = 0; } student(string givenName, int s1, int s2) { name = givenName; exam1 = s1; exam2 = s2; exam3 = 0; } void shoutOut() { cout << "Hi, my name is " << name << ", I scored " << computeAverage() << endl; } double computeAverage() { double avg = (exam1 + exam2 + exam3) / 3.0; return avg; } //Add points to given exam score void extraCredit(int examNum, int points) { if (examNum == 1) exam1 += points; else if (examNum == 2) exam2 += points; else if (examNum == 3) exam3 += points; } void setExam(int examNum, int points) { if (points < 0) cout << "Stop trying to do crazy stuff" << endl; else if (examNum == 1) exam1 = points; else if (examNum == 2) exam2 = points; else if (examNum == 3) exam3 = points; } //Copy exam scores. Get caught 70 chance. void cheatOff(student victim) { if ( rand() % 100 > 70)//get away with it { exam1 = victim.exam1; exam2 = victim.exam2; exam3 = victim.exam3; } else//got caught { cout << "You're busted, " << name << endl; exam1 = 0; exam2 = 0; exam3 = 0; victim.exam3 -= 50; } } }; int main() { student nancy; student roger; student sam; student juan("Juan"); student jose("Jose", 65, 72); //access the attributes of these objects nancy.name = "Nancy"; roger.name = "Roger"; sam.name = "Samwise"; nancy.id = 543; roger.id = 600; sam.id = 714; cout << nancy.id << endl; //543 cout << sam.id << endl; //714 nancy.setExam(1,40); nancy.setExam(2,50); nancy.setExam(3,93); juan.setExam(2, -5000); double averageScore; averageScore = nancy.computeAverage(); //Give nancy +10 points on exam 1 nancy.extraCredit(1,10); roger.extraCredit(3, -500); roger.setExam(1,-2000); student maria("Maria"); maria.cheatOff(nancy); roger.cheatOff(maria); sam.cheatOff(roger); nancy.shoutOut(); roger.shoutOut(); sam.shoutOut(); juan.shoutOut(); jose.shoutOut(); return 0; }