无法在 C++ 子项中使用虚函数 Class

Unable to use virtual function in C++ child Class

我正在制作一个虚函数程序,其中有三个 classes Person、Professor 和 Student。 Person 由 Professor 和 Student 公开继承。这里我想在教授和学生中输入一些参数。为了输入和输出它们,我在每个 class 中制作了 getdataputdata。亲自 class 这些功能是虚拟定义的。但是在代码中我有一些 errorsi.ie -

Solution.cpp: In function ‘int main()’:
Solution.cpp:84:26: error: invalid new-expression of abstract class type ‘Professor’
             per[i] = new Professor;
                          ^~~~~~~~~
Solution.cpp:20:7: note:   because the following virtual functions are pure within ‘Professor’:
 class Professor : public Person
       ^~~~~~~~~
Solution.cpp:16:18: note:   ‘virtual void Person::putdata()’
     virtual void putdata() = 0;
                  ^~~~~~~
Solution.cpp:87:27: error: invalid new-expression of abstract class type ‘Student’
         else per[i] = new Student; // Else the current object is of type Student
                           ^~~~~~~
Solution.cpp:42:7: note:   because the following virtual functions are pure within ‘Student’:
 class Student: public Person
       ^~~~~~~
Solution.cpp:16:18: note:   ‘virtual void Person::putdata()’
     virtual void putdata() = 0;
                  ^~~~~~~  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class Person
{
    public:
    string name;
    int age;

    virtual void getdata() = 0;
    virtual void putdata() = 0;

};

class Professor : public Person
{
    public:
    static int cur_id;
    int publications; 

    Professor() {
        cur_id++;
    }

    void getdata( )
    {

        cin>>name>>age>>publications;
    }

    void pushdata()
    {
        cout<<name<<" "<<age<<" "<<publications<<"\n";
    }
};

class Student: public Person
{
    public:
    static int cur_id;
    int marks[6];

    Student(){cur_id++;}

    void getdata()
    {
        cin>>name>>age;
        int i=0;
        for(i = 0;i<6;i++)
          cin>>marks[i];
    }

    void pushdata()
    {
        cout<<name<<" "<<age;

        int i=0,sum=0;
        for(i = 0;i<6;i++)
          sum += marks[i];

        cout<<" "<<sum<<" "<<cur_id;
    }
};

int Professor::cur_id = 1;
int Student::cur_id = 1;
int main(){

这个函数:

void pushdata()
    {
        cout<<name<<" "<<age<<" "<<publications<<"\n";
    }

不会覆盖Person中的纯虚函数putdata

改为:

void putdata() override
    {
        cout<<name<<" "<<age<<" "<<publications<<"\n";
    }

注意,如果你把override放在你想覆盖的函数上,如果你写错了,编译器会报错