初学者C ++继承

时间:2016-04-13 20:07:33

标签: c++ inheritance

我对C有一点经验,但对OOP没有经验,我试图在继承中解决C ++中的HackerRank问题。我认为由于我的average变量计算不正确,我在如何定义派生类时搞砸了。此外,我甚至不确定如何打印测试用例进行调试,因为当我向cout添加calculate()语句时,它没有做任何事情。

#include <iostream>
#include <vector>

using namespace std;

// given this definition  
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"; 
        }

};

// this is what I'm supposed to be creating
class Student :  public Person{
    private:
        vector<int> testScores;  
    public:
    Student(string firstName, string lastName, int id, vector<int> testScores) : Person(firstName,lastName, id) 
    {};

    char calculate(){
        double average = 0;
        for (int i = 0; i < testScores.size(); i++){
            average += testScores[i];
            cout << average;
        }
        average = average / testScores.size();
            if ((average >= 90) && (average <= 100))
                return 'O';
            else if ((average >= 80) && (average < 90))
                return 'E';
            else if ((average >= 70) && (average < 80))
                return 'A';
            else if ((average >= 55) && (average < 70))
                return 'P';
            else if ((average >= 40) && (average < 55))
                return 'D';
            else if (average < 40)
                return 'T';
            else 
                return 'X'; // always returns this??
    }

};

// also given this main
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;
}

1 个答案:

答案 0 :(得分:4)

问题是你向你的构造函数提供了你从控制台读取的分数:

 Student* s = new Student(firstName, lastName, id, scores);

不幸的是,在你的课堂上,你不能用它来初始化Student对象的分数:

Student(string firstName, string lastName, int id, vector<int> testScores)
       : Person(firstName,lastName, id) 
{};   // ouch testScores is lost

只需要ccopy构建你的sutdent这样的矢量:

Student(string firstName, string lastName, int id, vector<int> testScores)
       : Person(firstName,lastName, id), testScores(testScores) 
{};   

与您的问题无关的其他评论

如果你使用new创建一个对象,你应该想到delete - 在某个地方(以后你肯定会了解像unique_ptr或shared_ptr这样的智能助手,它会照顾到你。)

您可以像this->中一样使用mem-initializer来避开Person构造函数中的Student

   Person(string firstName, string lastName, int identification)
       : firstName(firstName), lastName(lastName), id(identification)
   {  // this code starts once all the members are constructed
   }