student.h
const int subjects = 6;
class student {
private:
int stud_id, stud_level;
double stud_scores[subjects];
string name;
public:
student();
void setStudentData(int id, int level, string n, double score[]);
double GPA();
void print();
};
student.cpp
#include<iostream>
#include"student.h"
using namespace std;
student::student()
{
stud_id = NULL;
stud_level = NULL;
name = "";
for (int i = 0; i < subjects; i++)
stud_scores[i] = 0;
}
void student::setData(int id, int level, string n, double score[])
{
stud_id = id;
stud_level = level;
name = n;
for (int i = 0; i < subjects; i++)
stud_scores[i] = score[i];
}
double student::GPA()
{
double gpa[subjects];
double sumGpa = 0;
for (int i = 0; i < subjects; i++)
{
if (stud_scores[i] >= 90) gpa[i] = 4;
else if (stud_scores[i] >= 85) gpa[i] = 3.7;
else if (stud_scores[i] >= 70) gpa[i] = 3;
else if (stud_scores[i] >= 50) gpa[i] = 2;
else gpa[i] = 0;
sumGpa += gpa[i];
}
return sumGpa / subjects;
}
void student::print()
{
cout << "Name : " << name << endl;
cout << "Id : " << stud_id << endl;
cout << "Level : " << stud_level << endl;
for (int i = 0; i < subjects; i++)
cout << "Score of Subject no. " << i + 1 << " = " << stud_scores[i] << endl;
cout << "Your GPA = " << GPA() << endl;
}
main.cpp
#include<iostream>
#include"student.h"
using namespace std;
void main() {
student s1;
string name;
int level, id;
double scores[subjects];
cout << "Enter your Name : ";
cin >> name;
cout << "Enter your Id : ";
cin >> id;
cout << "Enter your Level : ";
cin >> level;
for (int i = 0; i < subjects; i++) {
cout << "enter your score of subject no. " << i + 1 << endl;
cin >> scores[i];
}
s.setStudentData(id, level, n, score);
s.print();
}