0
#include<iostream>
#include<string>
using namespace std;

class Student {
public:
    const int codeStud;
    int noGrades = 0;
    int* grades = NULL;

    Student(int code) :codeStud(code) {
    }

    Student(int code, int* grades, int noGrades) :codeStud(code) {
        this->noGrades = noGrades;
        this->grades = new int[noGrades];
        for (int i = 0; i < noGrades; i++)
            this->grades[i] = grades[i];

    }

    Student(const Student&existent):codeStud(existent.codeStud) {
        this->noGrades = existent.noGrades;
        this->grades = new int[this->noGrades];
        for (int i = 0; i < this->noGrades; i++)
            this->grades[i] = existent.grades[i];
    }

    int getCode() {
        return this->codeStud;
    }

    int getNoGrades() {
        return this->noGrades;
    }

    void setGrades(int grades[],int noGrades) {
        this->noGrades = noGrades;
        this->grades = new int[noGrades];
        for (int i = 0; i < noGrades; i++)
            this->grades[i] = grades[i];
    }
};

void main() {

    Student s1(101);
    cout<<s1.getNoGrades();
    int grades[] = { 10,7,8,10,4 };
    Student s2(104, grades, 5);
    cout << "\n" << s2.getNoGrades();

    Student s3 = s2;
    cout << "\n" << s3.getCode();

    int grades2[] = { 5,5,4,10 };
    s1.setGrades(grades2,4);
    cout << "\n" << s1.getNoGrades(); // here is the problem
}

After I changed the grades for student 1 it shows that he has 0 grades, when the output should be 4, the number of these grades: 5,5,4,10. The rest of output is correct, even when I want to know the number of grades for student 1, which is 0 , and then for student 2, which is 5.

Andreea Elena
  • 135
  • 1
  • 8
  • Odd. Looks fine to me. On my phone so difficult to actually run it. Are you reading your output correctly? – Andrew Cheong Nov 10 '19 at 15:20
  • 2
    Other than a wee typo (you have `const int codeStud;` but should be `const int codStud;` - as referenced elsewhere), the code runs as expected for me. – Adrian Mole Nov 10 '19 at 15:25
  • 1
    Maybe you are not running the current code because of the typo listed above. – drescherjm Nov 10 '19 at 15:43
  • I translated the name of variables in English, my code has no errors of this type. It keeps showing 0 instead of 4 :\. Also, I restarted my pc. – Andreea Elena Nov 10 '19 at 15:48

2 Answers2

0

I've changed some things in your code to compile it


#include<iostream>
#include<string>
using namespace std;

class Student {
public:
    int codeStud;
    int noGrades = 0;
    int* grades = NULL;

    Student(int code) {
        codeStud = code;
    }

    Student(int code, int* grades, int noGrades) {
        this->noGrades = noGrades;
        this->grades = new int[noGrades];
        for (int i = 0; i < noGrades; i++)
            this->grades[i] = grades[i];

    }

    Student(const Student&existent){
        this->noGrades = existent.noGrades;
        this->grades = new int[this->noGrades];
        for (int i = 0; i < this->noGrades; i++)
            this->grades[i] = existent.grades[i];
    }

    int getCode() {
        return this->codeStud;
    }

    int getNoGrades() {
        return this->noGrades;
    }

    void setGrades(int grades[],int noGrades) {
        this->noGrades = noGrades;
        this->grades = new int[noGrades];
        for (int i = 0; i < noGrades; i++)
            this->grades[i] = grades[i];
    }
};

int main() {

    Student s1(101);
    cout<<s1.getNoGrades();
    int grades[] = { 10,7,8,10,4 };
    Student s2(104, grades, 5);
    cout << "\n" << s2.getNoGrades();

    Student s3 = s2;

    int grades2[] = { 5,5,4,10 };
    s1.setGrades(grades2,4);
    cout << "\n" << s1.getNoGrades(); // here is the problem
}

and output is:

0
5
4

what is correct, because you don't assign number of grades of s1 anywhere in your code before first printing

Also look for: https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c why void main() is not correct

alekq
  • 137
  • 2
  • 15
0

After I corrected the typo (codStud --> codeStud) your code produced the correct results for me. Before I did that I couldn't even compile it, so my guess would be that your IDE just run the latest working version of it that could be compiled successfully (look for error messages somewhere). That's the reason you got the wrong result, because your changes weren't even in that version.

A couple of note about your code:

  • In your setGrades function check that grades not pointing to something already. For example, if I call Student(int code, int* grades, int noGrades) and after I call setGrades your code leaks memory because it loses the array that Student(int code, int* grades, int noGrades) allocated before.

  • You should use vector instead of C-style arrays. It will make your code much more cleaner and less error-prone (see my example).

  • You could make your getter functions to const (like in my example), so it would be guaranteed that those functions don't change the value of any member of the class (you get a compile error if they do). Other than that, you can make the member variables to private.

Implementation using vectors:

#include <iostream> // cout
#include <vector> // vector

using namespace std;

class Student
{
public:
    Student(const int code)
      : m_code{code}
    {
    }

    Student(const int code, const std::vector<int>& grades)
      : m_code{code},
        m_grades{grades}
    {
    }

    // Default copy constructor is sufficient because the class can be copied
    // memberwise.

    int getCode() const {
        return m_code;
    }

    int getNoGrades() const {
        return m_grades.size();
    }

    void setGrades(const std::vector<int>& grades) {
        m_grades = grades;
    }

private:
    const int m_code;
    std::vector<int> m_grades;
};

int main()
{
    Student s1(101);
    cout << s1.getNoGrades();
    Student s2(104, {10, 7, 8, 10, 4});
    cout << "\n" << s2.getNoGrades();

    Student s3 = s2;
    cout << "\n" << s3.getCode();

    s1.setGrades({5, 5, 4, 10});
    cout << "\n" << s1.getNoGrades();

    return 0;
}
Doeus
  • 430
  • 1
  • 3
  • 7