gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:42:52 25 4
gpt4 key购买 nike

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

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

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

我希望输出看起来像什么:

subjectA 中的重复名称是:Michael。

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

其中 subjectAsubjectB 应该称为 C++C

到目前为止,这是我的代码(我在过去一两个小时内用谷歌搜索了我的这个问题,但我找不到合适的答案/示例)。

注意:我将所有这些文件包括在内以作澄清。

主题.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;
};

主题.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;
};

学生.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;
}

最佳答案

1) 将一组 Students 放入您的 Subject 对象中:您可能想在这里使用 vector 而不是数组:在 subject.h 添加

#include "Student.h"

public:
void addStudent(Student student);
private:
std::vector<Student> students_;

在subject.cpp中添加

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

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

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

您必须注意:学生对象在您的主题对象中,而不是学生姓名。您必须先提取它们,例如将它们放在一个 vector 中。

关于c++ - 将对象存储在第二个对象的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36797256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com