Note - I have searched a lot on pointers and references for C++. I don't seem to understand them in THIS particular scenario. Hence posting it here!
The following is CORRECT code. It is working. I wrote this for an online C++ practice problem. A part of the code was given to me initially.
I don't understand why an array of Person objects is being created in the main function with a *per[n] as shown below:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Person {
        string name;
        int age;
    public:
        Person(){
            name = "";
        }
        virtual void putdata() = 0;
        virtual void getdata() = 0;
};
class Professor: public Person {
        int publications, cur_id;
        string name;
        int age;
    public:
        static int professorCount;
        Professor(){
            name = "";
            age = 0;
            publications = 0;
            cur_id = professorCount + 1;
            professorCount++;
        }
        void getdata(){
            cin >> name >> age >> publications;
        }
        void putdata(){
            cout << name << " " << age << " " << publications << " " << cur_id << endl;
        }
};
class Student: public Person {
        int marks[6];
        int cur_id;
        string name;
        int age;
    public:
        static int studentCount;
        Student(){
            name = "";
            age = 0;
            cur_id = studentCount + 1;
            studentCount++;
        }
        void getdata(){
            cin >> name >> age >> marks[0] >> marks[1] >> marks[2] >> marks[3] >> marks[4] >> marks[5];
        }
        void putdata(){
            cout << name << " " << age << " " << marks[0] + marks[1] + marks[2] + marks[3] + marks[4] + marks[5] << " " << cur_id << endl;
        }
};
int Professor::professorCount = 0;
int Student::studentCount = 0;
In this main function below, an array of Person objects is being created, but it is given a * in the beginning. How is it working?
int main(){
    int n, val;
    cin>>n; //The number of objects that is going to be created.
    Person *per[n]; // THIS ONE RIGHT HERE! THIS ONE!
    for(int i = 0;i < n;i++){
        cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
        }
        else per[i] = new Student; // Else the current object is of type Student
        per[i]->getdata(); // Get the data from the user.
    }
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
    return 0;
}
 
     
    