Student.cpp
Initializer lists
You should use initializer lists in place of these constructors. They offer some advantages, such as being able to initialize const
members. Normal constructors do not allow that.
The initializer list is in this form:
Class::Class() : member(value) /* additional members separated by commas */ {}
Here's what it'll look like with the overloaded constructor:
Student::Student(int ID, std::string const& name, double GPA, char gender)
: ID(ID)
, name(name)
, GPA(GPA)
, gender(gender)
{}
You should then apply the same idea to the other constructor.
Overloading operator<<
Consider overloading operator<<
for Student
so that it'll be easier to output.
Declare it in the header:
friend std::ostream& operator<<(std::ostream& out, Student const& obj);
Define it in the implementation:
std::ostream& operator<<(std::ostream& out, Student const& obj)
{
out << "ID: " << obj.ID << "\n";
out << "Name: " << obj.name << "\n";
out << "GPA: " << obj.GPA << "\n";
out << "Gender: " << obj.gender << "\n";
return out;
}
Overloading operator>>
You can also do the same with operator>>
to make inputting cleaner.
Declare it in the header:
friend std::istream& operator>>(std::istream& in, Student& obj);
Define it in the implementation:
std::istream& operator>>(std::istream& in, Student& obj)
{
return in >> obj.ID >> obj.name >> obj.GPA >> obj.gender;
}
Regarding name
: you should use std::getline()
instead of std::cin >>
so that spaces can be properly handled (it's also preferred in general for inputting into an std::string
). However, it'll require a call to std::ignore()
as you cannot just mix both forms of input.
StudentDemp.cpp
You should construct the Student
object instead of using a setter.
If you keep your user inputs as-is, this is how the construction should look:
Student s(ID, name, GPA, gender);
But if you decide to use operator>>
, you just need the default Student
:
Student s;
and your input will now look like this:
std::cout << "Enter student information (ID, name, GPA, gender):";
std::cin >> s;
With the latter, you should then remove the other variables from the top of the function.
Final notes:
Please do not use
using namespace std
in so many places. It can cause name-clashing issues, especially in larger programs. While it is okay to use it in a lower scope (such as a function), it's especially worse to use it in a header file. Any file including the header will be forced to use it, which could cause bugs. Read this for more information.Declare variables as close in scope as possible to help ease maintainability. This is apparent with your variables declared at the top of
main()
.Avoid setters and getters as much as possible. They can break encapsulation as they expose the internals of the class. In this particular program, you won't even need them if you're just displaying values. Remove them all and it should make the code cleaner.