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

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...
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...
C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i...
C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i <= 0) { cout << "!"; --i; }
The solution has to be written on C++ Visual Studio Thank you (Package Inheritance Hierarchy) Package-delivery...
The solution has to be written on C++ Visual Studio Thank you (Package Inheritance Hierarchy) Package-delivery services, such as FedEx®, DHL® and UPS®, offer a number of different shipping options, each with specific costs associated. Create an inheritance hierarchy to represent various types of packages. Use class Package as the base class of the hierarchy, then include classes TwoDayPackage and OvernightPackage that derive from Package. Base class Package should include data members representing the name, address, city, state and ZIP...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read. 2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer...
Data Manipulation In this lab, you will be manipulating the database to add, delete and modify...
Data Manipulation In this lab, you will be manipulating the database to add, delete and modify the values in the database. Please use a "select * from..." after each query to show the effects of your data manipulation query. 1. The title 'Time Flies' now has a new track, the 11th track 'Spring', which is 150 seconds long and has only a MP3 file. Insert the new track into Tracks table (Don’t hand-code any data for insert that can be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT