2016-04-22 98 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