Hackerrank Day 12: Inheritance | Hackerrank Solutions in C++


 Objective

Today, we're delving into Inheritance. Check out the attached tutorial for learning materials and an instructional video!

Task
You are given two classes, Person and Student, where Person is the base class and Student is the derived class. Completed code for Person and a declaration for Student are provided for you in the editor. Observe that Student inherits all the properties of Person.

Complete the Student class by writing the following:

  • Student class constructor, which has  parameters:
    1. A string, .
    2. A string, .
    3. An integer, .
    4. An integer array (or vector) of test scores, .
  • 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 your editor calls your Student class constructor and passes it the necessary arguments. It also calls the calculate method (which takes no arguments).

You are not responsible for reading the following input from stdin:
The first line contains , and , respectively. The second line contains the number of test scores. The third line of space-separated integers describes .

Constraints

Output Format

This is handled by the locked stub code in your editor. 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  scores to average:  and . The student's average grade is . An average grade of  corresponds to the letter grade , so our calculate() method should return the character'O'.


Solution: 

#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 total = 0;


        for (int i = 0; i < this->testScores.size(); i++)

            total += this->testScores[i];


        int avg = (int) (total / testScores.size());


        if (avg >= 90 && avg <= 100) return 'O';

        if (avg >= 80 && avg < 90) return 'E';

        if (avg >= 70 && avg < 80) return 'A';

        if (avg >= 55 && avg < 70) return 'P';

        if (avg >= 40 && avg < 55) 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;

}

Post a Comment

0 Comments