简体   繁体   English

在 c++ 中的继承 class 中声明构造函数时出错

[英]Error while declaring constructor in an Inherited class in c++

I have been given two classes, Person and Student, where Person is the base class and Student is the derived class.我有两个类,Person 和 Student,其中 Person 是基础 class,Student 是派生的 class。 Any change in Person class or main function is not allowed.. Observe that Student inherits all the properties of Person.不允许对 Person class 或主 function 进行任何更改。观察 Student 继承了 Person 的所有属性。 A Student class constructor, which has parameters: A string, first name A string, last name An integer, id.一个学生 class 构造函数,它有参数:一个字符串,名字一个字符串,姓一个 integer,id。 An integer array (or vector) of test scores, .测试分数的 integer 数组(或向量),. char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average. char calculate() 方法,计算学生对象的平均值并返回代表其计算平均值的成绩字符。 Sample Input-样本输入-

Heraldo Memelli 8135627 2 100 80

Expected Output-预期输出-

Name: Memelli, Heraldo ID: 8135627 Grade: O

Error which I am getting is while declaring constructor can you please explain me why and is there any other approach which you would suggest.我得到的错误是在声明构造函数时,你能解释一下为什么吗?还有你建议的其他方法吗? Thanks in advance.提前致谢。 Here's my code-这是我的代码-

    #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: Student(string firstName, string lastName, int identification, vector < int > & scores) {
        for (int i = 0; i < & scores.size(); i++)
          this - > testScores.pushback( & scores[i]);
      }
      char calculate() {
        int avg, sum = 0, count = 0;
        for (int i = testScores.begin(); i < testScores.size(); i++) {
          sum = sum + testScores[i];
          count++;
        }
        avg = sum / count;
        if (avg >= 90 && avg <= 100)
          return ('O');
        else if (avg >= 80 && avg < 90)
          return ('E');
        else if (avg >= 70 && avg < 80)
          return ('A');
        else if (avg >= 55 && avg < 70)
          return ('P');
        else if (avg >= 40 && avg < 55)
          return ('D');
        else if (avg0 < 40)
          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;
    }

The class Person does not have the default constructor, So you have to call the constructor with parameters of the sub-object of the type Person explicitly in the constructor of the class Student in the mem-initializer list. class Person没有默认构造函数,因此您必须在 mem-initializer 列表中的 class Student的构造函数中显式调用带有Person类型子对象的参数的构造函数。

The constructor can look the following way构造函数可以如下所示

Student( const std::string &firstName, 
         const std::string &lastName, 
         int identification, 
         const std::vector<int> &scores ) 
         : Person( firstName, lastName, identification ), testScores( scores )
{
} 

And the member function can be defined like并且成员 function 可以定义为

  char calculate() const
  {
    long long int sum = 0;

    for ( const auto &item : testScores )
    {
        sum += item;
    }

    long long int avg = testScores.size() == 0 ? 0 : sum / testScores.size();

    char c;

    if ( avg >= 90 )
      c = 'O';
    else if ( avg >= 80 )
      c = 'E';
    else if ( avg >= 70 )
      c ='A';
    else if ( avg >= 55 )
      c = 'P';
    else if ( avg >= 40 )
      c = 'D';
    else
      c = 'T';

    return c;
  }

As for your code then for example this loop至于你的代码,例如这个循环

    for (int i = 0; i < & scores.size(); i++)
      this - > testScores.pushback( & scores[i]);

is invalid and shall not be compiled at least because you are trying to take the address of an rvalue returned by the member function size .无效,至少不应编译,因为您试图获取成员 function size返回的右值的地址。

I think it's pretty clear that you are expecting your Student object to be a person which the given name etc, but where in your code to you say that?我认为很明显,您期望您的学生 object 是一个具有给定名称等的人,但是您在代码中的哪个地方这么说? The firstName , lastName and identitfication parameters are unused.未使用firstNamelastNameidentitfication参数。 Here's how you should to it这是你应该如何去做

Student(string firstName, string lastName, int identification, vector < int > & scores) : 
    Person(firstName, lastName, identification) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM