Presentation is loading. Please wait.

Presentation is loading. Please wait.

Declaring a Class: the.h file #pragma once #include using namespace std; class Student { private: string myFirst; string myLast; int myCredits; double.

Similar presentations


Presentation on theme: "Declaring a Class: the.h file #pragma once #include using namespace std; class Student { private: string myFirst; string myLast; int myCredits; double."— Presentation transcript:

1 Declaring a Class: the.h file #pragma once #include 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); };

2 Implementing a class: the.cpp file #include "Student.h" #include 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"; }

3 .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; }

4 .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"; }

5 .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; }

6 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.

7 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)).

8 #include "Student.h" #include 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; } Default constructor in action

9 Assign Class Year from Credits CreditsClass Year >= 92 Senior >= 60 Junior >= 28 Sophomore 0-27 Freshman

10 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

11 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

12 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"; }

13 #include "Student.h" #include 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; } Initializing Constructor in Action

14 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. Using the Initializing Constructor

15 #include "Student.h" #include 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; } Initializing Constructor in Action

16 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?

17 toString Method Write public method inside class Student Pre-conditions: None Post-conditions: prints out the value of each Student attribute with appropriate messages.

18 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; }

19 toString in action #include "Student.h" #include using namespace std; int main () { Student joe(“Blow”,"Joe", 32, 3.92); cout << joe.toString(); }

20 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. addCredits Method

21 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"; }

22 #include "Student.h" #include using namespace std; int main () { Student joe(“Blow”,"Joe", 32, 3.92); joe.addCredits(16); cout << joe.toString(); } Using addCredits Method


Download ppt "Declaring a Class: the.h file #pragma once #include using namespace std; class Student { private: string myFirst; string myLast; int myCredits; double."

Similar presentations


Ads by Google