Question
I am having trouble with the bolded portion in the bottom of my Student.cpp fie. Is there a way I can write this code without
I am having trouble with the bolded portion in the bottom of my Student.cpp fie. Is there a way I can write this code without using vectors because this is too confusing for me to test in my Client.cpp. Define a class called Student.The class will have the following attributesand methods. Attributes(all need to be set to private) id(should be a constant) first name last name major gpa Constructors ... 2 overloaded 1) without any parameters 2) second constructor with all 5 attributes as parameters Behaviors(set all to public) appropriate getters and setters findStudent(int id) listStudents(double gpa) listStudents(string major) Files: Student.h //Definition of the Student class Student.cpp //Implementation of the Student class Client.cpp //Contains the main() method, through which the various Student objects will be created and tested. ToDo: - Create 5 Student objects; both of the constructors should be used at least once, Id's: 1001, 1002, 1003, 1004 and 1005 Names and gpa can be at your discetion Majors can be assigned from: Biology, Computer Science, Education, Engineering, History, Mathematics - Set up a loop to display the information(values of attributes) of all 5 objects. - Change the major of two of the Students objects.Display the change. - Change the gpa of two other Student objects.Display the change. - Test the findStudent() method by passing a valid id value. Display the appropriate message. - Test the findStudent() method by passing an invalid(non - existing) id value. Display the appropriate message. - Test both of the listStudents() methods + Pass a double value and list all students whose gpa is above the passed value. + Pass a string, and list all students whose major equals the passed value. + In either, display the appropriate message ... either found or not found.
My .h file
class Student{
public:
Student();
Student(int new_id, string new_firstName, string new_lastName, string new_major, double new_gpa);
//Getter
int getId();
string getFirstName();
string getLastName();
string getMajor();
double getGpa();
//Setter
int setId(int id);
void setFirstName( string fname);
void setLastName(string lname);
void setMajor(string m);
void setGpa(double g);
Student findStudent(int id);
void listStudents(double gpa);
void listStudents(string major);
private:
const int id;
string firstName;
string lastName;
string major;
double gpa;
};
my Student.cpp file
//Initializing variables
Student::Student(): id(0), firstName(""), lastName(""), major(""), gpa(0){}
Student::Student(int new_id, string new_firstName, string new_lastName, string new_major, double new_gpa): id(new_id), firstName(new_firstName), lastName(new_lastName), major(new_major), gpa(new_gpa){}
//Getters
int Student::getId(){
return id;
}
string Student::getFirstName(){
return firstName;
}
string Student::getLastName(){
return lastName;
}
string Student::getMajor(){
return major;
}
double Student::getGpa(){
return gpa;
}
//Setters
void Student::setFirstName(string fname){
firstName = fname;
}
void Student::setLastName(string lname){
lastName = lname;
}
void Student::setMajor(string m){
if (m == "Biology" || m == "Computer Science" || m == "Education" || m == "Engineering" || m == "History" || m == "Mathematics")
{
major = m;
}
else
{
cout << m << " is an Invalid major"<< endl;
}
}
void Student::setGpa(double g){
if (g < 0.0 || g > 4.0)
{
cout << g << " is an Invalid major. GPA has to be within 0.0 and 4.0" << endl;
return;
}
gpa = g;
}
// Code to find a student based on id
// Returns a Student object
Student Student::findStudent(int id) {
std::vector
students.push_back(*this);
for (int i = 0; i < students.size(); i++)
{
if (students[i].getId() == id) {
return students[i];
}
}
return Student();
}
// Code to list all students with gpa greater than the passed gpa value
// Displays appropriate message if no students are found
void Student::listStudents(double gpa) {
std::vector
students.push_back(*this);
for (int i = 0; i < students.size(); i++) {
if (students[i].getGpa() >= gpa) {
std::cout << "Student ID: " << students[i].getId() << " Name: " << students[i].getFirstName() << " "
<< students[i].getLastName() << " Major: " << students[i].getMajor() << " GPA: "
<< students[i].getGpa() << std::endl;
}
}
}
// Code to list all students with same major as one passed
// Displays appropriate message if no students are found
void Student::listStudents(string major) {
std::vector
students.push_back(*this);
for (int i = 0; i < students.size(); i++) {
if (students[i].getMajor() == major) {
std::cout << "Student ID: " << students[i].getId() << " Name: " << students[i].getFirstName() << " "
<< students[i].getLastName() << " Major: " << students[i].getMajor() << " GPA: "
<< students[i].getGpa() << std::endl;
}
}
}
My Client.cpp file
int main(){
//objects
Student student1(1001, "Mary", "Smith", "Computer Science", 3.1);
Student student2(1002, "Emma", "Greek", "Education", 4.0);
Student student3(1003, "John", "Salvator", "Biology", 3.9);
Student student4(1004, "Ricky", "Delhunty", "History", 3.8);
Student student5(1005, "Olive", "Jones", "Computer Science", 1.7);
//display student information
//changing the major
cout << student2.getFirstName() << "'s orginal major: " << student2.getMajor() << endl;
student2.setMajor("History");
cout << student2.getFirstName() << "'s new major: " << student2.getMajor() << endl << endl;
cout << student5.getFirstName() << "'s orginal major: " << student5.getMajor() << endl;
student5.setMajor("Biology");
cout << student5.getFirstName() << "'s new major: " << student5.getMajor() << endl << endl;
//changing the gpa
cout << student1.getFirstName() << "'s orginal gpa: " << student1.getGpa() << endl;
student1.setGpa(3.9);
cout << student1.getFirstName() << "'s new gpa: " << student1.getGpa() << endl << endl;
cout << student5.getFirstName() << "'s orginal gpa: " << student5.getGpa() << endl;
student5.setGpa(2.0);
cout << student5.getFirstName() << "'s new gpa: " << student5.getGpa() << endl << endl;
// Test findStudent method
// Test listStudents method with gpa
// Test listStudents method with major
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started