96.2k views
2 votes
Create a class Students that has three data members’ id, name and static numOfStudents.

Define a method that is used to give the data members new values, and add 1 to numOfStudents.
A method that is used to print student’s information.
Create three objects of Student’s class and use the available methods for each object.

1 Answer

4 votes

Final answer:

The Students class contains data members for id and name, and a static member numOfStudents which is incremented when a new student is added. A method is defined to set values and another to print student information. Three objects can be created, with their information set and displayed using these methods.

Step-by-step explanation:

Students Class in Programming

To fulfill the task, we need to create a 'Students' class in programming that contains three data members: 'id', 'name', and a static member 'numOfStudents'. The class will have a method to set the student's id and name, and increment 'numOfStudents' by 1 each time a new student object is created. Another method will be defined to print out the student's information.

Class Definition Example:

class Students {
public:
int id;
string name;
static int numOfStudents;

Students(int id, string name) {
this->id = id;
this->name = name;
numOfStudents++;
}

void printInfo() {
cout << "ID: " << id << ", Name: " << name << endl;
}
};

int Students::numOfStudents = 0;

With this setup, we can create three objects of the 'Students' class and use the provided constructor and 'printInfo' method to assign values and display each student's information. Each new instance of a student will automatically increase 'numOfStudents'.

User Fabien Demangeat
by
8.3k points