简体   繁体   English

班级组成

[英]Classes composition

This is my first topic. 这是我的第一个话题。 I would like to ask you about composition with classes in C++. 我想问一下有关C ++中类的组成。 I have two classes, whose one of them contains an array of the other one, for example Teacher and School . 我有两个班级,其中一个班级包含另一个班级的数组,例如TeacherSchool

School contains an array of Teacher and Teacher has two variables, like string subject and int students (number of students). School包含数组TeacherTeacher有两个变量,像string subjectint students (学生人数)。

For example, the class Teacher has two functions: getSubAndStud(string, int) and setSubAndStud() . 例如,类Teacher具有两个函数: getSubAndStud(string, int)setSubAndStud()

I would like to ask you in which way I could initialize the array, so that the school has a number of N teachers and every teacher has a subject and a number of students . 我想问你用什么方式可以初始化数组,以便学校有N 老师 ,每个老师都有一个科目和很多学生

Please, consider that I am a beginner, so, if you can, try to give a reply as simple as possible. 请,请考虑我是一个初学者,因此,如果可以的话,请尝试给出尽可能简单的答复。

Thank you in advance. 先感谢您。

I would create a method that takes in a variable N and constructs a School object with N teachers. 我将创建一个方法,该方法接受变量N并使用N位老师构造School对象。 Then I would create another method that instantiates a Teacher object at the smallest empty index in the Teacher array with given Subject and Student values. 然后,我将创建另一个方法,该方法以给定的Subject和Student值在Teacher数组中的最小空索引处实例化Teacher对象。 You could parse for an empty index by checking for NULL. 您可以通过检查NULL来解析空索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM