Pen Gerald
Pen Gerald

Reputation: 31

Creating objects from another class and adding them to an Array [C++]

I'm new to C++ and stuck on a problem. I want class Admin to be able to create new objects of the Student class and add the objects to an array that contains all students' info. How can I do that in the Admin class?

#include <iostream>
#include <string>
#include <sstream>

using namespace std;



class Student
{

public:

    int SSN_LENGTH = 9, MIN_NAME_LEN = 1, MAX_NAME_LEN = 40;
    string DEFAULT_NAME = "no name", DEFAULT_SSN = "000000000";

private:
    string studentName, SSN;


public:
    // constructor declarations
    Student();
    Student( string studentName, string SSN);

    // Accessors
    string getSSN(){ return SSN; }
    string getStudentName(){ return studentName; }


    // Mutators
    bool setSSN(string SSN);
    bool setStudentName(string studentName);

};

class Admin
{

private:
    static const int Student_MAX_SIZE = 100000;


public:
    bool addStudent (string studentName, string SSN);

};

Upvotes: 0

Views: 53

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

How can I do that in the Admin class?

Use std::vector, illustrated by the code below:

#include <vector>
//...
class Admin
{
    private:
        static const std::size_t Student_MAX_SIZE = 100000;
        std::vector<Student> vStudent;

    public:
        bool addStudent (string studentName, string SSN)
        {
           if ( vStudent.size() < Student_MAX_SIZE )  
           {
               vStudent.push_back(Student(studentName, SSN));  
               return true;
           }
           return false;
        }
};

Upvotes: 1

Related Questions