220 likes | 327 Views
Declaring a Class: the .h file. #pragma once #include <string> using namespace std; class Student { private: string myFirst; string myLast; int myCredits; double myGPA; string myClassYear; public: Student(void);
E N D
Declaring a Class: the .h file #pragma once #include <string> using namespace std; class Student { private: string myFirst; string myLast; int myCredits; double myGPA; string myClassYear; public: Student(void); Student(string, int, double); ~Student(void); void setMyFirst(string); void setMyLast(string); void setMyGpa(double); void setMyClassYear(string); string getMyFirst(void); string getMyLast(void); int getMyCredits(void); double getMyGPA(void); string getMyClassYear(void); void addCredits(int); string toString(void); };
Implementing a class:the .cpp file #include "Student.h" #include <sstream> Student::Student(void) { myName = "None"; myCredits = 0; myGPA = 0.0; myClassYear = "None"; } Student::Student(string fisrt, string last, int credits, double gpa) { myFirst = first; myLast = last; myCredits = credits; myGPA = gpa; if (myCredits >= 92) myClassYear = "Senior"; else if (myCredits >= 60) myClassYear = "Junior"; else if (myCredits >= 28) myClassYear = "Sophomore"; else myClassYear = "Freshman"; }
.cpp file continued void Student::setMyFirst(string first) { myFirst = first; } void Student::setMyLast(string last) { myLast = last; } void Student::setMyGpa(double gpa) { myGPA = gpa; } void Student::setMyClassYear(string year) { myClassYear = year; } string Student::getMyFirst(void) { return myFirst; } String Student::getMyLast(void) { return myLast; } int Student::getMyCredits(void) { return myCredits; } double Student::getMyGPA(void) { return myGPA; } string Student::getMyClassYear(void) { return myClassYear; }
.cpp file continued • void Student::addCredits(int credits) • { • if (credits > 0) • { • myCredits = myCredits + credits; • if (myCredits >= 92) • myClassYear = "Senior"; • else if (myCredits >= 60) • myClassYear = "Junior"; • else if (myCredits >= 28) • myClassYear = "Sophomore"; • else • myClassYear = "Freshman"; • } • }
.cpp continued string Student::toString(void) { stringstream credits, gpa; credits << myCredits; gpa << myGPA; string c, g; c = credits.str(); g = gpa.str(); string stuString = "Name: " + myLast + “, “ + myFirst + "\nCredits: " + c + "\nGPA: " + g + "\nClass: " + myClassYear + "\n"; return stuString; }
Constructor Method • A constructor is a special method that assigns initial values to instance variables. 1. Constructors must have the same name as the class itself. 2. Constructors do not have a return type—not even void. 3. Constructors are invoked when an object is created. • The constructor is automatically called whenever an object is created.
Student’s Default Constructor Student::Student(void) { myFirst = "None"; myLast = “None”; myCredits = 0; myGPA = 0.0; myClassYear = "None"; } A class normally provides a constructor without formal parameters (arguments) (e.g., Student(void)).
Default constructor in action • #include "Student.h" • #include <iostream> • #include <string> • using namespace std; • int main () • { • Student sue; • cout << "Name " << sue.getMyLast() • << endl; • cout << "Credits " • << sue.getMyCredits() << endl; • cout << "GPA " << sue.getMyGPA() • << endl; • cout << "Year " • << sue.getMyClassYear() << endl; • return 0; • }
Assign Class Year from Credits Credits Class Year >= 92 Senior >= 60 Junior >= 28 Sophomore 0-27 Freshman
Initializing Constructor • This version of the constructor method receives parameters that are used to set the initial instance variable values. • For class Student: pre-conditions: receives: • String for setting name • int for setting credit hours earned • double for setting GPA
Initializing Constructor • Add this constructor after the default Student() constructor method. • The name of this method is also Student • Pre-conditions: Receives • string for specifying name • int for giving the credit hours earned • double for giving the GPA
Student’s Initializing Constructor Student::Student(string last, string first, int credits, double gpa) { myFirst = first; myLast = last; myCredits = credits; myGPA = gpa; if (myCredits >= 92) myClassYear = "Senior"; else if (myCredits >= 60) myClassYear = "Junior"; else if (myCredits >= 28) myClassYear = "Sophomore"; else myClassYear = "Freshman"; }
Initializing Constructor in Action #include "Student.h" #include <iostream> #include <string> using namespace std; int main () { Student sue("Doe","Sue", 16, 4.0); cout << "Name " << sue.getMyLast() << “, “ << sue.getMyFirst << endl; cout << "Credits " << sue.getMyCredits() << endl; cout << "GPA " << sue.getMyGPA() << endl; cout << "Year " << sue.getMyClassYear() << endl; return 0; }
Using the Initializing Constructor • Declare another Student object named Joe. • Use the initializing constructor to create and initialize instance variables for your new Student with a name of “Joe”, credits of 60 and a gpa of 3.8. • Use accessors to retrieve and print out all of its instance variable values.
Initializing Constructor in Action #include "Student.h" #include <iostream> #include <string> using namespace std; int main () { Student joe(“Blow”,"Joe", 32, 3.92); cout << "Name " << joe.getMyLast() << “, “ << joe.getMyFirst << endl; cout << "Credits " << joe.getMyCredits() << endl; cout << "GPA " << joe.getMyGPA() << endl; cout << "Year " << joe.getMyClassYear() << endl; return 0; }
Printing a Student’s Data • Wouldn’t it be easier if our main didn’t have to call four “getter” methods and do four cout commands to get and display a Student’s information. • What could we add to class Student to help save us from doing all these steps?
toString Method • Write public method inside class Student • Pre-conditions: None • Post-conditions: prints out the value of each Student attribute with appropriate messages.
toString Method string Student::toString(void) { stringstream credits, gpa; credits << myCredits; gpa << myGPA; string c, g; c = credits.str(); g = gpa.str(); string stuString = "Name: " + myLast + “, “ + myFirst + "\nCredits: " + c + "\nGPA: " + g + "\nClass: " + myClassYear + "\n"; return stuString; }
toString in action #include "Student.h" #include <iostream> #include <string> using namespace std; int main () { Student joe(“Blow”,"Joe", 32, 3.92); cout << joe.toString(); }
addCredits Method • Pre-condition: Receives an integer value giving number of credits earned. • Post-conditions: If given number of credits is > 0, then add that number of credits to the Student’s credits instance variable. Returns updated number of credit hours.
Student’s addCredits Method void Student::addCredits(int credits) { if (credits > 0) { myCredits = myCredits + credits; if (myCredits >= 92) myClassYear = "Senior"; else if (myCredits >= 60) myClassYear = "Junior"; else if (myCredits >= 28) myClassYear = "Sophomore"; else myClassYear = "Freshman"; } }
Using addCredits Method #include "Student.h" #include <iostream> #include <string> using namespace std; int main () { Student joe(“Blow”,"Joe", 32, 3.92); joe.addCredits(16); cout << joe.toString(); }