Page of 7 ZoOM 4. The following program represents a gradebook in C+ using class
ID: 3904684 • Letter: P
Question
Page of 7 ZoOM 4. The following program represents a gradebook in C+ using classes and member functions. For this question you may NOT use smart pointers. study the pragram, and then correctly add logic to properly cleanup all resources that have been allocated on the heap. 4,7 Structured Dato.md 5/22/2018 #include #include #include using nanespace std class GradeRecord ( public: GradeRecord (int id, double grade) Ch int getId) const return_studentId; :studentld(id), erade(erade) void setérade (double grade) _erade grade;) friend ostrea& operatorExplanation / Answer
Q 4 : --------->>>>>>>>answer : ------->>>>>>
#include<iostream>
#include<vector>
using namespace std;
class GradeRecord{
//same as in question
};
class Gradebook{
public:
Gradebook(){
//same as in the question
}
~Gradebook(){
for(int i = 0;i<_records.size();i++){
delete _records[i];
}
}
private:
vector<GradeRecord*> _records;
};
int main(){
//same as in the question
delete grades;
//this will delete all the GradeRecord by calling the destructor of Gradebook and also the gradebook of main
}
Q5 answer : ------------->>>>>>>>>>>>>>
#include<iostream>
#include<vector>
using namespace std;
class GradeRecord{
//same as in question
};
class Gradebook{
public:
Gradebook(){
//same as in the question
}
~Gradebook(){
//nothing to do here because of smart pointer
}
void addRecord(int studentId,double grade){
std::unique_ptr<GradeRecord> newRecord(new GradeRecord(studentId,grade));
_records.push_back(newRecord);
}
void updateRecord(int studentId,double grade){
for(int i = 0;i<_records.size();++i){
if(_records.at(i)->getId() == studentId){
_records.at(i)->setGrade(grade);
break;
}
}
}
//rest is same as in question
private:
vector<std::unique_ptr<GradeRecord>> _records;
};
int main(){
std::unique_ptr<Gradebook> grades(new Gradebook());
//rest is same as in question
//delete grades; no need of this line in code
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.