Question

In: Computer Science

Must be written in C++ in Visual studios community In this lab, you will modify the...

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;

};

Solutions

Expert Solution

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).


Related Solutions

Program is to be written in C++ using Visual studios Community You are to design a...
Program is to be written in C++ using Visual studios Community You are to design a system to keep track of either a CD or DVD/Blu-ray collection. The program will only work exclusively with either CDs or DVDs/Blu-rays since some of the data is different. Which item your program will work with will be up to you. Each CD/DVD/Blu-ray in the collection will be represented as a class, so there will be one class that is the CD/DVD/Blu-ray. The CD...
Code should be written in C++ using Visual Studios Community This requires several classes to interact...
Code should be written in C++ using Visual Studios Community This requires several classes to interact with each other. Two class aggregations are formed. The program will simulate a police officer giving out tickets for parked cars whose meters have expired. You must include both a header file and an implementation file for each class. Car class (include Car.h and Car.cpp) Contains the information about a car. Contains data members for the following String make String model String color String...
MUST BE WRITTEN IN ASSEMBLY LANGUAGE ONLY AND MUST COMPILE IN VISUAL STUDIO You will write...
MUST BE WRITTEN IN ASSEMBLY LANGUAGE ONLY AND MUST COMPILE IN VISUAL STUDIO You will write a simple assembly language program that performs a few arithmetic operations. This will require you to establish your programming environment and create the capability to assemble and execute the assembly programs that are part of this course. Your \student ID number is a 7-digit number. Begin by splitting your student ID into two different values. Assign the three most significant digits to a variable...
C# Programming language!!! Using visual studios if possible!! PrimeHealth Suite You will create an application that...
C# Programming language!!! Using visual studios if possible!! PrimeHealth Suite You will create an application that serves as a healthcare billing management system. This application is a multiform project (Chapter 9) with three buttons. The "All Accounts" button allows the user to see all the account information for each patient which is stored in an array of class type objects (Chapter 9, section 9.4).The "Charge Service" button calls the Debit method which charges the selected service to the patient's account...
Must be written in C: Lab 3: Logical Expressions and if Statements Project Goals The goals...
Must be written in C: Lab 3: Logical Expressions and if Statements Project Goals The goals of this project are to: 1. Get students familiar with evaluating logical expressions 2. Get students familiar with if - else statements Important Notes: 1. Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text of the problem from “ Enter the number of loaves of bread and price per loaf: ”...
Use C++ please Implement the following exercise on Visual Studios and submit the necessary (.h and...
Use C++ please Implement the following exercise on Visual Studios and submit the necessary (.h and .cpp) files in a .zip folder 1.   Create a class NumberType such that it has the following functionality: The draw function in NumberType only prints 'this is an empty function' message. Create a class NumberOne (derived from NumberType) which in the draw function prints the number using a 5(rows)x3(columns) matrix. See example at https://www.istockphoto.com/vector/led-numbers-gm805084182-130563203 * * * * * Similarly create a class NumberTwo...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a)...
DEVELOP IN VISUAL STUDIOS C++ PLEASE 1. Develop a main program that does the following a) Create six nodes of integers such as n0, n1, n2, n3, n4, n5 (n0 is the head) b) Assign data to each nodes such as n1->data = 2; n2->data = 5, n3->data = 3, n4->data = 10, n5->data = 1. c) Make n0 ->next to point to n1, and n0->prev to point to NULL 2.) Print out the content of list you have created...
Must be written in Java: After completing this lab, you should be able to: Write a...
Must be written in Java: After completing this lab, you should be able to: Write a Java class to represent Time. Create default and parameterized constructors. Create accessor and mutator methods. Write a toString method. This project will represent time in hours, minutes, and seconds. Part 1: Create a new Java class named Time that has the following instance fields in the parameterized constructor: hours minutes seconds Create a default constructor that sets the time to that would represent the...
I need this code in C++ form using visual studios please: Create a class that simulates...
I need this code in C++ form using visual studios please: Create a class that simulates an alarm clock. In this class you should: •       Store time in hours, minutes, and seconds. Note if time is AM or PM. (Hint: You should have separate private members for the alarm and the clock. Do not forget to have a character variable representing AM or PM.) •       Initialize the clock to a specified time. •       Allow the clock to increment to the...
Using Visual Studio Code (JavaScript) For this lab you must use a reasonable faceted search example,...
Using Visual Studio Code (JavaScript) For this lab you must use a reasonable faceted search example, each item must have at least three categorical attributes and at least one numeric attribute. Attributes such as ID, name etc do not count as categorical or numeric attributes. Exercise 1 (a) Define a class for your item that meets the above three categorical and one numeric attribute requirements. (b) Create a text file that contains at least 5 such records in CSV format....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT