Virtual Functions

  • + 4 comments

    This problem is driving me crazy! My code works for the first test case but fails on about half of them. I downloaded the textfiles for the input and output of one of the cases it fails on. Then I used the file as an input and piped the output to another file. I used a diff tool to see if there was any difference between the expected output and the generated output and there was none. If anyone can let me know what I am doing wrong I would greatly appreciate it.

    static int student_id, professor_id;
    class Person
    {
        public:
    
        int age;
        string name;
    
        // = 0 indicates pure virtual function, must be overridden
        virtual void getdata() = 0;
    
        // virtual function with definition, does not have to be overridden
        virtual void putdata() = 0;
    };
    
    class Professor : public Person
    {
        public:
        int publications;
        int id;
    
        Professor()
        {
            ++professor_id;
        }
    
        void getdata()
        {
            cin >> name >> age >> publications;
            id = professor_id;
        }
    
        void putdata()
        {
            cout << name << " " << age << " " << publications << " " << id << endl;
        }
    };
    
    
    
    class Student : public Person
    {
        public:
        int marks[6];
        int marks_sum;
        int id;
    
        Student()
        {
           ++student_id;
        }
    
        void getdata()
        {
            cin >> name >> age;
            id = student_id;
            for(int i = 0; i < 6; i++)
            {
                cin >> marks[i];
                marks_sum += marks[i];
            }
        }
    
        void putdata()
        {
            cout << name << " " << age << " " << marks_sum << " " << id << endl;
        }
    };