0

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);

};
4
  • You cannot add elements to an array, arrays are fixed size. Use std::vector instead. I don't see any function named Admin what are you exactly talking about? Commented Dec 9, 2018 at 20:22
  • I meant "Class" admin. I want to create objects of class Student inside of Admin and add those objects to an array that holds all of the students' info. Do I have to use vector for that? Commented Dec 9, 2018 at 20:27
  • Try this simpler exercise first: assign a value to a Student variable. That is, create a Student, and then change it, perhaps by passing another Student to it. This is a necessary step on the way to storing a Student in a container. Commented Dec 9, 2018 at 20:28
  • 1
    Do I have to use vector for that? -- Do you need legs to walk? No, you can learn to walk on your hands. The point is that the easiest thing to use is vector, as that is what it is designed for. Commented Dec 9, 2018 at 20:28

1 Answer 1

1

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;
        }
};
Sign up to request clarification or add additional context in comments.

1 Comment

I would appreciate a small note suggesting the use of std::size_t instead of int or unsigned int

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.