Narayan Dheeraj Kumar
Narayan Dheeraj Kumar

Reputation: 113

What is the necessity of pointer in this piece of C++ code?

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;

}

Upvotes: 1

Views: 131

Answers (2)

user0042
user0042

Reputation: 8018

I don't understand why an array of Person objects is being created in the main function with a *per[n] as shown below

The purpose of storing a pointer is to support virtual polymorphism (abstract classes like Person cannot be instantiated). A smart pointer serves that as well, but takes care about the correct dynamic memory management.

There's no need to use raw pointers or raw arrays in c++ at all. That code doesn't give a good example of "best practices".

At the main() function

Person *per[n]; // Note that VLA's aren't standard c++ syntax

should be replaced with

std::vector<std::unique_ptr<Person>> per(n);

and the loop accordingly

for(int i = 0;i < n;i++){

    cin>>val;
    if(val == 1){
        // If val is 1 current object is of type Professor
        per[i] = std::make_unique<Professor>();
              // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    }
    // Else the current object is of type Student
    else per[i] = std::make_unique<Student>(); 
               // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    per[i]->getdata(); // Get the data from the user.

}

Also

int marks[6];

should be replaced with

std::array<int,6> marks;

std::array is way more convenient and less error prone when passed as function parameter, etc.

Upvotes: 3

frslm
frslm

Reputation: 2978

You're creating an array of pointers to Person objects. That's how assignments like per[i] = new Professor; can work - new Professor returns a pointer to a Professor object, so you need an array of pointers to store that.

Upvotes: 1

Related Questions