You are to create a C++ program that collects student information for your class
ID: 3624198 • Letter: Y
Question
You are to create a C++ program that collects student information for your classmates. The program will require the following data to be gathered:
-First Name (e.g., Mergim)
-Last Name (e.g., Cahani)
-GPA (e.g., 3.43)
-Enrollment Year (e.g., 2009)
-Graduation Year (e.g., 2012)
Do not create more than one array for this assignment. You are expected to create a class, name it students and add class members for first name, last name, gpa, etc…
You should also have methods (functions) that set those data members to a given value, e.g., setfname, getfname, setlname, getlname, etc. some functions are VOID such as setfname, while others are returning value functions, such as getfname (which is a string returning value).
HINTS:
- Declare variables (properties) as PRIVATE
- Declare functions (methods) as PUBLIC
- Declare an array of the data type which you defined with the class, e.g., if your class name is “students” then the array declaration would have the following syntax:
- “students studentsArray[x]” where x is the number of elements (or class members – you can come up with a number, say 10), “students” is the data type, and studentsArray is the array name. Once you have this array you can use a for loop to populate the array, then use another for loop to output the array.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
class Student
{private:
string fname;
string lname;
double gpa;
int enroll;
int grad;
public:
Student()
{}
void setfname(string f)
{fname=f;}
void setlname(string l)
{lname=l;}
void setgpa(double g)
{gpa=g;}
void setenroll(int e)
{enroll=e;}
void setgrad(int g)
{grad=g;}
string getfname()
{return fname;}
string getlname()
{return lname;}
double getgpa()
{return gpa;}
int getenroll()
{return enroll;}
int getgrad()
{return grad;}
};
void display(Student[],int);
int main()
{int i,n;
string s;
double d;
int j;
Student a[10];
cout<<"How many students do you have data for? ";
cin>>n;
for(i=0;i<n;i++)
{cout<<"For Student "<<i+1<<endl;
cout<<"Enter first name: ";
cin>>s;
a[i].setfname(s);
cout<<"Enter last name: ";
cin>>s;
a[i].setlname(s);
cout<<"Enter gpa: ";
cin>>d;
a[i].setgpa(d);
cout<<"Enter enrollment date: ";
cin>>j;
a[i].setenroll(j);
cout<<"Enter graduation date: ";
cin>>j;
a[i].setgrad(j);
}
display(a,n);
system("pause");
return 0;
}
void display(Student a[],int n)
{ cout<<"Student Information ";
int i;
for(i=0;i<n;i++)
{cout<<"First name: "<<a[i].getfname()<<endl;
cout<<"Last name: "<<a[i].getlname()<<endl;
cout<<"gpa: "<<a[i].getgpa()<<endl;
cout<<"Enrollment year: "<<a[i].getenroll()<<endl;
cout<<"Graduation year: "<<a[i].getgrad()<<endl<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.