#include #include using namespace std; class student { public: string name; int id; double exam1; double exam2; double exam3; //default constructor: special function/method //that is called when you create a new student //variable. Constructors must have same name //as class. student() { cout << "Calling the default constructor!" << endl; name = "john doe"; id = 0; exam1 = 0; exam2 = 0; exam3 = 0; } //parameterized constructor: student(string n) { cout << "Ooooh, calling a parameterized constructor!!!" << endl; name = n; id = 13; exam2 = 50.3; } //Each parameterized constructor must have a unique parameter list. //The constructor that is called is decided by matching the given //parameters to a suitable constructor. student(string somename, double x1, double x2, double x3, string phrase) { cout << phrase << endl; name = somename; exam1 = x1; exam2 = x2; exam3 = x3; if (exam1 < 90) { cout << "Hey " << name << ", you really need to hit the books..." << endl; } } //A basic method void sayHello() { cout << "Hi! My name is " << name << " and my id is " << id << endl; if (exam1 < 80) { cout << "I'm having trouble in this class.." << endl; } else { cout << "I'm doing pretty good in this class!!!" << endl; } } //This methods takes some parameters and //changes some variables of the object void setExamScore(int examNum, double newScore) { if (examNum == 1) exam1 = newScore; if (examNum == 2) exam2 = newScore; if (examNum == 3) exam3 = newScore; } //A method with a return type bool passed() { if ((exam1 + exam2 + exam3)/3 >= 70) return true; else return false; } }; //A normal function that takes a student as a parameter double computeAvg(student X) { return (X.exam1 + X.exam2 + X.exam3) / 3; } int main() { //quiz: write a student class so that the following //code works as described. student A; student B; student C("roger"); student D("tami"); student E("lucy", 51.3, 70.9, 72, "Hey hey hey, print this phrase!!!"); student F("tommy", 95, 70.9, 72, "let's see what happens"); A.name = "Julia"; A.id = 1234; A.exam1 = 83.5; A.exam2 = 76.2; A.exam3 = 92.1; //Let's make a method that does something to the student E.setExamScore(1, 80.5); F.setExamScore(3, 0); //cheated C.setExamScore(2, 85); cout << A.name << ", " << A.id << ", " << computeAvg(A) << endl; // Julia, 1234, 83.93 cout << B.name << ", " << B.id << ", " << computeAvg(B) << endl; // cout << C.name << ", " << C.id << ", " << computeAvg(C) << endl; // cout << D.name << ", " << D.id << ", " << computeAvg(D) << endl; // cout << E.name << ", " << E.id << ", " << computeAvg(E) << endl; // cout << F.name << ", " << F.id << ", " << computeAvg(F) << endl; // //Let's add a basic method to the student class. A.sayHello(); C.sayHello(); E.sayHello(); //A method with a return type if (A.passed()) cout << A.name << " passed the course!" << endl; else cout << A.name << " failed!" << endl; return 0; }