Please write in C++ Create a class with the name \"Student\". private data membe
ID: 3806253 • Letter: P
Question
Please write in C++
Create a class with the name "Student".
private data members of the Student class should include:
int - rollno (roll number or id number of student)
string - name (name of student)
int - alg, datastruct, architect, proglang (hold scores out of 100 for these 4 classes)
float - per (average score of 4 classes above)
char - grade (letter grade based on per.. example 90 is an A)
public member functions of the Student class should include:
getdata() (function to accept data from user
showdata() (function to show data on screen
Create a constructor that initializes all int to 0, float to 0.0, char to ' ', name = "NoName".
Prompt the user for a valid class size.
Prompt the user for student data.
Store students in a vector.
Display student data including students average score and letter grade.
The following is an example:
This should be done in C++
Explanation / Answer
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
class Student
{
private:
int rollno;
string name;
int alg,datastruct, architect, proglang;
float per;
char grade;
public:
Student() //default constructor
{
rollno = 0;
name = "NoName";
alg=datastruct=architect=proglang= 0;
per = 0.0;
grade = ' ';
}
void getdata() //input student data
{
cout<<" Enter the roll number of student: ";
cin>>rollno;
cin.ignore(); //to take care of character arrays
cout<<" Enter The Name of student: ";
getline(cin,name);
cout<<" Enter the grade in Algorithms out of 100: ";
cin>>alg;
cout<<" Enter the grade in Data Structures out of 100: ";
cin>>datastruct;
cout<<" Enter the grade in Architecture out of 100: ";
cin>>architect;
cout<<" Enter the grade in Programming Languages out of 100: ";
cin>>proglang;
}
void showdata() //display student data
{
cout<<" Roll number of student: "<<rollno;
cout<<" Name of student: "<<name;
cout<<" Grade in Algorithms: "<<alg;
cout<<" Grade in Data Structures: "<<datastruct;
cout<<" Grade in Architecture: "<<architect;
cout<<" Grade in Programming Languages: "<<proglang;
int per = (alg+datastruct+architect+proglang)/4; //calculate percentage
//calculate Grade
if(per > 80 && per <= 100)
grade = 'A';
else if(per > 60 && per <= 80)
grade = 'B';
else if(per > 40 && per <= 60)
grade = 'C';
else if(per > 0 && per <= 40)
grade = 'F';
cout<<" Percentage of student is: "<<per;
cout<<" Grade of student is: "<<grade;
}
};
int main()
{
vector<Student> list; //declare vector list of type Student
int i,n;
Student *s; //pointer to student object
cout<<"Enter size of class: ";
cin>>n;
if(n<=0)
{
cout<<" Invalid class size";
cout<<" Enter size of class: ";
cin>>n;
}
for(i=0;i<n;i++)
{
s = new Student;
s->getdata();
list.push_back(*s); // push each object into the list
}
// setup an iterator loop through the vector
vector<Student>::iterator it;
for ( it = list.begin(); it != list.end(); ++it ) {
// For each Student, print out his info
it->showdata();
}
return 0;
}
output: