In: Computer Science
This should be written 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:
Enter size of class: 0 Invalid class size Enter size of class: 1 Enter the roll number of student: 45 Enter The Name of student: Trish Duce Enter the grade in Algorithms out of 100: 88 Enter the grade in Data Structures out of 100: 94 Enter the grade in Architecture out of 100: 98 Enter the grade in Programming Languages out of 100: 92 Roll number of student: 45 Name of student: Trish Duce Grade in Algorithms: 88 Grade in Data Structures: 94 Grade in Architecture: 98 Grade in Programming Languages: 92 Percentage of student is: 93 Grade of student is: A
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
// Creating the Student class
class Student
{
private:
// Declaring variable
int rollno;
string name;
int alg, datastruct, architect, proglang;
float per;
char grade;
public:
// Parameterized constructor
Student()
{
this->rollno = 0;
this->name = "NoName";
this->alg = 0;
this->datastruct = 0;
this->architect = 0;
this->proglang = 0;
this->per = 0.0;
this->grade = ' ';
}
// function declarations
void getData();
void showdata();
// Setters and getters
int getRollno()
{
return rollno;
}
void setRollno(int rollno)
{
this->rollno = rollno;
}
string getName()
{
return name;
}
void setName(string name)
{
this->name = name;
}
int getAlg()
{
return alg;
}
void setAlg(int alg)
{
this->alg = alg;
}
int getDatastruct()
{
return datastruct;
}
void setDatastruct(int datastruct)
{
this->datastruct = datastruct;
}
int getArchitect()
{
return architect;
}
void setArchitect(int architect)
{
this->architect = architect;
}
int getProglang()
{
return proglang;
}
void setProglang(int proglang)
{
this->proglang = proglang;
}
float getPer()
{
return per;
}
void setPer(float per)
{
this->per = per;
}
char getGrade()
{
return grade;
}
void setGrade(char grade)
{
this->grade = grade;
}
};
/* Function implementatin which
* gets the data entered by the user
*/
void Student::getData()
{
double average = 0.0, sum = 0.0;
cout << "Enter the roll number of student: ";
cin >> this->rollno;
cin.ignore();
cout << "Enter The Name of student:";
std::getline(std::cin, this->name);
cout << "Enter the grade in Algorithms out of 100: ";
cin >> this->alg;
cout << "Enter the grade in Data Structures out of 100:
";
cin >> this->datastruct;
cout << "Enter the grade in Architecture out of 100: ";
cin >> this->architect;
cout << "Enter the grade in Programming Languages out of 100:
";
cin >> this->proglang;
sum += this->getAlg() + this->getDatastruct() +
this->getArchitect() + this->getProglang();
average = sum / 4;
this->setPer(average);
if (getPer() >= 90 && getPer() <= 100)
this->setGrade('A');
else if (getPer() >= 80 && getPer() < 90)
this->setGrade('B');
else if (getPer() >= 70 && getPer() < 80)
this->setGrade('C');
else if (getPer() >= 60 && getPer() < 70)
this->setGrade('D');
else if (getPer() < 60)
this->setGrade('F');
}
// Function which displays the Student's data
void Student::showdata()
{
cout << "Roll number of student: " <<
this->getRollno() << endl;
cout << "Name of student: " << this->getName()
<< endl;
cout << "Grade in Algorithms: " << this->getAlg()
<< endl;
cout << "Grade in Data Structures: " <<
this->getDatastruct() << endl;
cout << "Grade in Architecture: " <<
this->getArchitect() << endl;
cout << "Grade in Programming Languages: " <<
this->getProglang() << endl;
cout << "Percentage of student is: " <<
this->getPer() << endl;
cout << "Grade of student is: " << this->grade
<< endl;
}
int main()
{
// Declaring vector
vector<Student> vec;
// Declaring variables
int size;
// Getting the valid size entwered by the user
while (true)
{
cout << "Enter the size of the class :";
cin >> size;
if (size <= 0)
{
cout << "Invalid class size" << endl;
continue;
}
else
break;
}
/* Getting the data entered by user
* by calling the getData() function
* on the Student class
*/
for (int i = 0; i < size; i++)
{
Student st;
st.getData();
vec.push_back(st);
}
cout << endl;
/* Displaying the data entered by user
* by calling the showData() function
* on the Student class
*/
for (int i = 0; i < size; i++)
{
vec[i].showdata();
}
return 0;
}
_________________
output:

_______________Thank You