Last active
March 28, 2023 04:24
-
-
Save drpventura/364de9d10aa8b445e0a0ba7cd7aebdf7 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 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 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; | |
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 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 |
Thank you, great video and explanation.
are we only uses classes in header file or we can use also functions?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for explaining everything to an old man of 55 who just likes to learn. My body is worn but i am trying to keep my mind working. Again thank you for your videos and God Bless You!