#include #include using namespace std; class student { public: //class variables are sometimes called "attributes" string name; int id; double exam1; double exam2; double exam3; //Here is a (default) constructor //The code in here will run when you //declare a student variable //Constructors are usually used to inialize class variables. student() { cout << "Hey, someone is declaring a student variable!" << endl; name = "noname"; id = 0; exam1 = 0; exam2 = 0; exam3 = 0; } //Here is a parameterized constructor student(string initialName, int initialID) { cout << "Ooooh, we're calling a parameterized consturcotr this time" << endl; name = initialName; id = initialID; exam1 = 10; exam2 = 20; exam3 = 30; } //Can have multiple parameterized constructors student(string initialName, string funSentence, int initialID) { cout << funSentence << endl; name = initialName; id = initialID; exam1 = 90; exam2 = 100; } //Methods are just functions attached to a class. //In Object Oriented Programming we often use the term "method" //instead of "function inside a class". //A basic method void sayHi() { cout << "Hi, my name is " << name << " and my exam1 score is " << exam1 << endl; } //Here is a method that returns a value double computeGrade() { double sum = exam1 + exam2 + exam3; double avg = sum / 3; return avg; } //Here is a method with parameters void setScore(int examNum, double newScore) { if (examNum == 1) { exam1 = newScore; } else if (examNum == 2) { exam2 = newScore; } else if (examNum == 3) { exam3 = newScore; } else { //do nothing, they gave a weird number cout << "Hey, no such exam number dude!" << endl; } } //Oh no, the students now have cheat method! //They're going to get caught and flunk... void cheatOffof(student &victim) { if ( rand()%2 == 0 ) //caught! { cout << name << " has been caught cheating from " << victim.name << endl; exam1 = 0; exam2 = 0; exam3 = 0; //hmmm... they might have been colluding //should also punish the victim //Reduce victim's scores by 10 points victim.exam1 = victim.exam1 - 10; victim.exam2 = victim.exam2 - 10; victim.exam3 = victim.exam3 - 10; } else { //They got way with it...Ok, copy scores! exam1 = victim.exam1; exam2 = victim.exam2; exam3 = victim.exam3; } } }; int main() { student A; student B("Belinda", 4721); student C; student D("Lucy", 9388); student F("Fonzi", "eeeeehhhhhhh... have HAPPY DAY!!!", 1783); student E("Bart", "Don't have a cow man!", 1234); A.name = "Steven"; cout << A.name << " " << A.id << " " << A.exam1 << endl; cout << B.name << " " << B.id << " " << B.exam1 << endl; cout << C.name << " " << C.id << " " << C.exam1 << endl; cout << D.name << " " << D.id << " " << D.exam1 << endl; cout << F.name << " " << F.id << " " << F.exam1 << endl; cout << E.name << " " << E.id << " " << E.exam1 << endl; //Methods! B.sayHi(); F.sayHi(); E.sayHi(); B.exam1 = 5; B.exam2 = 17; B.exam3 = 54; //methods can also take parameters, just like normal functions D.setScore(2, 95); D.setScore(3, 85); B.setScore(2, 15); E.setScore(1, 99); //Ok, let's do a method that returns a value cout << B.name << " got a grade of " << B.computeGrade() << endl; cout << D.name << " got a grade of " << D.computeGrade() << endl; cout << E.name << " got a grade of " << E.computeGrade() << endl; //Let's do something interesting //Uh oh... shenanigans! They might get caught... B.cheatOffof(E); F.cheatOffof(D); A.cheatOffof(B); C.cheatOffof(F); D.cheatOffof(A); E.cheatOffof(D); cout << B.name << " got a grade of " << B.computeGrade() << endl; cout << F.name << " got a grade of " << F.computeGrade() << endl; cout << E.name << " got a grade of " << E.computeGrade() << endl; cout << D.name << " got a grade of " << D.computeGrade() << endl; cout << A.name << " got a grade of " << A.computeGrade() << endl; cout << C.name << " got a grade of " << C.computeGrade() << endl; return 0; }