Results calculator class

Lecture 1 Q3 Exercise 2

Results Calculator class

/*Results.cpp*/
#include <iostream>
using namespace std;
 
class Results{
    private:
        int *results; //must use dynamic mem allocation 
        int studentID;
        int total;
    public:
        Results();
        Results(int);
        ~Results();
        float getAverage();
        int getTotal();
        void enterMarks();
};
 
//default constructor
Results::Results(){
}
 
//constructor with arguments - overloaded constructor
Results::Results(int sid){
    studentID = sid;
    //use the new operator for dynamic memory allocation
    results = new int[4];     
}
 
//destructor 
Results::~Results(){
     //free the memory which was allocated to the results array
     //use delete to free the dynamically allocated memory
     delete [] results;
}
 
void Results::enterMarks(){
     for (int i = 0; i < 4 ; i++) {
         cout << "enter marks " << (i+1) << ":";
         cin >> results[i];
     }
}
 
float Results::getAverage(){
    total = 0; //MUST intilialize before use
    for (int i = 0; i < 4 ; i++)
        total = total + results[i];
    return total / 4;
}
 
int Results::getTotal(){
    return total;
}
 
int main() {
 
    Results r1(10101);
    r1.enterMarks();
    cout << r1.getAverage() << endl;
    cout << r1.getTotal() << endl;
 
    Results r2(100101);
    r2 = r1;
    cout << r2.getAverage() << endl;
    cout << r2.getTotal() << endl;
 
    system("pause");
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License