Home > Blockchain >  Static class variable initializing to 100 by itself
Static class variable initializing to 100 by itself

Time:03-17

This is my first question on here, so excuse me if I've formatted everything in a wrong way.

So, to get to the problem - this is s university assignment of mine. The goal is to create a class called Student, which has a few fields, and store the instances in an array of Student objects. One of the tasks is to have a static variable inside the class that keeps track of how many Student instances have been created. To clarify, I have a getData() method that asks the user for the values, and then sets the current object's values to those entered (basically just like a constructor, which makes the constructors obsolete, but they want us to do it that way for some reason). In the getData() function, the static count variable gets raised by 1, as well as in the constructors, and gets decremented in the destructor.

The issue is that for some reason, Student::amount variable sets itself to 100. Every time that i try to access it from the main() function, its 100 plus the amount of students created, so if we have created 2 students, Student::amount will be equal to 102. I've nowhere explicitly set it to that number, which also matches the size of the array, by the way.

I'm still rather new to C , I've read and watched some material on how OOP works here, but I've barely even scratched the surface.

Sorry again if my question is badly formulated or/and badly formatted, and I hope you can help me with this!

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

class Date {
...
};

class Student {
    // Fields
private:
    string name = "";
    string PID = "";
    int marks[5]{ 0 };
    short course = 0;
public:
    // Count of all students
    static int amount;

    // Getters
    ...

    // Setters
    ...

    // Constructors
    Student(); // Default one, Student::amount gets raised by 1 there
    Student(string name, string PID, int marks[5], short course);

    // Destructor
    ~Student();

    // Methods
    void getData();
    void display(); // Display the information of a student
};

// Array of students
Student students[100];

// Student::Count
int Student::amount; // Initializes with 0 if not explicitly initialized

void Student::getData() {
    cin.ignore();
    cout << "\nName: "; getline(cin, name);
    cout << "PID: "; getline(cin, PID);
    cout << "Marks:\n";
    for (int i = 0; i < 5; i  )
    {
        cin >> marks[i];
    }
    cout << "Course: "; cin >> course;
    Student::amount  ;
}

Student::Student(string name, string PID, int marks[5], short course) {
    this->setName(name);
    this->setPID(PID);
    this->setMarks(marks);
    this->setCourse(course);
    Student::amount  ;
}

CodePudding user response:

The global declaration Student students[100]; calls the default Student constructor 100 times, before main is reached. According to your comment (you don't supply the constructor implementation), that constructor increases amount by 1.

A solution here is to remove Student::amount and instead use

std::vector<Student> students;

students.size() will give you the number of students in that container. Use push_back to put students into the vector.

A very crude alternative that at least is broadly compliant with the question constraints is to remove the amount increment from the default constructor, and all other places apart from the four argument constructor.

  • Related