将对象存储在第二个对象的数组中

时间:2016-04-22 15:01:48

标签: c++ arrays object

我必须用C ++创建一个小型控制台应用程序,它将执行以下操作:

创建具有下一个属性的主题:主题名称,学生人数和参加该主题的学生阵列。之后创建一个具有学生姓名和姓氏的学生作为属性。在主文件中计算每个主题中有多少重复的名称。

我这里几乎没有问题。首先,我不知道如何在我的Subject.h文件中初始化数组。其次是如何将Student个对象实际放入Subject个对象中并最后比较名称。

我希望输出的内容如下:

subjectA中的重复名称是:Michael。

subjectB中的重复名称是:Nicholas,John。

subjectAsubjectB应该被称为C++C

到目前为止,这是我的代码(关于我的这个问题,我用Google搜索过了一两个小时但是我无法找到正确的答案/示例)。

注意:我包含所有这些文件以供澄清。

Subject.h

#include <string>
#include <iostream>

using namespace std;

/*
 * I should also have an array named `arrayOfStudents`
 * which should store all students who are attending
 * that Subject.
 */ 

class Subject
{
public:
    Subject();
    Subject(Subject &subject);
    Subject(string nameOfSubject, int numberOfStudents);
    ~Subject();

    const string getNameOfSubject();
    const int getNumberOfStudents();

    void setNameOfSubject(string nameOfSubject);
    void setNumberOfStudents(int numberOfStudents);
    void print();
private:
    string nameOfSubject;
    int numberOfStudents;
};

Subject.cpp

#include <iostream>
#include "Subject.h"

using namespace std;

Subject::Subject()
{

}

Subject::Subject(string nameOfSubject, int numberOfStudents)
{
    this->nameOfSubject = nameOfSubject;
    this->numberOfStudents = numberOfStudents;
}

Subject::Subject(Subject &Subject) : nameOfSubject(Subject.getNameOfSubject()), numberOfStudents(Subject.getNumberOfStudents())
{

}

Subject::~Subject()
{
    cout << "Object is destroyed" << endl;
}

const string Subject::getNameOfSubject()
{
    return nameOfSubject;
}

const int Subject::getNumberOfStudents()
{
    return numberOfStudents;
}

void Subject::setNameOfSubject(string nameOfSubject)
{
    nameOfSubject = this->nameOfSubject;
}

void Subject::setNumberOfStudents(int numberOfStudents)
{
    numberOfStudents = this->numberOfStudents;
}

void Subject::print()
{
   cout << "Subject: " << nameOfSubject << " :: Number of students: " << numberOfStudents << endl;
}

Student.h

#include <string>
#include <iostream>

using namespace std;

class Student
{
public:
    Student();
    Student(Student &student);
    Student(string name, string surname);
    ~Student();

    const string getName();
    const string getSurname();

    void setName(string name);
    void setSurname(string surname);
    void print();
private:
    string name;
    string surname;
};

Student.cpp

#include <iostream>
#include "Student.h"

using namespace std;

Student::Student()
{

}

Student::Student(string name, string surname)
{
    this->name = name;
    this->surname = surname;
}

Student::Student(Student &student) : name(student.getName()), surname(student.getSurname())
{

}

Student::~Student()
{
    cout << "Object is destroyed" << endl;
}

const string Student::getName()
{
    return name;
}

const string Student::getSurname()
{
    return surname;
}

void Student::setName(string name)
{
    name = this->name;
}

void Student::setSurname(string surname)
{
    surname = this->surname;
}

void Student::print()
{
   cout << "Student: " << name << " " << surname << endl;
}

Main.cpp的

#include <iostream>
#include "Subject.h"
#include "Student.h"

using namespace std;

int main()
{
    /*
     * First three students should attend first Subject
     * while other four the second Subject.
     * Also note that only names matter and not surnames.
     */

    Student stA("Michael", "Doe");
    Student stB("Michael", "Doe");
    Student stC("Thomas", "Doe");
    Student stD("Nicholas", "Doe");
    Student stE("Nicholas", "Doe");
    Student stF("John", "Doe");
    Student stG("John", "Doe");

    Subject subjectA("C++", 3);
    Subject subjectB("C", 4);

    return 0;
}

2 个答案:

答案 0 :(得分:1)

1)将一组学生放入Subject对象中:您可能希望在此处使用向量而不是数组: 在subject.h中添加

#include "Student.h"

public:
    void addStudent(Student student);
private:
    std::vector<Student> students_;
<.c>在subject.cpp中添加

void Subject::addStudent(Student student)
    {
        this->students_.push_back(student);
    }

如果您想稍后以某种方式提取学生列表,则需要编写一个函数来访问它(或将其公开)。

2)要查找重复项,请查看此处 Checking for duplicates in a vector

您必须注意:学生对象在您的主题对象中,而不是学生姓名。你必须先提取它们,例如把它们放在矢量中。

答案 1 :(得分:0)

你的任务定义是你应该有一个Subject类的Students属性数组,但我没有在你的Subject类定义中看到这一点。 也许是添加Student方法然后迭代数组。