Task
You are given two classes, Person and Student, where Person is the base class and Student is the derived class. The completed code for Person and a declaration for Student are provided for you in the editor. Observe that the Student inherits all the properties of the Person.
Input Format
The locked stub code in the editor reads the input and calls the Student class constructor with the necessary arguments. It also calls the calculate method which takes no arguments.
The first line contains firstName, lastName, and idNumber, separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes scores.
Constraints
1 <= length of firstName, length of lastName <=10
length of idNumber = 7
0<= score <= 100
Output Format
Output is handled by the locked stub code. Your output will be correct if your Student class constructor and calculate() method are properly implemented.
Sample Input
Heraldo Memelli 8135627
2
100 80
Sample Output
Name: Memelli, Heraldo
ID: 8135627
Grade: O
Explanation
This student had 2 scores to average: 100 and 80. The student's average grade is (100 + 80)/2 = 90. An average grade of 90 corresponds to the letter grade of O, so the calculate() method should return the character 'O'.
You are given two classes, Person and Student, where Person is the base class and Student is the derived class. The completed code for Person and a declaration for Student are provided for you in the editor. Observe that the Student inherits all the properties of the Person.
- Complete the Student class by writing the following:
- A Student class constructor, which has the parameters:
- A string, firstName.
- A string, lastName.
- An integer, idNumber.
- An integer array (or vector) of test scores, scores.
- A char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average:
Input Format
The locked stub code in the editor reads the input and calls the Student class constructor with the necessary arguments. It also calls the calculate method which takes no arguments.
The first line contains firstName, lastName, and idNumber, separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes scores.
Constraints
1 <= length of firstName, length of lastName <=10
length of idNumber = 7
0<= score <= 100
Output Format
Output is handled by the locked stub code. Your output will be correct if your Student class constructor and calculate() method are properly implemented.
Sample Input
Heraldo Memelli 8135627
2
100 80
Sample Output
Name: Memelli, Heraldo
ID: 8135627
Grade: O
Explanation
This student had 2 scores to average: 100 and 80. The student's average grade is (100 + 80)/2 = 90. An average grade of 90 corresponds to the letter grade of O, so the calculate() method should return the character 'O'.
Submit the Solution: Click Here
Day 12 Inheritance Hackerrank Solution in C++
#include <iostream>
#include <vector>
using namespace std;
class Person {
protected: string firstName;
string lastName;
int id;
public: Person(string firstName, string lastName, int identification) {
this -> firstName = firstName;
this -> lastName = lastName;
this -> id = identification;
}
void printPerson() {
cout << "Name: " << lastName << ", " << firstName << "\nID: " << id << "\n";
}
};
class Student: public Person {
private: vector < int > testScores;
public:
// Write your constructor
Student(string firstName, string lastName, int id, vector < int > scores): Person(firstName, lastName, id) {
this -> testScores = scores;
}
// Write char calculate()
char calculate() {
int sumScore = 0;
for (int i = 0; i < testScores.size(); i++) {
sumScore += testScores[i];
}
int averageScore = sumScore / testScores.size();
if (averageScore <= 100 && averageScore >= 90) {
return 'O';
} else if (averageScore < 90 && averageScore >= 80) {
return 'E';
} else if (averageScore < 80 && averageScore >= 70) {
return 'A';
} else if (averageScore < 70 && averageScore >= 55) {
return 'P';
} else if (averageScore < 55 && averageScore >= 40) {
return 'D';
}
return '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(firstName, lastName, id, scores);
s -> printPerson();
cout << "Grade: " << s -> calculate() << "\n";
return 0;
}
0 Comments: