2016-04-22 95 views
0

我要创建C++中的小型控制台应用程序,这将做到以下几点:在第二个对象的数组存储对象

创建类话题为接下来的属性:对象的名字,学生和阵列的数量的参加该科目的学生。之后,创建一个名为学生姓名的学生作为属性。在主文件中统计每个主题有多少重复名称。

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

想输出什么我看起来像:

subjectA重复的名称是:迈克尔。

subjectB重复的名字是:Nicholas,John。

其中subjectAsubjectB应该被称为C++C

这是我的代码到目前为止(我搜索了一两个关于我的这个问题,但我只是找不到正确的答案/例子)。

注:我包括所有这些文件的澄清。

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; 
} 
+0

'nameOfSubject = this-> nameOfSubject;'在'setNameOfSubject'和其他的后面。但是我没有看到有关打印“重复名称......”的任何代码,所以,谁知道? –

+0

如果你想得到很好的回复请发表[MCVE] –

回答

0

你的任务清晰度最高审计机关,你应该有一个数组的学生属性的主题类的,但我没有看到这个你主题类定义。 也许添加学生方法,然后遍历数组。

+0

我说我不知道​​如何初始化它。我在'Subject.cpp'文件中发现了各处的错误。 – brajevicm

+0

我会给存储有多少学生已添加到学生数组的主题类的另一个属性,然后在添加学生,我会检查它是否低于或等于numberOfStudents然后我将学生obj添加到计数数组的索引 – David

1

1)让学生的阵列到你的主题对象:您可能希望使用矢量数组来代替在这里:在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

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

+0

谢谢,我将查看向量。 – brajevicm