-
-
Save MonteLogic/e6d775ee04dc472a8bbe474dc26d2532 to your computer and use it in GitHub Desktop.
Student class moved to separate .h and .cpp files. See video at https://youtu.be/gyA7uDlazkc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Student.h" | |
#include <iostream> | |
using namespace std; | |
int main() { | |
Student s1 {"Cecelia", 3.8 }; | |
Student s2 {"Sam", 3.1}; | |
cout << s1 << endl; | |
cout << s2 << endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Student.h" | |
using namespace std; | |
// This is the constructor. | |
Student::Student(string theName, double theGpa) : name(theName), gpa(-1) { | |
set_gpa(theGpa); | |
} | |
// accessor | |
string Student::get_name() const { | |
return name; | |
} | |
// mutator | |
void Student::set_gpa(double newGpa) { | |
if (newGpa >= 0 && newGpa <= 4.0) { | |
gpa = newGpa; | |
} | |
} | |
ostream& operator<<(ostream& ostr, const Student& stud) { | |
ostr << stud.get_name() << ", " << stud.gpa; | |
return ostr; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STUDENTDB_STUDENT_H | |
#define STUDENTDB_STUDENT_H | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
class Student { | |
//private: | |
string name; | |
double gpa; | |
public: | |
Student(string theName, double theGpa); | |
// accessor | |
string get_name() const; | |
// mutator | |
void set_gpa(double newGpa); | |
friend ostream& operator<<(ostream& ostr, const Student& stud); | |
}; | |
ostream& operator<<(ostream& ostr, const Student& stud); | |
#endif //STUDENTDB_STUDENT_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment