Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Forked from drpventura/Student.cpp
Last active April 4, 2021 19:16
Show Gist options
  • Save MonteLogic/e6d775ee04dc472a8bbe474dc26d2532 to your computer and use it in GitHub Desktop.
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.
#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;
}
#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;
}
#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