You are viewing a single comment's thread. Return to all comments →
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; class Person{ protected: string firstName; string lastName; int id; public: Person(string firstName, string lastName, int identification); void printPerson(); }; Person::Person(string firstName, string lastName, int identification) { this->firstName = firstName; this->lastName = lastName; this->id = identification; } void Person::printPerson(){ cout << "Name: " << lastName << ", " << firstName << "\nID: " << id << "\n"; } class Student : public Person{ private: vector<int> testScores; public: Student(int idNumber, const string& lastName, const string& firstName, const vector<int>& scores); char calculate(); }; Student::Student(int idNumber, const string& lastName, const string& firstName, const vector<int>& scores) : Person(firstName, lastName, idNumber){ this->id = idNumber; this->lastName = lastName; this->firstName = firstName; this->testScores = scores; } char Student::calculate(){ int sum = 0; for(auto &score : testScores){ sum += score; } int average = sum / testScores.size(); return (average >= 90) ? 'O' : (average >= 80) ? 'E' : (average >= 70) ? 'A' : (average >= 55) ? 'P' : (average >= 40) ? 'D' : 'T'; } int main() { string firstName; string lastName; int id; int numScores; cin >> firstName >> lastName >> id >> numScores; vector<int> scores; for(int i = 0; i < numScores; ++i){ int tmpScore; cin >> tmpScore; scores.push_back(tmpScore); } Student* s = new Student(id, lastName, firstName, scores); s->printPerson(); cout << "Grade: " << s->calculate() << "\n"; return 0; }
Day 12: Inheritance
You are viewing a single comment's thread. Return to all comments →