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

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

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

我希望输出看起来像:

subjectA中的重复名称是:Michael。

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

其中subjectAsubjectB应称为C++C

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

注意:为了说明我将包括所有这些文件。

主题

#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;
}


学生

#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)在您的Subject对象中获取一个Student数组:您可能想在这里使用向量而不是数组:
在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

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

07-24 09:47
查看更多