Answer:
See explaination
Explanation:
#include <iostream>
using namespace std;
//DynamicGrades.h
class DynamicGrades
{
public:
DynamicGrades(int size);
~DynamicGrades();
void mutate(int studentId, int index, int grade);
int getGrade(int studentId, int index);
private:
int m_studentId;
int m_noofGrades;
int* m_dynamicArr;
};
//DynamicGrades.cpp
#include "DynamicGrades.h"
DynamicGrades::DynamicGrades(int size)
{
m_noofGrades = size;
m_dynamicArr = new int[m_noofGrades] {0};
}
DynamicGrades::~DynamicGrades()
{
if(nullptr != m_dynamicArr)
delete[] m_dynamicArr;
}
void DynamicGrades::mutate(int studentId, int index, int grade)
{
m_studentId = studentId;
if (index < m_noofGrades)
m_dynamicArr[index] = grade;
}
int DynamicGrades::getGrade(int studentId, int index)
{
if (index >= m_noofGrades)
return -1;
return m_dynamicArr[index];
}
//Main.cpp
#include "DynamicGrades.h"
int main()
{
DynamicGrades* grade = new DynamicGrades(5);
DynamicGrades grade2(10);
for (int i = 0;i < 5;++i)
{
grade->mutate(2, i, i + 1);
}
for (int i = 0; i < 10; i++)
{
grade2.mutate(3, i, i + 1);
}
cout << "Grades are " << endl;
for (int i = 0;i < 5;++i)
{
cout << grade->getGrade(2, i) <<" ";
}
cout << endl;
for (int i = 0; i < 10; i++)
{
cout << grade2.getGrade(3, i) << " ";
}
}