In: Computer Science
Must be written in C++ in Visual studios community
In this lab, you will modify the Student class you created in a previous lab. You will modify one new data member which will be a static integer data member. Call that data member count. You also add a static method to the Student class that will display the value of count with a message indicating what the value represents, meaning I do not want to just see a value printed to the screen.
Change main so that it calls this new method towards the end of the program. Call the method using the static syntax; do not use one of the instances of the Student class.
You will also need to change the Student constructor so that it increments the new data member you added, count.
Explain why you get the value it displays. Use codes below.
FIRST CODE
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
Student rich(2);
rich.DisplayStudent();
Student mary(3);
mary.DisplayStudent();
return 0;
}
SECOND CODE
#include "Student.h"
Student::Student(int numGrades)
{
quanity = numGrades;
grades = new int[numGrades];
name = "rich";
grades[0] = 88;
grades[1] = 96;
}
Student::~Student()
{
delete[] grades;
}
void Student::DisplayStudent()
{
cout << "Grades for " << name << endl;
for (int index = 0; index < quanity; index++)
{
cout << *(grades + index) << endl;
}
}
THIRD CODE
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student(int numGrades);
~Student();
void DisplayStudent();
private:
string name;
int* grades;
int quanity;
};
Here is the C++ code to the given question.
All the three parts of the code is provided.
Output screenshot is added after the code.
Explanation of the program (asked in question) is added at the end.
FIRST CODE:
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
Student rich(2);
rich.DisplayStudent();
Student mary(3);
mary.DisplayStudent();
Student::displayCount(); /*calls the static function displayCount()*/
return 0;
}
SECOND CODE:
#include "Student.h"
int Student::count = 0; /*initializing static variable*/
void Student::displayCount()
{
cout<<"The number of student objects created are: "<<count; /*prints the number of student objects created*/
}
Student::Student(int numGrades)
{
quanity = numGrades;
grades = new int[numGrades];
name = "rich";
grades[0] = 88;
grades[1] = 96;
count+=1; /*increments the count by 1*/
}
Student::~Student()
{
delete[] grades;
}
void Student::DisplayStudent()
{
cout << "Grades for " << name << endl;
for (int index = 0; index < quanity; index++)
{
cout << *(grades + index) << endl;
}
}
THIRD CODE:
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Student
{
public:
Student(int numGrades);
~Student();
void DisplayStudent();
static void displayCount(); /*new static method*/
private:
string name;
int* grades;
int quanity;
static int count; /*new static variable*/
};
Output:
Explanation:
The variable 'count' is declared as a static variable, meaning that it's value does not belong to a particular instance(object) of the class, but belongs to the class itself.
In other words, the value of count is shared by all instances of Student class.
Initially, the value of count is set to 0.
In the class constructor, the value of count is incremented by 1. This means that every time we create a new Student object, the value of count gets incremented by 1. In this particular program, two Student objects are created , so the value of count is 2.
To access this static variable, we need a static function. In this program displayCount() is a static function that prints the value of count(which is 2 in this case).