Skip to content

Instantly share code, notes, and snippets.

@drpventura
Last active March 28, 2023 04:24
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save drpventura/364de9d10aa8b445e0a0ba7cd7aebdf7 to your computer and use it in GitHub Desktop.
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.
#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;
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
@BigDog044
Copy link

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!

@Djole01
Copy link

Djole01 commented Apr 24, 2020

Thank you, great video and explanation.

@IamDanishIrfan
Copy link

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