2014-02-28 26 views
0

我有一個動態數組成員的類(我在pastebin中發佈了我的代碼)我不知道我的兩個類是否正確以及它們是否有任何問題?另外我需要寫(41行)的功能,設置類StudentStudentsDynamicArrayC++類中的動態數組

這裏是我的數組類

class StudentsDynamicArray{ 
     private: 
      Student *Stud;// students dynamic array 
      int n; // current size of array 
      int nmax; // max size of array 
     public: 
      StudentsDynamicArray():n(0), Stud(NULL), nmax(0){} 
      StudentsDynamicArray(int n): 
        n(n){} 
      ~StudentsDynamicArray(); // destructor 
      void SetStud(Student S){Stud[n++] = S;} // I get error when I try to set String or Int. //How I need to change this? 
      Student GetStud(int i){return Stud[i];} 
      void IncreaseSize(int ns); 
     }; 
//Function which change size of array 
void Student::ChangeSize(int kiek){ 
     if(kiek > nmax){ 
       int *SNEW = new int[kiek]; 
       for(int i=0; i<n; i++) 
         SNEW[i] = mark[i]; 
       delete [] mark; 
       mark = SNEW; 
       nmax = kiek; 
     } 
     else if(kiek < nmax){ 
       int *SNEW = new int[kiek]; 
       for(int i=0; i<n; i++) 
         SNEW[i] = mark[i]; 
       delete [] mark; 
       mark = SNEW; 
       n = nmax = kiek; 
     } 
} 

回答

0

在學生類,你有

int *mark 

這是必須應用rule of three指示:拷貝構造函數,賦值運算符和析構函數。前兩個應該創建一個動態數組的副本,並且析構函數應該釋放內存。你只有正確實施了析構函數。 StudentsDynamicArray類也一樣。

爲:

void SetStud(Student S){Stud[n++] = S;} 

從您的代碼看起來你已經不叫INCREASESIZE最初,一旦你打電話SetStud梭哈可能是NULL。你可以通過調用它的構造函數修正:

StudentsDynamicArray():n(0), Stud(NULL), nmax(0){ IncreaseSize(10); } 

還,所有上述不會是必要的,如果你會使用std ::載體,因此,如果這種低槓桿動態數組是不是你的要求然後使用std :: vector。那麼你會使用rule of zero

[編輯]

你的拷貝構造函數/賦值運算符想如下:

Student (const Student& other) { 
    // here allocate this->mark to be the same size as in other 
    // copy values from other.mark to this->mark 
    // also copy other values from `other` to this 
} 

Student& operator= (const Student& other) 
{ 
    // here allocate this->mark to be the same size as in other 
    // copy values from other.mark to this->mark 
    // also copy other values from `other` to this 
    return *this; 
} 

正如你可以看到他們是 「相當」 類似。關於三規則的Wiki實際上已更新爲C++ 11,您可以在其中添加移動語義,以提高按值操作複製效率(實際上稱爲五規則)。如果你正在學習基礎知識,你可以保持以前的規則。

+0

marcin_j感謝您的快速回復!正如你在我的代碼中看到的,我寫了析構函數。但我如何需要複製構造函數?我不明白。什麼是賦值運算符?我以前從未使用過這個東西。 – David

+0

您正在代碼中複製Student,例如'Stud [n ++] = S',這使用operator =(查看我的答案中的鏈接),使這樣的副本正確您需要實現operator =來分配新的動態數組。 C++中的對象是按值複製的,而不是像C#或Java中那樣引用。 – marcinj

+0

謝謝我在帖子中做了一些修改。 (也在pastebin中)。 – David

0

你可以更好地使用std::vector將學生包裝成一個集合。這將顯着提高性能,並會導致更少的錯誤。

下面是您可以使用的類的原型。

class Student 
{ 
private: 
    string mName; 
    unsigned int mAge; 
public: 
    Student() : mName(""), mAge(0) {} 
    Student(string name, unsigned int age) : mName(name), mAge(age) {} 
    string get_name() const; 
    void set_name(string name); 
    unsigned int get_age() const; 
    void set_age(unsigned int age); 
}; 

class Students 
{ 
private: 
    vector<Student> mCollection; 
public: 
    Students() : mCollection() { } 
    Students(vector<Student> &input_collection) : mCollection(input_collection) { } 
    Students &add_student(Student &s); 
    Students &remove_student(Student &s); 
    Students &remove_student(string name); 
    Students &remove_student(size_t index); 
    Students &edit_student(size_t index, string new_name, unsigned int new_age); 
}; 

希望這會有所幫助。

+0

謝謝維克多。我知道這個矢量是非常有用的東西。但我想了解更多關於動態數組的知識。 – David

+0

我明白了,先生:D – Victor