C++ solved inheritance problem from hackerrank:
#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 identification, vector<int> scores): Person(firstName, lastName, identification), testScores(scores) {
}
// Write char calculate()
char calculate() {
int sum = 0;
int count = 0;
vector<int>::iterator it;
for(it = testScores.begin(); it != testScores.end(); ++it) {
sum+= *it;
count++;
}
int avg = sum/count;
if (90 <= avg && avg <=100)
return 'O';
else if(80<= avg && avg < 90)
return 'E';
else if(70<= avg && avg < 80)
return 'A';
else if(55<= avg && avg < 70)
return 'P';
else if(40 <= avg && avg < 55)
return 'D';
else
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;
}
#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 identification, vector<int> scores): Person(firstName, lastName, identification), testScores(scores) {
}
// Write char calculate()
char calculate() {
int sum = 0;
int count = 0;
vector<int>::iterator it;
for(it = testScores.begin(); it != testScores.end(); ++it) {
sum+= *it;
count++;
}
int avg = sum/count;
if (90 <= avg && avg <=100)
return 'O';
else if(80<= avg && avg < 90)
return 'E';
else if(70<= avg && avg < 80)
return 'A';
else if(55<= avg && avg < 70)
return 'P';
else if(40 <= avg && avg < 55)
return 'D';
else
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;
}
No comments:
Post a Comment