A program that uses a Student class with the following member variables and memb
ID: 3891866 • Letter: A
Question
A program that uses a Student class with the following member variables and member functions: Private member variables Member name Type Description name string Student name idNum int Student ID number GPA double GPA Public member functions Mutators setName: Takes as argument the student's name as a string and sets the name member variable to that string setIdNum: Takes as argument the student's Id number as an int and sets the Idnum member variable to that value. Performs input validation: If the parameter value is not > 0, displays "Invalid Id number" error message and exits the program with EXIT_FAILURE. setGPA: Takes as argument the student's GPA as a double and sets the GPA member variable to that value. Performs input validation: If the GPA value is not in the range from 0 to 4 inclusive, displays "invalid GPA" error message and exits the program with EXIT_FAILURE. Accessors getName: returns the name getIdNum: returns the Idnum getGPA: returns the GPA The program should ask the user how many students there are. It should then dynamically allocate an array of Student. After the array has been dynamically allocated, the program should ask for the name, ID number and GPA for each student and populate the array. Then the content of the array is displayed. Don't use a constructor. Additional Requirements a) Outline of main Your main function must demonstrate your class is working correctly by implementing this pseudo code: Prompt the user and get the number of students Dynamically allocate an array of Student, with the size equal to the number of students Loop over the students in the array and for each student do the following Prompt the user to input name, ID number and GPA Call the appropriate mutators to populate the name, ID number and GPA of the student Loop over the students in the array and for each student do the following Call the appropriate accessors and display the name, ID number and GPA b) Separation of header file and implementation file All member functions must be in the implementation file (no in-line functions). You should turn in - main.cpp - Student.cpp - Student.h c) Use of const Use const for the member functions when appropriate
Explanation / Answer
Below are the required files I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
Student.h -
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student
{
private:
//instance member variables
int ID;
string name;
double GPA;
public:
//declaration of instance member function
void setName(string);
void setId(int);
void setGPA(double);
int getID();
string getName();
double getGPA();
void print();
};
#endif
Student.cpp -
#include "Student.h"
#include <iostream>
using namespace std;
void Student :: setName(string s)
{
name=s;
}
void Student :: setId(int n)
{
//check for correctness of ID
if(n<=0)
{
//display error message
cout<<"Invalid Id number"<<endl;
//terminate the program
exit(0);
}
ID=n;
}
void Student :: setGPA(double d)
{
//check for correctness of GPA
if(GPA<0 || GPA>4)
{
//display error message
cout<<"Invalid GPA"<<endl;
//terminate the program
exit(0);
GPA=d;
}
int Student :: getID()
{
return ID;
}
string Student :: getName()
{
return name;
}
double Student :: getGPA()
{
return GPA;
}
void Student :: print()
{
//print information of student
cout << "ID : " << ID << endl;
cout << "Name : " << name << endl;
cout << "GPA : " << GPA << endl;
}
main.cpp -
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
Student *s;
int ,in;
//Take user's input
cout<<"Enter number of students : ";
cin>>n;
//dynamically allocate array
s=new Student[n];
int ID;
string name;
double GPA;
char gender;
//set student's information
for(i=0;i<n;i++)
{
cout<<"For student "<<i+1<<endl;
cout << "Enter ID : ";
cin >> ID;
cout << "Enter name : ";
cin >> name;
cout << "Enter GPA : ";
cin >> GPA;
s[i].setName(name);
s[i].setId(ID);
s[i].setGPA(GPA);
}
//print every student's information
for(i=0;i<n;i++)
{
s[i].print();
}
return 0;
}
Hope i have answered your question satisfactorily.Leave doubts in comment section if any.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.