Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ Can not manage to display the class datatype array

This is the code I wrote, total beginner

  #include <iostream>
  #include <cstring>

using namespace std;


class Person
{
public:
    string ID;
    string name;
    string surname;
    string department;
    string email;

public:
    //get and set functions for ID, Name, Surname, Department, Email properties
    string getID()
    {
        return this->ID;
    };
    void setID(string _ID)
    {
        this->ID = _ID;
    };
    string getName()
    {
        return this->name;
    };
    void setName(string _name)
    {
        this->name = _name;
    };
    string getSurname()
    {
        return this->surname;
    };
    void setSurname(string _surname)
    {
        this->surname = _surname;
    };
    string getDepartment()
    {
        return this->department;
    };
    void setDepartment(string _department)
    {
        this->department = _department;
    };
    string getEmail()
    {
        return this->email;
    };
    void setEmail(string _email)
    {
        this->email = _email;
    };
};

//inherit Student class from Person class
class Student :public Person
{
private:
    int student_counter = 0;
public:
    //constructor
    Student()
    {
    };

    Student(string id, string Name, string Surname, string Department, string Email)
    {
        setID(id);
        setName(Name);
        setSurname(Surname);
        setDepartment(Department);
        setEmail(Email);
    }
    //student add 
    void addStudent(string id, string Name, string Surname, string Department, string Email)
    {
        if (student_counter >= 100)
        {
            cout << "cant add more students";
        }
        else
        {
            Student _S[100]
            Student newS;
            newS.setID(id);
            newS.setName(Name);
            newS.setSurname(Surname);
            newS.setDepartment(Department);
            newS.setEmail(Email);
            _S[student_counter] = newS;
            student_counter++;
        }
    }

    //display students
    void display()
    {
        for (int i = 0; i < student_counter; i++)
        {
            cout << _S[i].getID() << " - ";
        }

    }
};
};





int
main()
{

    Student stu;
    stu.addStudent("ST123456", "Ege", "Inan", "CS", "ege @ gmail.com");
    stu.display();
}

The problem is here

//inherit Student class from Person class
    class Student :public Person
    {
    private:
        int student_counter = 0;
    public:
        //constructor
        Student()
        {
        };
    
        Student(string id, string Name, string Surname, string Department, string Email)
        {
            setID(id);
            setName(Name);
            setSurname(Surname);
            setDepartment(Department);
            setEmail(Email);
        }
        //student add 
        void addStudent(string id, string Name, string Surname, string Department, string Email)
        {
            if (student_counter >= 100)
            {
                cout << "cant add more students";
            }
            else
            {
                Student _S[100];
                Student newS;
                newS.setID(id);
                newS.setName(Name);
                newS.setSurname(Surname);
                newS.setDepartment(Department);
                newS.setEmail(Email);
                _S[student_counter] = newS;
                student_counter++;
            }
        }
    
        //display students
        void display()
        {
            for (int i = 0; i < student_counter; i++)
            {
                cout << _S[i].getID() << " - ";
            }
    
        }
    };
    };

The error message I get is this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

main.cpp:106:9: error: ‘_S’ was not declared in this scope
  106 |   cout<<_S[i].getID()<<" - ";
      |         ^~

I tried moving the definition of Student _S to the private part of student class like this:

class Student :public Person
    {
    private:
        int student_counter = 0;
        Student _S[100];
    public:
        //constructor
        Student()
        {
        };

        Student(string id, string Name, string Surname, string Department, string Email)
        {
            setID(id);
            setName(Name);
            setSurname(Surname);
            setDepartment(Department);
            setEmail(Email);
        }
        //student add 
        void addStudent(string id, string Name, string Surname, string Department, string Email)
        {
            if (student_counter >= 100)
            {
                cout << "cant add more students";
            }
            else
            {

                Student newS;
                newS.setID(id);
                newS.setName(Name);
                newS.setSurname(Surname);
                newS.setDepartment(Department);
                newS.setEmail(Email);
                _S[student_counter] = newS;
                student_counter++;
            }
        }

        //display students
        void display()
        {
            for (int i = 0; i < student_counter; i++)
            {
                cout << _S[i].getID() << " - ";
            }

        }
    };
    };

But then I got this:

   65 |   Student _S[100];
      |           ^~
main.cpp:61:7: note: definition of ‘class Student’ is not complete until the closing brace
   61 | class Student:public Person
      |       ^~~~~~~

What would be the best way of handling this? where to define the array to get the program to run as intended?

>Solution :

In the first code, _S is a local variable inside of addStudent(), so display() can’t access it.

Your second code is better, in that display() can now access _S. But a Student object can’t hold any data members that are also Student objects, you would end up with an endless recursive allocation.

To accomplish what you want, you need to make _S, student_counter, addStudent(), and display() be static members of the Student class, eg:

class Student :public Person
{
private:
    static int student_counter;
    static Student _S[100];
public:
    //constructor
    Student()
    {
    };

    Student(string id, string Name, string Surname, string Department, string Email)
    {
        setID(id);
        setName(Name);
        setSurname(Surname);
        setDepartment(Department);
        setEmail(Email);
    }

    //student add 
    static void addStudent(string id, string Name, string Surname, string Department, string Email)
    {
        if (student_counter >= 100)
        {
            cout << "cant add more students";
        }
        else
        {
            Student newS;
            newS.setID(id);
            newS.setName(Name);
            newS.setSurname(Surname);
            newS.setDepartment(Department);
            newS.setEmail(Email);
            _S[student_counter] = newS;
            student_counter++;
        }
    }

    //display students
    static void display()
    {
        for (int i = 0; i < student_counter; i++)
        {
            cout << _S[i].getID() << " - ";
        }
    }
};

int Student::student_counter = 0;
Student Student::_S[100];

int main()
{
    Student::addStudent("ST123456", "Ege", "Inan", "CS", "ege @ gmail.com");
    Student::display();
}

Online Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading