0

I have two completed classes at the moment, the Teacher and Student classes have default definitions.

Right now I am trying to figure out the Classroom and School classes. The Classroom class is supposed to hold a Teacher and an array of 35 Student objects.

The School class is supposed to contain an array of 100 Classroom objects.

How do I do this, I sort of know how to initialize an array in a class but I'm not sure how to achieve this using the objects of another class?

class Teacher
{
   private:
     string last;
     string first;
     int gradeLevel;
   public:
     Teacher();
};

Teacher::Teacher()
{
   last = "AAAA";
   first = "BBBB";
   gradeLevel = 0;
}

class Student
{
   private:
      string studLast;
      string studFirst;
   public:
      Student();
};

Student::Student()
{
   studLast = "AAAA";
   studFirst = "BBBB";
}

class Classroom
{

};

class School
{
};
3
  • 1
    Instead of array you can use vector. Commented Apr 6, 2017 at 4:21
  • @user1438832 I need to use an array Commented Apr 6, 2017 at 4:22
  • @Alex204: Why would you "need" to use an array? Commented Apr 6, 2017 at 5:07

2 Answers 2

2

For example:

class Classroom
{
private:
    Teacher t; // create a teacher
    Student s[35]; // create an array of 35 students
...
};

class School
{
private:
    Classroom rooms[100]; // create an array of 100 rooms
...
};
Sign up to request clarification or add additional context in comments.

Comments

1

What you want to do here is create a Teacher, just one like you wanted, and then create an array of Student objects, which if you didn't know is done like Student students[35];. Then to the School object which is just an array of Classroom objects. Here is the full code:

class Classroom
{
private:
    Teacher teacher;
    Student students[35];
public:
    Classroom();
};

Classroom::Classroom()
{
    ;
}

class School
{
private:
    Classroom classrooms[100];
public:
    School();
};

School::School()
{
    ;
}

Note: all of the items in the arrays are initialized when you write something like Student students[35];. You can check this by doing cout << this->stduents[12].studLast << endl;

Comments

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.